View Javadoc

1   package com.atlassian.vcache.internal.core;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   import static com.atlassian.vcache.internal.NameValidator.requireValidProductIdentifier;
7   
8   /**
9    * Represents the ability to generate the keys used with an external cache.
10   *
11   * @since 1.0.0
12   */
13  public abstract class ExternalCacheKeyGenerator {
14      private static final Logger log = LoggerFactory.getLogger(ExternalCacheKeyGenerator.class);
15  
16      private static final String SEPARATOR = "::";
17      private static final String CACHE_VERSION_KEY = "cache-version";
18  
19      private final String productIdentifier;
20  
21      public ExternalCacheKeyGenerator(String productIdentifier) {
22          this.productIdentifier = requireValidProductIdentifier(productIdentifier);
23      }
24  
25      /**
26       * Generates the key for an individual entry.
27       */
28      public String entryKey(String partition, String cacheName, long cacheVersion, String entryKey) {
29          final String plain = productIdentifier + SEPARATOR + partition + SEPARATOR + cacheName
30                  + SEPARATOR + cacheVersion + SEPARATOR + entryKey;
31          final String encoded = encode(plain);
32          log.trace("Encoded cacheKey {} to {}", plain, encoded);
33          return encoded;
34      }
35  
36      /**
37       * Generates the key used to hold the cache version number.
38       */
39      public String cacheVersionKey(String partition, String cacheName) {
40          final String plain = productIdentifier + SEPARATOR + partition + SEPARATOR + cacheName
41                  + SEPARATOR + CACHE_VERSION_KEY;
42          final String encoded = encode(plain);
43          log.trace("Encoded cacheVersionKey {} to {}", plain, encoded);
44          return encoded;
45      }
46  
47      /**
48       * Responsible for encoding the plain key using the appropriate algorithm.
49       *
50       * @param plain the plain key to be encoded
51       * @return the key encoded
52       */
53      protected abstract String encode(String plain);
54  }