1717
1818import java .util .concurrent .locks .ReadWriteLock ;
1919
20- import net .sf .ehcache .CacheManager ;
21- import net .sf .ehcache .Ehcache ;
22- import net .sf .ehcache .Element ;
23-
24- import org .apache .ibatis .cache .Cache ;
20+ import org .ehcache .Cache ;
21+ import org .ehcache .CacheManager ;
22+ import org .ehcache .config .builders .CacheManagerBuilder ;
23+ import org .ehcache .sizeof .SizeOf ;
2524
2625/**
2726 * Cache adapter for Ehcache.
2827 *
2928 * @author Simone Tripodi
3029 */
31- public abstract class AbstractEhcacheCache implements Cache {
30+ public abstract class AbstractEhcacheCache implements org . apache . ibatis . cache . Cache {
3231
3332 /**
3433 * The cache manager reference.
3534 */
36- protected static CacheManager CACHE_MANAGER = CacheManager . create ( );
35+ protected static CacheManager CACHE_MANAGER = CacheManagerBuilder . newCacheManagerBuilder (). build ( true );
3736
3837 /**
3938 * The cache id (namespace).
@@ -43,7 +42,13 @@ public abstract class AbstractEhcacheCache implements Cache {
4342 /**
4443 * The cache instance.
4544 */
46- protected Ehcache cache ;
45+ protected Cache <Object , Object > cache ;
46+
47+ protected long timeToIdleSeconds ;
48+ protected long timeToLiveSeconds ;
49+ protected long maxEntriesLocalHeap = 1 ;
50+ protected long maxEntriesLocalDisk = 1 ;
51+ protected String memoryStoreEvictionPolicy ;
4752
4853 /**
4954 * Instantiates a new abstract ehcache cache.
@@ -63,7 +68,7 @@ public AbstractEhcacheCache(final String id) {
6368 */
6469 @ Override
6570 public void clear () {
66- cache .removeAll ();
71+ cache .clear ();
6772 }
6873
6974 /**
@@ -79,27 +84,27 @@ public String getId() {
7984 */
8085 @ Override
8186 public Object getObject (Object key ) {
82- Element cachedElement = cache .get (key );
87+ Object cachedElement = cache .get (key );
8388 if (cachedElement == null ) {
8489 return null ;
8590 }
86- return cachedElement . getObjectValue () ;
91+ return cachedElement ;
8792 }
8893
8994 /**
9095 * {@inheritDoc}
9196 */
9297 @ Override
9398 public int getSize () {
94- return cache . getSize ( );
99+ return ( int ) SizeOf . newInstance (). deepSizeOf ( cache );
95100 }
96101
97102 /**
98103 * {@inheritDoc}
99104 */
100105 @ Override
101106 public void putObject (Object key , Object value ) {
102- cache .put (new Element ( key , value ) );
107+ cache .put (key , value );
103108 }
104109
105110 /**
@@ -133,8 +138,8 @@ public boolean equals(Object obj) {
133138 return false ;
134139 }
135140
136- Cache otherCache = (Cache ) obj ;
137- return id .equals (otherCache .getId ( ));
141+ Cache < Object , Object > otherCache = (Cache < Object , Object > ) obj ;
142+ return id .equals (otherCache .get ( id ));
138143 }
139144
140145 /**
@@ -167,7 +172,7 @@ public String toString() {
167172 * the default amount of time to live for an element from its last accessed or modified date
168173 */
169174 public void setTimeToIdleSeconds (long timeToIdleSeconds ) {
170- cache . getCacheConfiguration (). setTimeToIdleSeconds ( timeToIdleSeconds ) ;
175+ this . timeToIdleSeconds = timeToIdleSeconds ;
171176 }
172177
173178 /**
@@ -177,7 +182,7 @@ public void setTimeToIdleSeconds(long timeToIdleSeconds) {
177182 * the default amount of time to live for an element from its creation date
178183 */
179184 public void setTimeToLiveSeconds (long timeToLiveSeconds ) {
180- cache . getCacheConfiguration (). setTimeToLiveSeconds ( timeToLiveSeconds ) ;
185+ this . timeToLiveSeconds = timeToLiveSeconds ;
181186 }
182187
183188 /**
@@ -187,7 +192,7 @@ public void setTimeToLiveSeconds(long timeToLiveSeconds) {
187192 * The maximum number of elements in heap, before they are evicted (0 == no limit)
188193 */
189194 public void setMaxEntriesLocalHeap (long maxEntriesLocalHeap ) {
190- cache . getCacheConfiguration (). setMaxEntriesLocalHeap ( maxEntriesLocalHeap ) ;
195+ this . maxEntriesLocalHeap = maxEntriesLocalHeap ;
191196 }
192197
193198 /**
@@ -197,7 +202,7 @@ public void setMaxEntriesLocalHeap(long maxEntriesLocalHeap) {
197202 * the maximum number of Elements to allow on the disk. 0 means unlimited.
198203 */
199204 public void setMaxEntriesLocalDisk (long maxEntriesLocalDisk ) {
200- cache . getCacheConfiguration (). setMaxEntriesLocalDisk ( maxEntriesLocalDisk ) ;
205+ this . maxEntriesLocalDisk = maxEntriesLocalDisk ;
201206 }
202207
203208 /**
@@ -207,7 +212,7 @@ public void setMaxEntriesLocalDisk(long maxEntriesLocalDisk) {
207212 * a String representation of the policy. One of "LRU", "LFU" or "FIFO".
208213 */
209214 public void setMemoryStoreEvictionPolicy (String memoryStoreEvictionPolicy ) {
210- cache . getCacheConfiguration (). setMemoryStoreEvictionPolicy ( memoryStoreEvictionPolicy ) ;
215+ this . memoryStoreEvictionPolicy = memoryStoreEvictionPolicy ;
211216 }
212217
213218}
0 commit comments