View Javadoc

1   package com.atlassian.vcache.internal.redis;
2   
3   import com.atlassian.marshalling.api.MarshallingPair;
4   import com.atlassian.vcache.DirectExternalCache;
5   import com.atlassian.vcache.ExternalCacheSettings;
6   import com.atlassian.vcache.JvmCache;
7   import com.atlassian.vcache.JvmCacheSettings;
8   import com.atlassian.vcache.StableReadExternalCache;
9   import com.atlassian.vcache.TransactionalExternalCache;
10  import com.atlassian.vcache.internal.BegunTransactionalActivityHandler;
11  import com.atlassian.vcache.internal.RequestContext;
12  import com.atlassian.vcache.internal.VCacheCreationHandler;
13  import com.atlassian.vcache.internal.VCacheSettingsDefaultsProvider;
14  import com.atlassian.vcache.internal.core.ExternalCacheKeyGenerator;
15  import com.atlassian.vcache.internal.core.Sha1ExternalCacheKeyGenerator;
16  import com.atlassian.vcache.internal.core.metrics.MetricsCollector;
17  import com.atlassian.vcache.internal.core.service.AbstractVCacheService;
18  import com.atlassian.vcache.internal.core.service.GuavaJvmCache;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import redis.clients.jedis.Jedis;
22  
23  import java.util.function.Supplier;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * Main service that is backed by <tt>Redis</tt>.
29   *
30   * @since 1.0.0
31   */
32  public class RedisVCacheService extends AbstractVCacheService {
33      private static final Logger log = LoggerFactory.getLogger(RedisVCacheService.class);
34  
35      private final Supplier<Jedis> clientSupplier;
36  
37      /**
38       * Creates an instance, with the default {@link com.atlassian.vcache.internal.core.ExternalCacheKeyGenerator}.
39       *
40       * @param productIdentifier             the unique product identifier, such as <tt>jira</tt> or <tt>confluence</tt>
41       * @param clientSupplier                the supplier for the Memcached client
42       * @param threadLocalContextSupplier   the supplier for the request context.
43       * @param workContextContextSupplier   A supplier of a thread safe request context.
44       * @param defaultsProvider              the cache defaults provider
45       * @param creationHandler               the handler called when caches are being created.
46       * @param metricsCollector              the collector for gathering metrics.
47       */
48      public RedisVCacheService(String productIdentifier,
49                                Supplier<Jedis> clientSupplier,
50                                Supplier<RequestContext> threadLocalContextSupplier,
51                                Supplier<RequestContext> workContextContextSupplier,
52                                VCacheSettingsDefaultsProvider defaultsProvider,
53                                VCacheCreationHandler creationHandler,
54                                MetricsCollector metricsCollector,
55                                BegunTransactionalActivityHandler begunTransactionalActivityHandler) {
56          super(threadLocalContextSupplier,
57                  workContextContextSupplier,
58                  defaultsProvider,
59                  creationHandler,
60                  metricsCollector,
61                  new Sha1ExternalCacheKeyGenerator(productIdentifier),
62                  begunTransactionalActivityHandler);
63          this.clientSupplier = requireNonNull(clientSupplier);
64      }
65  
66      /**
67       * Creates an instance, with a supplied {@link com.atlassian.vcache.internal.core.ExternalCacheKeyGenerator}.
68       *
69       * @param clientSupplier   the supplier for the Memcached client
70       * @param threadLocalContextSupplier   the supplier for the request context.
71       * @param workContextContextSupplier   A supplier of a thread safe request context.
72       * @param defaultsProvider the cache defaults provider
73       * @param creationHandler  the handler called when caches are being created.
74       * @param metricsCollector the collector for gathering metrics.
75       */
76      public RedisVCacheService(Supplier<Jedis> clientSupplier,
77                                Supplier<RequestContext> threadLocalContextSupplier,
78                                Supplier<RequestContext> workContextContextSupplier,
79                                VCacheSettingsDefaultsProvider defaultsProvider,
80                                VCacheCreationHandler creationHandler,
81                                MetricsCollector metricsCollector,
82                                ExternalCacheKeyGenerator externalCacheKeyGenerator,
83                                BegunTransactionalActivityHandler begunTransactionalActivityHandler) {
84          super(threadLocalContextSupplier,
85                  workContextContextSupplier,
86                  defaultsProvider,
87                  creationHandler,
88                  metricsCollector,
89                  externalCacheKeyGenerator,
90                  begunTransactionalActivityHandler);
91          this.clientSupplier = requireNonNull(clientSupplier);
92      }
93  
94      @Override
95      protected Logger log() {
96          return log;
97      }
98  
99      @Override
100     protected <K, V> JvmCache<K, V> createJvmCache(String name, JvmCacheSettings settings) {
101         return new GuavaJvmCache<>(name, settings);
102     }
103 
104     @Override
105     protected <V> TransactionalExternalCache<V> createTransactionalExternalCache(
106             String name, ExternalCacheSettings settings, MarshallingPair<V> valueMarshalling, boolean valueSerializable) {
107         return new RedisTransactionalExternalCache<>(
108                 clientSupplier,
109                 threadLocalContextSupplier,
110                 externalCacheKeyGenerator,
111                 name,
112                 valueMarshalling,
113                 settings,
114                 transactionControlManager,
115                 metricsCollector);
116     }
117 
118     @Override
119     protected <V> StableReadExternalCache<V> createStableReadExternalCache(String name, ExternalCacheSettings settings, MarshallingPair<V> valueMarshalling, boolean valueSerializable) {
120         return new RedisStableReadExternalCache<>(
121                 clientSupplier,
122                 workContextContextSupplier,
123                 externalCacheKeyGenerator,
124                 name,
125                 valueMarshalling,
126                 settings,
127                 metricsCollector);
128     }
129 
130     @Override
131     protected <V> DirectExternalCache<V> createDirectExternalCache(String name, ExternalCacheSettings settings, MarshallingPair<V> valueMarshalling, boolean valueSerializable) {
132         return new RedisDirectExternalCache<>(
133                 clientSupplier,
134                 workContextContextSupplier,
135                 externalCacheKeyGenerator,
136                 name,
137                 valueMarshalling,
138                 settings);
139     }
140 }