Skip to content

Commit ca2e183

Browse files
committed
WIP ehcache 3
1 parent c2f654c commit ca2e183

File tree

5 files changed

+117
-35
lines changed

5 files changed

+117
-35
lines changed

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,16 @@
8383
</dependency>
8484

8585
<dependency>
86-
<groupId>net.sf.ehcache</groupId>
86+
<groupId>org.ehcache</groupId>
8787
<artifactId>ehcache</artifactId>
88-
<version>2.10.9.2</version>
88+
<version>3.10.8</version>
8989
<scope>compile</scope>
90+
<exclusions>
91+
<exclusion>
92+
<groupId>*</groupId>
93+
<artifactId>*</artifactId>
94+
</exclusion>
95+
</exclusions>
9096
</dependency>
9197

9298
<dependency>

src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717

1818
import 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
}

src/main/java/org/mybatis/caches/ehcache/EhBlockingCache.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
*/
1616
package org.mybatis.caches.ehcache;
1717

18-
import net.sf.ehcache.Ehcache;
19-
import net.sf.ehcache.Element;
20-
import net.sf.ehcache.constructs.blocking.BlockingCache;
18+
import java.time.Duration;
19+
import java.time.temporal.ChronoUnit;
20+
21+
import org.ehcache.Cache;
22+
import org.ehcache.config.builders.CacheConfigurationBuilder;
23+
import org.ehcache.config.builders.ExpiryPolicyBuilder;
24+
import org.ehcache.config.builders.ResourcePoolsBuilder;
25+
import org.ehcache.config.units.EntryUnit;
26+
import org.ehcache.config.units.MemoryUnit;
2127

2228
/**
2329
* The Class EhBlockingCache.
@@ -34,20 +40,26 @@ public class EhBlockingCache extends AbstractEhcacheCache {
3440
*/
3541
public EhBlockingCache(final String id) {
3642
super(id);
37-
if (!CACHE_MANAGER.cacheExists(id)) {
38-
CACHE_MANAGER.addCache(this.id);
39-
Ehcache ehcache = CACHE_MANAGER.getEhcache(this.id);
40-
BlockingCache blockingCache = new BlockingCache(ehcache);
41-
CACHE_MANAGER.replaceCacheWithDecoratedCache(ehcache, blockingCache);
43+
if (CACHE_MANAGER.getCache(id, Object.class, Object.class) == null) {
44+
CACHE_MANAGER.createCache(this.id, CacheConfigurationBuilder
45+
.newCacheConfigurationBuilder(Object.class, Object.class,
46+
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(this.maxEntriesLocalHeap, EntryUnit.ENTRIES)
47+
.offheap(this.maxEntriesLocalDisk, MemoryUnit.MB))
48+
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.of(this.timeToIdleSeconds, ChronoUnit.SECONDS)))
49+
.withExpiry(
50+
ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(this.timeToLiveSeconds, ChronoUnit.SECONDS))));
51+
Cache<Object, Object> ehcache = CACHE_MANAGER.getCache(this.id, Object.class, Object.class);
52+
// BlockingCache blockingCache = new BlockingCache(ehcache);
53+
// CACHE_MANAGER.replaceCacheWithDecoratedCache(ehcache, blockingCache);
4254
}
43-
this.cache = CACHE_MANAGER.getEhcache(id);
55+
this.cache = CACHE_MANAGER.getCache(id, Object.class, Object.class);
4456
}
4557

4658
@Override
4759
public Object removeObject(Object key) {
4860
// this method is called during a rollback just to
4961
// release any previous lock
50-
cache.put(new Element(key, null));
62+
cache.put(key, null);
5163
return null;
5264
}
5365

src/main/java/org/mybatis/caches/ehcache/EhcacheCache.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
*/
1616
package org.mybatis.caches.ehcache;
1717

18+
import java.time.Duration;
19+
import java.time.temporal.ChronoUnit;
20+
21+
import org.ehcache.config.builders.CacheConfigurationBuilder;
22+
import org.ehcache.config.builders.ExpiryPolicyBuilder;
23+
import org.ehcache.config.builders.ResourcePoolsBuilder;
24+
import org.ehcache.config.units.EntryUnit;
25+
import org.ehcache.config.units.MemoryUnit;
26+
1827
public class EhcacheCache extends AbstractEhcacheCache {
1928

2029
/**
@@ -25,10 +34,16 @@ public class EhcacheCache extends AbstractEhcacheCache {
2534
*/
2635
public EhcacheCache(String id) {
2736
super(id);
28-
if (!CACHE_MANAGER.cacheExists(id)) {
29-
CACHE_MANAGER.addCache(id);
37+
if (CACHE_MANAGER.getCache(id, Object.class, Object.class) == null) {
38+
CACHE_MANAGER.createCache(this.id, CacheConfigurationBuilder
39+
.newCacheConfigurationBuilder(Object.class, Object.class,
40+
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(this.maxEntriesLocalHeap, EntryUnit.ENTRIES)
41+
.offheap(this.maxEntriesLocalDisk, MemoryUnit.MB))
42+
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.of(this.timeToIdleSeconds, ChronoUnit.SECONDS)))
43+
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(this.timeToLiveSeconds, ChronoUnit.SECONDS)))
44+
.withKeySerializer(ObjectSerializer.class).withValueSerializer(ObjectSerializer.class));
3045
}
31-
this.cache = CACHE_MANAGER.getEhcache(id);
46+
this.cache = CACHE_MANAGER.getCache(id, Object.class, Object.class);
3247
}
3348

3449
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2010-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.caches.ehcache;
17+
18+
import java.nio.ByteBuffer;
19+
20+
import org.ehcache.spi.serialization.Serializer;
21+
import org.ehcache.spi.serialization.SerializerException;
22+
23+
public class ObjectSerializer implements Serializer<Object> {
24+
25+
public ObjectSerializer(ClassLoader loader) {
26+
// no-op
27+
}
28+
29+
@Override
30+
public boolean equals(Object arg0, ByteBuffer arg1) throws ClassNotFoundException, SerializerException {
31+
return false;
32+
}
33+
34+
@Override
35+
public Object read(ByteBuffer arg0) throws ClassNotFoundException, SerializerException {
36+
return null;
37+
}
38+
39+
@Override
40+
public ByteBuffer serialize(Object arg0) throws SerializerException {
41+
return null;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)