Skip to content

Commit a24fa5e

Browse files
committed
Ehcache3 template usage
1 parent 4e214a7 commit a24fa5e

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/cache/EHCache3Manager.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.ehcache.Cache;
2020
import org.ehcache.CacheManagerBuilder;
21-
import org.ehcache.config.CacheConfiguration;
2221
import org.ehcache.config.CacheConfigurationBuilder;
2322
import org.ehcache.config.xml.XmlConfiguration;
2423
import org.javalite.activejdbc.InitException;
@@ -38,6 +37,9 @@ public class EHCache3Manager extends CacheManager {
3837

3938
private static final Logger logger = LoggerFactory.getLogger(EHCacheManager.class);
4039
private org.ehcache.CacheManager cacheManager;
40+
private final CacheConfigurationBuilder<String, Object> cacheTemplate;
41+
42+
private final Object lock = new Object();
4143

4244

4345
public EHCache3Manager() throws ClassNotFoundException, SAXException, InstantiationException, IOException, IllegalAccessException {
@@ -48,12 +50,7 @@ public EHCache3Manager() throws ClassNotFoundException, SAXException, Instantiat
4850

4951
XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
5052

51-
// how to use this???
52-
//CacheConfigurationBuilder<String, Object> cacheBuilder
53-
// = xmlConfiguration.newCacheConfigurationBuilderFromTemplate("activejdbc", String.class, Object.class);
54-
55-
56-
53+
cacheTemplate = xmlConfiguration.newCacheConfigurationBuilderFromTemplate("activejdbc", String.class, Object.class);
5754
cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
5855
cacheManager.init();
5956
}
@@ -62,34 +59,40 @@ public EHCache3Manager() throws ClassNotFoundException, SAXException, Instantiat
6259
@Override
6360
public Object getCache(String group, String key) {
6461
try {
65-
createIfMissing(group);
66-
Cache c = cacheManager.getCache(group, String.class, Object.class);
67-
return c.get(key) == null ? null : c.get(key);
62+
Cache<String, Object> c = getCacheForGroupOrCreateIt(group);
63+
return c.get(key);
6864
} catch (Exception e) {
6965
logger.warn("{}", e, e);
7066
return null;
7167
}
7268
}
7369

74-
private void createIfMissing(String group) {
75-
//double-checked synchronization is broken in Java, but this should work just fine.
76-
if (cacheManager.getCache(group, String.class, Object.class) == null) {
77-
cacheManager.createCache(group, CacheConfigurationBuilder.newCacheConfigurationBuilder().buildConfig(String.class, Object.class));
78-
}
79-
}
80-
8170
@Override
8271
public void addCache(String group, String key, Object cache) {
83-
createIfMissing(group);
84-
cacheManager.getCache(group, String.class, Object.class).put(key, cache);
72+
getCacheForGroupOrCreateIt(group).put(key, cache);
8573
}
8674

8775
@Override
8876
public void doFlush(CacheEvent event) {
8977
if (event.getType().equals(CacheEvent.CacheEventType.ALL)) {
9078
logger.warn(getClass() + " does not support flushing all caches. Flush one group at the time.");
9179
} else if (event.getType().equals(CacheEvent.CacheEventType.GROUP)) {
92-
cacheManager.removeCache(event.getGroup());
80+
synchronized (lock) {
81+
cacheManager.removeCache(event.getGroup());
82+
}
83+
}
84+
}
85+
86+
private Cache<String, Object> getCacheForGroupOrCreateIt(String group) {
87+
Cache<String, Object> cache = cacheManager.getCache(group, String.class, Object.class);
88+
if (cache == null) {
89+
synchronized (lock) {
90+
cache = cacheManager.getCache(group, String.class, Object.class);
91+
if (cache == null) {
92+
cache = cacheManager.createCache(group, cacheTemplate.buildConfig(String.class, Object.class));
93+
}
94+
}
9395
}
96+
return cache;
9497
}
9598
}

0 commit comments

Comments
 (0)