18
18
19
19
import org .ehcache .Cache ;
20
20
import org .ehcache .CacheManagerBuilder ;
21
- import org .ehcache .config .CacheConfiguration ;
22
21
import org .ehcache .config .CacheConfigurationBuilder ;
23
22
import org .ehcache .config .xml .XmlConfiguration ;
24
23
import org .javalite .activejdbc .InitException ;
@@ -38,6 +37,9 @@ public class EHCache3Manager extends CacheManager {
38
37
39
38
private static final Logger logger = LoggerFactory .getLogger (EHCacheManager .class );
40
39
private org .ehcache .CacheManager cacheManager ;
40
+ private final CacheConfigurationBuilder <String , Object > cacheTemplate ;
41
+
42
+ private final Object lock = new Object ();
41
43
42
44
43
45
public EHCache3Manager () throws ClassNotFoundException , SAXException , InstantiationException , IOException , IllegalAccessException {
@@ -48,12 +50,7 @@ public EHCache3Manager() throws ClassNotFoundException, SAXException, Instantiat
48
50
49
51
XmlConfiguration xmlConfiguration = new XmlConfiguration (url );
50
52
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 );
57
54
cacheManager = CacheManagerBuilder .newCacheManager (xmlConfiguration );
58
55
cacheManager .init ();
59
56
}
@@ -62,34 +59,41 @@ public EHCache3Manager() throws ClassNotFoundException, SAXException, Instantiat
62
59
@ Override
63
60
public Object getCache (String group , String key ) {
64
61
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 );
68
64
} catch (Exception e ) {
69
65
logger .warn ("{}" , e , e );
70
66
return null ;
71
67
}
72
68
}
73
69
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
-
81
70
@ Override
82
71
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 );
85
73
}
86
74
87
75
@ Override
88
76
public void doFlush (CacheEvent event ) {
89
77
if (event .getType ().equals (CacheEvent .CacheEventType .ALL )) {
90
78
logger .warn (getClass () + " does not support flushing all caches. Flush one group at the time." );
91
79
} 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
+ }
93
96
}
97
+ return cache ;
94
98
}
95
99
}
0 commit comments