Skip to content

Commit a26df7e

Browse files
author
Igor Polevoy
committed
Merge pull request #447 from alexsnaps/ehcache3
Ehcache3
2 parents 4e214a7 + 5444ce9 commit a26df7e

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

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

Lines changed: 24 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,41 @@ 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+
final Cache<String, Object> cache = cacheManager.getCache(event.getGroup(), String.class, Object.class);
81+
if (cache != null) {
82+
cache.clear();
83+
}
84+
}
85+
}
86+
87+
private Cache<String, Object> getCacheForGroupOrCreateIt(String group) {
88+
Cache<String, Object> cache = cacheManager.getCache(group, String.class, Object.class);
89+
if (cache == null) {
90+
synchronized (lock) {
91+
cache = cacheManager.getCache(group, String.class, Object.class);
92+
if (cache == null) {
93+
cache = cacheManager.createCache(group, cacheTemplate.buildConfig(String.class, Object.class));
94+
}
95+
}
9396
}
97+
return cache;
9498
}
9599
}

0 commit comments

Comments
 (0)