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 if (null != settings.getExpireAfterAccess())
91 {
92 config.timeToIdleSeconds(SECONDS.convert(settings.getExpireAfterAccess(), MILLISECONDS));
93 }
94
95 if (null != settings.getExpireAfterWrite())
96 {
97 config.timeToLiveSeconds(SECONDS.convert(settings.getExpireAfterWrite(), MILLISECONDS));
98 }
99
100
101 if (settings.getExpireAfterAccess() != null || settings.getExpireAfterWrite() != null)
102 {
103 config.setEternal(false);
104 }
105
106 return ehMgr.addCacheIfAbsent(new net.sf.ehcache.Cache(config));
107 }
108
109 private boolean isReplicateCache(final CacheManager ehMgr, final CacheSettings settings)
110 {
111 final boolean isLocalCacheSetting = settings.getLocal(false);
112
113 if (!isLocalCacheSetting)
114 {
115 if (replicatorConfigFactory != null)
116 {
117 final boolean hasPeerProvider = !ehMgr.getCacheManagerPeerProviders().isEmpty();
118 if (hasPeerProvider)
119 {
120 return true;
121 }
122 else
123 {
124 log.warn("No PeerProviders configured in ehcache, replication cannot be configured for non-local cache");
125 }
126 }
127 }
128 return false;
129 }
130 }