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