View Javadoc

1   package com.atlassian.vcache.internal.memcached;
2   
3   import com.atlassian.vcache.internal.BegunTransactionalActivityHandler;
4   import com.atlassian.vcache.internal.RequestContext;
5   import com.atlassian.vcache.internal.VCacheCreationHandler;
6   import com.atlassian.vcache.internal.VCacheSettingsDefaultsProvider;
7   import com.atlassian.vcache.internal.core.ExternalCacheKeyGenerator;
8   import com.atlassian.vcache.internal.core.Sha1ExternalCacheKeyGenerator;
9   import com.atlassian.vcache.internal.core.metrics.DefaultMetricsCollector;
10  import com.atlassian.vcache.internal.core.metrics.MetricsCollector;
11  import net.spy.memcached.MemcachedClientIF;
12  
13  import java.util.function.Function;
14  import java.util.function.Supplier;
15  
16  import static java.util.Objects.requireNonNull;
17  
18  /**
19   * Builder for creating {@link MemcachedVCacheServiceSettings} instances.
20   * <p>
21   * All settings must be set before {@link #build()} can be called, except for
22   * {@link #metricsCollector(MetricsCollector)}, {@link #begunTransactionalActivityHandler(BegunTransactionalActivityHandler)}
23   * and {@link #dontExternaliseCache(Function)}. See their Javadoc for the defaults used.
24   * </p>
25   * <p>
26   * Either {@link #externalCacheKeyGenerator(ExternalCacheKeyGenerator)} or {@link #productIdentifier(String)} must be
27   * called.
28   * </p>
29   *
30   * @since 1.3.0
31   */
32  public class MemcachedVCacheServiceSettingsBuilder {
33      private Supplier<MemcachedClientIF> clientSupplier;
34      private Supplier<RequestContext> threadLocalContextSupplier;
35      private Supplier<RequestContext> workContextContextSupplier;
36      private VCacheSettingsDefaultsProvider defaultsProvider;
37      private VCacheCreationHandler creationHandler;
38      private MetricsCollector metricsCollector;
39      private ExternalCacheKeyGenerator externalCacheKeyGenerator;
40      // By default, don't care
41      private BegunTransactionalActivityHandler begunTransactionalActivityHandler = context -> {
42      };
43      // By default, externalise the external caches.
44      private Function<String, Boolean> dontExternaliseCache = name -> false;
45      // By default, don't use this hack
46      private boolean serializationHack;
47  
48      /**
49       * Sets the client supplier.
50       *
51       * @param clientSupplier the client supplier.
52       * @return the builder
53       */
54      public MemcachedVCacheServiceSettingsBuilder clientSupplier(Supplier<MemcachedClientIF> clientSupplier) {
55          this.clientSupplier = requireNonNull(clientSupplier);
56          return this;
57      }
58  
59      /**
60       * Sets the context supplier.
61       *
62       * @param threadLocalContextSupplier the context supplier.
63       * @return the builder.
64       */
65      public MemcachedVCacheServiceSettingsBuilder threadLocalContextSupplier(Supplier<RequestContext> threadLocalContextSupplier) {
66          this.threadLocalContextSupplier = requireNonNull(threadLocalContextSupplier);
67          return this;
68      }
69  
70      /**
71       * Sets the context supplier for caches that can be thread safe.
72       *
73       * @param contextSupplier the context supplier.
74       * @return the builder.
75       */
76      public MemcachedVCacheServiceSettingsBuilder workContextContextSupplier(Supplier<RequestContext> contextSupplier) {
77          this.workContextContextSupplier = contextSupplier;
78          return this;
79      }
80  
81      /**
82       * Sets the defaults provider.
83       *
84       * @param defaultsProvider the defaults provider.
85       * @return the builder.
86       */
87      public MemcachedVCacheServiceSettingsBuilder defaultsProvider(VCacheSettingsDefaultsProvider defaultsProvider) {
88          this.defaultsProvider = requireNonNull(defaultsProvider);
89          return this;
90      }
91  
92      /**
93       * Set the creation handler.
94       *
95       * @param creationHandler the creation handler.
96       * @return the builder.
97       */
98      public MemcachedVCacheServiceSettingsBuilder creationHandler(VCacheCreationHandler creationHandler) {
99          this.creationHandler = requireNonNull(creationHandler);
100         return this;
101     }
102 
103     /**
104      * Sets the metrics collector. If not invoked, then the {@link DefaultMetricsCollector} implementation will be
105      * used.
106      *
107      * @param metricsCollector the metrics collector.
108      * @return the builder.
109      */
110     public MemcachedVCacheServiceSettingsBuilder metricsCollector(MetricsCollector metricsCollector) {
111         this.metricsCollector = requireNonNull(metricsCollector);
112         return this;
113     }
114 
115     /**
116      * Equivalent to calling {@link #externalCacheKeyGenerator(ExternalCacheKeyGenerator)} passing a
117      * {@link Sha1ExternalCacheKeyGenerator} instance created using the supplied product identifier.
118      *
119      * @param productIdentifier the product identifier to use with the {@link Sha1ExternalCacheKeyGenerator}.
120      * @return the builder.
121      */
122     public MemcachedVCacheServiceSettingsBuilder productIdentifier(String productIdentifier) {
123         this.externalCacheKeyGenerator = new Sha1ExternalCacheKeyGenerator(productIdentifier);
124         return this;
125     }
126 
127     /**
128      * Set the external cache key generator.
129      *
130      * @param externalCacheKeyGenerator the external cache key generator.
131      * @return the builder.
132      */
133     public MemcachedVCacheServiceSettingsBuilder externalCacheKeyGenerator(ExternalCacheKeyGenerator externalCacheKeyGenerator) {
134         this.externalCacheKeyGenerator = requireNonNull(externalCacheKeyGenerator);
135         return this;
136     }
137 
138     /**
139      * Sets the begun transaction activity handler. If not invoked, then a default implementation is provided that does
140      * nothing.
141      *
142      * @param handler the begun transaction activity handler.
143      * @return the builder.
144      */
145     public MemcachedVCacheServiceSettingsBuilder begunTransactionalActivityHandler(BegunTransactionalActivityHandler handler) {
146         this.begunTransactionalActivityHandler = requireNonNull(handler);
147         return this;
148     }
149 
150     /**
151      * Sets the function to determine whether an {@link com.atlassian.vcache.ExternalCache} should really be externalised.
152      * If the supplied function returns <tt>true</tt> for a cache, then an in-memory implementation will be used.
153      * <p>
154      * If not set, then a default implementation is provided that will externalise all {@link com.atlassian.vcache.ExternalCache}'s.
155      * nothing.
156      * </p>
157      * <p>
158      * Note: this function is provided to allow for an orderly migration of external caches from being in-memory
159      * to be stored in Memcached.
160      * </p>
161      *
162      * @param dontExternaliseCache the function to determine whether an {@link com.atlassian.vcache.ExternalCache}
163      *                                 should really be externalised.
164      * @return the builder.
165      */
166     public MemcachedVCacheServiceSettingsBuilder dontExternaliseCache(Function<String, Boolean> dontExternaliseCache) {
167         this.dontExternaliseCache = requireNonNull(dontExternaliseCache);
168         return this;
169     }
170 
171     /**
172      * Enable the serialization hack, whereby if an {@link com.atlassian.vcache.ExternalCache}'s values are
173      * {@link java.io.Serializable}, then the values are not marshalled before they are passed to the delegate
174      * Atlassian Cache. This is to allow for performance optimisations.
175      */
176     public MemcachedVCacheServiceSettingsBuilder enableSerializationHack() {
177         serializationHack = true;
178         return this;
179     }
180 
181     /**
182      * Returns a new {@link MemcachedVCacheServiceSettings} instance configured using the supplied settings.
183      *
184      * @return a new {@link MemcachedVCacheServiceSettings} instance configured using the supplied settings.
185      */
186     public MemcachedVCacheServiceSettings build() {
187         return new MemcachedVCacheServiceSettings(
188                 requireNonNull(clientSupplier, "missing clientSupplier"),
189                 requireNonNull(threadLocalContextSupplier, "missing threadLocalContextSupplier"),
190                 requireNonNull(workContextContextSupplier, "missing thread safe workContextContextSupplier"),
191                 requireNonNull(defaultsProvider, "missing defaultsProvider"),
192                 requireNonNull(creationHandler, "missing creationHandler"),
193                 (metricsCollector != null) ? metricsCollector : new DefaultMetricsCollector(threadLocalContextSupplier),
194                 requireNonNull(externalCacheKeyGenerator, "missing externalCacheKeyGenerator"),
195                 begunTransactionalActivityHandler,
196                 dontExternaliseCache,
197                 serializationHack);
198     }
199 }