Skip to content

Commit

Permalink
WIP ehcache 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Sep 15, 2024
1 parent 424b224 commit d5ab5ac
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 35 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@
</dependency>

<dependency>
<groupId>net.sf.ehcache</groupId>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
<version>3.10.8</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/org/mybatis/caches/ehcache/AbstractEhcacheCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -43,7 +42,13 @@ public abstract class AbstractEhcacheCache implements Cache {
/**
* The cache instance.
*/
protected Ehcache cache;
protected Cache<Object, Object> cache;

protected long timeToIdleSeconds;
protected long timeToLiveSeconds;
protected long maxEntriesLocalHeap = 1;
protected long maxEntriesLocalDisk = 1;
protected String memoryStoreEvictionPolicy;

/**
* Instantiates a new abstract ehcache cache.
Expand All @@ -63,7 +68,7 @@ public AbstractEhcacheCache(final String id) {
*/
@Override
public void clear() {
cache.removeAll();
cache.clear();
}

/**
Expand All @@ -79,27 +84,27 @@ 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;
}

/**
* {@inheritDoc}
*/
@Override
public int getSize() {
return cache.getSize();
return (int) SizeOf.newInstance().deepSizeOf(cache);
}

/**
* {@inheritDoc}
*/
@Override
public void putObject(Object key, Object value) {
cache.put(new Element(key, value));
cache.put(key, value);
}

/**
Expand Down Expand Up @@ -133,8 +138,8 @@ public boolean equals(Object obj) {
return false;
}

Cache otherCache = (Cache) obj;
return id.equals(otherCache.getId());
Cache<Object, Object> otherCache = (Cache<Object, Object>) obj;
return id.equals(otherCache.get(id));
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

}
32 changes: 22 additions & 10 deletions src/main/java/org/mybatis/caches/ehcache/EhBlockingCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Object, Object> 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;
}

Expand Down
21 changes: 18 additions & 3 deletions src/main/java/org/mybatis/caches/ehcache/EhcacheCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -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);
}

}
44 changes: 44 additions & 0 deletions src/main/java/org/mybatis/caches/ehcache/ObjectSerializer.java
Original file line number Diff line number Diff line change
@@ -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<Object> {

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;
}

}

0 comments on commit d5ab5ac

Please sign in to comment.