View Javadoc
1   package com.atlassian.plugin.refimpl.cache;
2   
3   import com.atlassian.vcache.VCacheFactory;
4   import com.atlassian.vcache.internal.RequestContext;
5   import com.atlassian.vcache.internal.test.EmptyVCacheSettingsDefaultsProvider;
6   import com.atlassian.vcache.internal.core.DefaultVCacheCreationHandler;
7   import com.atlassian.vcache.internal.core.ThreadLocalRequestContextSupplier;
8   import com.atlassian.vcache.internal.core.metrics.DefaultMetricsCollector;
9   import com.atlassian.vcache.internal.guava.GuavaServiceSettingsBuilder;
10  import com.atlassian.vcache.internal.guava.GuavaVCacheService;
11  
12  import java.time.Duration;
13  import java.util.function.Supplier;
14  
15  import static com.atlassian.vcache.ChangeRate.HIGH_CHANGE;
16  
17  /**
18   * I chose to follow org.springframework.beans.factory.FactoryBean interface even though we don't have spring here,
19   * because we want to add Dependency Injection at some point (see REFAPPDEV-13) and it's likely to be Spring, thus when
20   * we do add it we could just add 'implements' to this one do the happy dance
21   */
22  public class VCacheFactoryBean {
23      private static final String PRODUCT_IDENTIFIER = "refapp";
24  
25      private final Supplier<RequestContext> requestContextSupplier = ThreadLocalRequestContextSupplier.lenientSupplier(() -> "refapp-tenant");
26      private final VCacheFactory vCacheFactory = new GuavaVCacheService(
27              PRODUCT_IDENTIFIER,
28              requestContextSupplier,
29              new EmptyVCacheSettingsDefaultsProvider(),
30              cacheCreationHandler(),
31              new DefaultMetricsCollector(requestContextSupplier),
32              new GuavaServiceSettingsBuilder().build(),
33              context -> {}
34      );
35  
36      public VCacheFactoryBean() {
37      }
38  
39      public VCacheFactory getObject()
40      {
41          return vCacheFactory;
42      }
43  
44      Class getObjectType() {
45          return VCacheFactory.class;
46      }
47  
48      boolean isSingleton() {
49          return true;
50      }
51  
52      private DefaultVCacheCreationHandler cacheCreationHandler()
53      {
54          return new DefaultVCacheCreationHandler(10_000, Duration.ofHours(1), 10_000, HIGH_CHANGE, HIGH_CHANGE);
55      }
56  }