1 package com.atlassian.cache.ehcache;
2
3 import javax.annotation.Nonnull;
4 import javax.annotation.Nullable;
5 import javax.annotation.ParametersAreNonnullByDefault;
6
7 import com.atlassian.cache.CacheSettings;
8 import com.atlassian.cache.ehcache.replication.EhCacheReplicatorConfigFactory;
9 import com.atlassian.cache.ehcache.replication.rmi.RMICacheReplicatorConfigFactory;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import net.sf.ehcache.CacheManager;
15 import net.sf.ehcache.Ehcache;
16 import net.sf.ehcache.config.CacheConfiguration;
17 import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
18 import net.sf.ehcache.config.PersistenceConfiguration;
19
20 import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 import static java.util.concurrent.TimeUnit.SECONDS;
22 import static net.sf.ehcache.config.PersistenceConfiguration.Strategy.NONE;
23
24
25
26
27
28
29 @ParametersAreNonnullByDefault
30 class EhCacheHelper
31 {
32 private static final Logger log = LoggerFactory.getLogger(EhCacheHelper.class);
33
34 static final PersistenceConfiguration.Strategy PERSISTENCE_STRATEGY = NONE;
35
36 private static final PersistenceConfiguration PERSISTENCE_CONFIGURATION =
37 new PersistenceConfiguration().strategy(PERSISTENCE_STRATEGY);
38
39 private final @Nullable
40 EhCacheReplicatorConfigFactory replicatorConfigFactory;
41
42
43
44
45 @Deprecated
46 EhCacheHelper()
47 {
48 this(new RMICacheReplicatorConfigFactory());
49 }
50
51 EhCacheHelper(@Nullable EhCacheReplicatorConfigFactory replicatorConfigFactory)
52 {
53 this.replicatorConfigFactory = replicatorConfigFactory;
54 }
55
56 private CacheEventListenerFactoryConfiguration getCacheEventListenerFactoryConfiguration(
57 final CacheSettings settings, final boolean selfLoading)
58 {
59 if (replicatorConfigFactory != null)
60 {
61 return replicatorConfigFactory.createCacheReplicatorConfiguration(settings, selfLoading);
62 }
63 else
64 {
65 throw new IllegalStateException("No EhCacheReplicatorConfigFactory has been configured");
66 }
67 }
68
69 @Nonnull
70 Ehcache getEhcache(final String name, final CacheManager ehMgr, final CacheSettings settings, final boolean selfLoading, final boolean statisticsEnabled)
71 {
72
73 CacheConfiguration config = ehMgr.getConfiguration().getDefaultCacheConfiguration().clone()
74 .name(name)
75 .statistics(statisticsEnabled)
76 .persistence(PERSISTENCE_CONFIGURATION);
77
78 final boolean replicateCache = isReplicateCache(ehMgr, settings);
79 if (replicateCache)
80 {
81 config.cacheEventListenerFactory(
82 getCacheEventListenerFactoryConfiguration(settings, selfLoading));
83 }
84
85 if (null != settings.getMaxEntries())
86 {
87 config.setMaxEntriesLocalHeap(settings.getMaxEntries());
88 }
89
90
91 if (settings.getExpireAfterAccess() != null || settings.getExpireAfterWrite() != null)
92 {
93 config.setEternal(false);
94 }
95
96 if (null != settings.getExpireAfterAccess())
97 {
98 config.timeToIdleSeconds(SECONDS.convert(settings.getExpireAfterAccess(), MILLISECONDS));
99 }
100
101 if (null != settings.getExpireAfterWrite())
102 {
103 config.timeToLiveSeconds(SECONDS.convert(settings.getExpireAfterWrite(), MILLISECONDS));
104 }
105
106 Ehcache cache = new net.sf.ehcache.Cache(config);
107 if (selfLoading)
108 {
109 cache = new SynchronizedLoadingCacheDecorator(cache);
110 }
111 return ehMgr.addCacheIfAbsent(cache);
112 }
113
114 private boolean isReplicateCache(final CacheManager ehMgr, final CacheSettings settings)
115 {
116 final boolean isLocalCacheSetting = settings.getLocal(false);
117
118 if (!isLocalCacheSetting)
119 {
120 if (replicatorConfigFactory != null)
121 {
122 final boolean hasPeerProvider = !ehMgr.getCacheManagerPeerProviders().isEmpty();
123 if (hasPeerProvider)
124 {
125 return true;
126 }
127 else
128 {
129 log.warn("No PeerProviders configured in ehcache, replication cannot be configured for non-local cache");
130 }
131 }
132 }
133 return false;
134 }
135 }