From d5ab5ac35147e782b8ee1d884ddef38a8518bcf3 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sat, 1 May 2021 14:17:08 -0400 Subject: [PATCH] WIP ehcache 3 --- pom.xml | 10 ++++- .../caches/ehcache/AbstractEhcacheCache.java | 45 ++++++++++--------- .../caches/ehcache/EhBlockingCache.java | 32 ++++++++----- .../mybatis/caches/ehcache/EhcacheCache.java | 21 +++++++-- .../caches/ehcache/ObjectSerializer.java | 44 ++++++++++++++++++ 5 files changed, 117 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/mybatis/caches/ehcache/ObjectSerializer.java diff --git a/pom.xml b/pom.xml index 09dce25..3ec233d 100644 --- a/pom.xml +++ b/pom.xml @@ -83,10 +83,16 @@ - net.sf.ehcache + org.ehcache ehcache - 2.10.9.2 + 3.10.8 compile + + + * + * + + diff --git a/src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java b/src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java index 462fa7c..cf6906b 100644 --- a/src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java +++ b/src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java @@ -17,23 +17,22 @@ import java.util.concurrent.locks.ReadWriteLock; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.Ehcache; -import net.sf.ehcache.Element; - -import org.apache.ibatis.cache.Cache; +import org.ehcache.Cache; +import org.ehcache.CacheManager; +import org.ehcache.config.builders.CacheManagerBuilder; +import org.ehcache.sizeof.SizeOf; /** * Cache adapter for Ehcache. * * @author Simone Tripodi */ -public abstract class AbstractEhcacheCache implements Cache { +public abstract class AbstractEhcacheCache implements org.apache.ibatis.cache.Cache { /** * The cache manager reference. */ - protected static CacheManager CACHE_MANAGER = CacheManager.create(); + protected static CacheManager CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true); /** * The cache id (namespace). @@ -43,7 +42,13 @@ public abstract class AbstractEhcacheCache implements Cache { /** * The cache instance. */ - protected Ehcache cache; + protected Cache cache; + + protected long timeToIdleSeconds; + protected long timeToLiveSeconds; + protected long maxEntriesLocalHeap = 1; + protected long maxEntriesLocalDisk = 1; + protected String memoryStoreEvictionPolicy; /** * Instantiates a new abstract ehcache cache. @@ -63,7 +68,7 @@ public AbstractEhcacheCache(final String id) { */ @Override public void clear() { - cache.removeAll(); + cache.clear(); } /** @@ -79,11 +84,11 @@ public String getId() { */ @Override public Object getObject(Object key) { - Element cachedElement = cache.get(key); + Object cachedElement = cache.get(key); if (cachedElement == null) { return null; } - return cachedElement.getObjectValue(); + return cachedElement; } /** @@ -91,7 +96,7 @@ public Object getObject(Object key) { */ @Override public int getSize() { - return cache.getSize(); + return (int) SizeOf.newInstance().deepSizeOf(cache); } /** @@ -99,7 +104,7 @@ public int getSize() { */ @Override public void putObject(Object key, Object value) { - cache.put(new Element(key, value)); + cache.put(key, value); } /** @@ -133,8 +138,8 @@ public boolean equals(Object obj) { return false; } - Cache otherCache = (Cache) obj; - return id.equals(otherCache.getId()); + Cache otherCache = (Cache) obj; + return id.equals(otherCache.get(id)); } /** @@ -167,7 +172,7 @@ public String toString() { * the default amount of time to live for an element from its last accessed or modified date */ public void setTimeToIdleSeconds(long timeToIdleSeconds) { - cache.getCacheConfiguration().setTimeToIdleSeconds(timeToIdleSeconds); + this.timeToIdleSeconds = timeToIdleSeconds; } /** @@ -177,7 +182,7 @@ public void setTimeToIdleSeconds(long timeToIdleSeconds) { * the default amount of time to live for an element from its creation date */ public void setTimeToLiveSeconds(long timeToLiveSeconds) { - cache.getCacheConfiguration().setTimeToLiveSeconds(timeToLiveSeconds); + this.timeToLiveSeconds = timeToLiveSeconds; } /** @@ -187,7 +192,7 @@ public void setTimeToLiveSeconds(long timeToLiveSeconds) { * The maximum number of elements in heap, before they are evicted (0 == no limit) */ public void setMaxEntriesLocalHeap(long maxEntriesLocalHeap) { - cache.getCacheConfiguration().setMaxEntriesLocalHeap(maxEntriesLocalHeap); + this.maxEntriesLocalHeap = maxEntriesLocalHeap; } /** @@ -197,7 +202,7 @@ public void setMaxEntriesLocalHeap(long maxEntriesLocalHeap) { * the maximum number of Elements to allow on the disk. 0 means unlimited. */ public void setMaxEntriesLocalDisk(long maxEntriesLocalDisk) { - cache.getCacheConfiguration().setMaxEntriesLocalDisk(maxEntriesLocalDisk); + this.maxEntriesLocalDisk = maxEntriesLocalDisk; } /** @@ -207,7 +212,7 @@ public void setMaxEntriesLocalDisk(long maxEntriesLocalDisk) { * a String representation of the policy. One of "LRU", "LFU" or "FIFO". */ public void setMemoryStoreEvictionPolicy(String memoryStoreEvictionPolicy) { - cache.getCacheConfiguration().setMemoryStoreEvictionPolicy(memoryStoreEvictionPolicy); + this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy; } } diff --git a/src/main/java/org/mybatis/caches/ehcache/EhBlockingCache.java b/src/main/java/org/mybatis/caches/ehcache/EhBlockingCache.java index b050a3b..2ea83d2 100644 --- a/src/main/java/org/mybatis/caches/ehcache/EhBlockingCache.java +++ b/src/main/java/org/mybatis/caches/ehcache/EhBlockingCache.java @@ -15,9 +15,15 @@ */ package org.mybatis.caches.ehcache; -import net.sf.ehcache.Ehcache; -import net.sf.ehcache.Element; -import net.sf.ehcache.constructs.blocking.BlockingCache; +import java.time.Duration; +import java.time.temporal.ChronoUnit; + +import org.ehcache.Cache; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.ExpiryPolicyBuilder; +import org.ehcache.config.builders.ResourcePoolsBuilder; +import org.ehcache.config.units.EntryUnit; +import org.ehcache.config.units.MemoryUnit; /** * The Class EhBlockingCache. @@ -34,20 +40,26 @@ public class EhBlockingCache extends AbstractEhcacheCache { */ public EhBlockingCache(final String id) { super(id); - if (!CACHE_MANAGER.cacheExists(id)) { - CACHE_MANAGER.addCache(this.id); - Ehcache ehcache = CACHE_MANAGER.getEhcache(this.id); - BlockingCache blockingCache = new BlockingCache(ehcache); - CACHE_MANAGER.replaceCacheWithDecoratedCache(ehcache, blockingCache); + if (CACHE_MANAGER.getCache(id, Object.class, Object.class) == null) { + CACHE_MANAGER.createCache(this.id, CacheConfigurationBuilder + .newCacheConfigurationBuilder(Object.class, Object.class, + ResourcePoolsBuilder.newResourcePoolsBuilder().heap(this.maxEntriesLocalHeap, EntryUnit.ENTRIES) + .offheap(this.maxEntriesLocalDisk, MemoryUnit.MB)) + .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.of(this.timeToIdleSeconds, ChronoUnit.SECONDS))) + .withExpiry( + ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(this.timeToLiveSeconds, ChronoUnit.SECONDS)))); + Cache ehcache = CACHE_MANAGER.getCache(this.id, Object.class, Object.class); + // BlockingCache blockingCache = new BlockingCache(ehcache); + // CACHE_MANAGER.replaceCacheWithDecoratedCache(ehcache, blockingCache); } - this.cache = CACHE_MANAGER.getEhcache(id); + this.cache = CACHE_MANAGER.getCache(id, Object.class, Object.class); } @Override public Object removeObject(Object key) { // this method is called during a rollback just to // release any previous lock - cache.put(new Element(key, null)); + cache.put(key, null); return null; } diff --git a/src/main/java/org/mybatis/caches/ehcache/EhcacheCache.java b/src/main/java/org/mybatis/caches/ehcache/EhcacheCache.java index ec2b3a4..00f96fe 100644 --- a/src/main/java/org/mybatis/caches/ehcache/EhcacheCache.java +++ b/src/main/java/org/mybatis/caches/ehcache/EhcacheCache.java @@ -15,6 +15,15 @@ */ package org.mybatis.caches.ehcache; +import java.time.Duration; +import java.time.temporal.ChronoUnit; + +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.ExpiryPolicyBuilder; +import org.ehcache.config.builders.ResourcePoolsBuilder; +import org.ehcache.config.units.EntryUnit; +import org.ehcache.config.units.MemoryUnit; + public class EhcacheCache extends AbstractEhcacheCache { /** @@ -25,10 +34,16 @@ public class EhcacheCache extends AbstractEhcacheCache { */ public EhcacheCache(String id) { super(id); - if (!CACHE_MANAGER.cacheExists(id)) { - CACHE_MANAGER.addCache(id); + if (CACHE_MANAGER.getCache(id, Object.class, Object.class) == null) { + CACHE_MANAGER.createCache(this.id, CacheConfigurationBuilder + .newCacheConfigurationBuilder(Object.class, Object.class, + ResourcePoolsBuilder.newResourcePoolsBuilder().heap(this.maxEntriesLocalHeap, EntryUnit.ENTRIES) + .offheap(this.maxEntriesLocalDisk, MemoryUnit.MB)) + .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.of(this.timeToIdleSeconds, ChronoUnit.SECONDS))) + .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(this.timeToLiveSeconds, ChronoUnit.SECONDS))) + .withKeySerializer(ObjectSerializer.class).withValueSerializer(ObjectSerializer.class)); } - this.cache = CACHE_MANAGER.getEhcache(id); + this.cache = CACHE_MANAGER.getCache(id, Object.class, Object.class); } } diff --git a/src/main/java/org/mybatis/caches/ehcache/ObjectSerializer.java b/src/main/java/org/mybatis/caches/ehcache/ObjectSerializer.java new file mode 100644 index 0000000..0b57827 --- /dev/null +++ b/src/main/java/org/mybatis/caches/ehcache/ObjectSerializer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.caches.ehcache; + +import java.nio.ByteBuffer; + +import org.ehcache.spi.serialization.Serializer; +import org.ehcache.spi.serialization.SerializerException; + +public class ObjectSerializer implements Serializer { + + public ObjectSerializer(ClassLoader loader) { + // no-op + } + + @Override + public boolean equals(Object arg0, ByteBuffer arg1) throws ClassNotFoundException, SerializerException { + return false; + } + + @Override + public Object read(ByteBuffer arg0) throws ClassNotFoundException, SerializerException { + return null; + } + + @Override + public ByteBuffer serialize(Object arg0) throws SerializerException { + return null; + } + +}