1 package com.atlassian.cache.ehcache;
2
3 import com.atlassian.cache.CacheLoader;
4 import com.atlassian.cache.CacheSettings;
5 import com.atlassian.cache.impl.AbstractCacheManager;
6
7 import net.sf.ehcache.Ehcache;
8 import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
9 import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;
10
11
12
13
14
15
16 public class EhCacheManager extends AbstractCacheManager
17 {
18 private net.sf.ehcache.CacheManager delegate;
19
20
21
22
23 public EhCacheManager()
24 {
25 this(net.sf.ehcache.CacheManager.create());
26 }
27
28
29
30
31
32
33 public EhCacheManager(net.sf.ehcache.CacheManager delegate)
34 {
35 this.delegate = delegate;
36 }
37
38 net.sf.ehcache.CacheManager getEh()
39 {
40 return delegate;
41 }
42
43 protected <V> DelegatingCachedReference<V> createLazyReference(
44 final String name,
45 final com.atlassian.cache.Supplier<V> supplier,
46 final CacheSettings settings)
47 {
48 final Ehcache cache =
49 new EhCacheHelper().getEhcache(name, delegate, settings);
50
51 final Ehcache spCache = new SelfPopulatingCache(cache, new CacheEntryFactory()
52 {
53 @Override
54 public Object createEntry(final Object o) throws Exception
55 {
56 return supplier.get();
57 }
58 });
59
60 return DelegatingCachedReference.create(spCache, settings);
61 }
62
63 protected <K, V> DelegatingCache<K, V> createComputingCache(final String name, final CacheSettings settings, final CacheLoader<K, V> loader)
64 {
65 final Ehcache cache = new EhCacheHelper().getEhcache(name, delegate, settings);
66
67 Ehcache spCache = new SelfPopulatingCache(cache, new CacheEntryFactory()
68 {
69 @Override
70 public Object createEntry(final Object key) throws Exception
71 {
72 if (key == null)
73 {
74 throw new NullPointerException();
75 }
76 @SuppressWarnings("unchecked")
77 final K loaderKey = (K) key;
78 return loader.load(loaderKey);
79 }
80 });
81
82 return DelegatingCache.create(spCache, settings);
83 }
84
85 protected DelegatingCache createSimpleCache(String name, CacheSettings settings)
86 {
87 final Ehcache cache = new EhCacheHelper().getEhcache(name, delegate, settings);
88
89 return DelegatingCache.create(cache, settings);
90 }
91 }