Skip to content

Commit

Permalink
INTERNAL: Upgrade ehcache version
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesecrust authored and jhpark816 committed Sep 25, 2024
1 parent f000fd3 commit 88ffa04
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 62 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<zookeeper.version>3.5.9</zookeeper.version>
<log4j.version>2.23.1</log4j.version>
<slf4j.version>2.0.12</slf4j.version>
<ehcache.version>2.6.0</ehcache.version>
<ehcache.version>3.10.8</ehcache.version>
<junit.version>5.10.2</junit.version>
<jmock.version>2.13.1</jmock.version>
</properties>
Expand Down Expand Up @@ -79,10 +79,16 @@
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
<exclusions>
<!--> org.glassfish.jaxb is blocked in maven 3.8.1 as it comes from http (not https) repository.
Also, it is not required in java 8 so we excluded it. See https://github.com/ehcache/ehcache3/issues/2881. </-->
<exclusion>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ public class ConnectionFactoryBuilder {
private int maxFrontCacheElements = DefaultConnectionFactory.DEFAULT_MAX_FRONTCACHE_ELEMENTS;
private int frontCacheExpireTime = DefaultConnectionFactory.DEFAULT_FRONTCACHE_EXPIRETIME;
private String frontCacheName = "ArcusFrontCache_" + this.hashCode();
private boolean frontCacheCopyOnRead = DefaultConnectionFactory.DEFAULT_FRONT_CACHE_COPY_ON_READ;
private boolean frontCacheCopyOnWrite =
DefaultConnectionFactory.DEFAULT_FRONT_CACHE_COPY_ON_WRITE;

private int maxSMGetChunkSize = DefaultConnectionFactory.DEFAULT_MAX_SMGET_KEY_CHUNK_SIZE;
private byte delimiter = DefaultConnectionFactory.DEFAULT_DELIMITER;
Expand Down Expand Up @@ -347,16 +344,16 @@ public ConnectionFactoryBuilder setFrontCacheExpireTime(int to) {
/**
* Set front cache copyOnRead property
*/
@Deprecated
public ConnectionFactoryBuilder setFrontCacheCopyOnRead(boolean copyOnRead) {
frontCacheCopyOnRead = copyOnRead;
return this;
}

/**
* Set front cache copyOnWrite property
*/
@Deprecated
public ConnectionFactoryBuilder setFrontCacheCopyOnWrite(boolean copyOnWrite) {
frontCacheCopyOnWrite = copyOnWrite;
return this;
}

Expand Down Expand Up @@ -616,13 +613,17 @@ public String getFrontCacheName() {
}

@Override
@SuppressWarnings("deprecation")
@Deprecated
public boolean getFrontCacheCopyOnRead() {
return frontCacheCopyOnRead;
return DefaultConnectionFactory.DEFAULT_FRONT_CACHE_COPY_ON_READ;
}

@Override
@SuppressWarnings("deprecation")
@Deprecated
public boolean getFrontCacheCopyOnWrite() {
return frontCacheCopyOnWrite;
return DefaultConnectionFactory.DEFAULT_FRONT_CACHE_COPY_ON_WRITE;
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,17 @@ public class DefaultConnectionFactory extends SpyObject
"ArcusFrontCache" + new Object().hashCode();

/**
* copyOnRead is no longer used
* Default copyOnRead : false
*/
@Deprecated
public static final boolean DEFAULT_FRONT_CACHE_COPY_ON_READ = false;

/**
* copyOnWrite is no longer used
* Default copyOnWrite : false
*/
@Deprecated
public static final boolean DEFAULT_FRONT_CACHE_COPY_ON_WRITE = false;

/**
Expand Down Expand Up @@ -347,11 +351,13 @@ public String getFrontCacheName() {
}

@Override
@Deprecated
public boolean getFrontCacheCopyOnRead() {
return DEFAULT_FRONT_CACHE_COPY_ON_READ;
}

@Override
@Deprecated
public boolean getFrontCacheCopyOnWrite() {
return DEFAULT_FRONT_CACHE_COPY_ON_WRITE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ public FrontCacheMemcachedClient(ConnectionFactory cf,
String cacheName = cf.getFrontCacheName();
int maxElements = cf.getMaxFrontCacheElements();
int timeToLiveSeconds = cf.getFrontCacheExpireTime();
boolean copyOnRead = cf.getFrontCacheCopyOnRead();
boolean copyOnWrite = cf.getFrontCacheCopyOnWrite();
// TODO add an additional option
// int timeToIdleSeconds = timeToLiveSeconds;

localCacheManager = new LocalCacheManager(cacheName, maxElements,
timeToLiveSeconds, copyOnRead, copyOnWrite);
localCacheManager = new LocalCacheManager(cacheName, maxElements, timeToLiveSeconds);
}
}

Expand Down
72 changes: 26 additions & 46 deletions src/main/java/net/spy/memcached/plugin/LocalCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package net.spy.memcached.plugin;

import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand All @@ -24,52 +25,39 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;

import net.spy.memcached.compat.log.Logger;
import net.spy.memcached.compat.log.LoggerFactory;

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;

/**
* Local cache storage based on ehcache.
*/
public class LocalCacheManager {

private Logger logger = LoggerFactory.getLogger(getClass());

protected Cache cache;
private Cache<String, Object> cache;
protected String name;

public LocalCacheManager(String name) {
public LocalCacheManager(String name, int max, int exptime) {
this.name = name;
// create a undecorated Cache object.
this.cache = CacheManager.getInstance().getCache(name);
}

public LocalCacheManager(String name, int max, int exptime, boolean copyOnRead,
boolean copyOnWrite) {
this.cache = CacheManager.getInstance().getCache(name);
if (cache == null) {
CacheConfiguration config =
new CacheConfiguration(name, max)
.copyOnRead(copyOnRead)
.copyOnWrite(copyOnWrite)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
.eternal(false)
.timeToLiveSeconds(exptime)
.timeToIdleSeconds(exptime)
.diskExpiryThreadIntervalSeconds(60)
.persistence(new PersistenceConfiguration().strategy(
PersistenceConfiguration.Strategy.NONE));
this.cache = new Cache(config, null, null);
CacheManager.getInstance().addCache(cache);

logger.info("Arcus k/v local cache is enabled : %s", cache.toString());
}
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.build(true);
CacheConfiguration<String, Object> config =
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Object.class,
ResourcePoolsBuilder.heap(max))
.withExpiry(ExpiryPolicyBuilder
.timeToLiveExpiration(Duration.ofSeconds(exptime)))
.build();
this.cache = cacheManager.createCache(name, config);

logger.info("Arcus k/v local cache is enabled : %s", cache.toString());
}

public <T> T get(String key) {
Expand All @@ -78,10 +66,10 @@ public <T> T get(String key) {
}

try {
Element element = cache.get(key);
if (null != element) {
Object value = cache.get(key);
if (null != value) {
logger.debug("ArcusFrontCache: local cache hit for %s", key);
@SuppressWarnings("unchecked") T ret = (T) element.getObjectValue();
@SuppressWarnings("unchecked") T ret = (T) value;
return ret;
}
} catch (Exception e) {
Expand All @@ -101,21 +89,13 @@ public T call() throws Exception {
return task;
}

public Element getElement(String key) {
Element element = cache.get(key);
if (null != element) {
logger.debug("ArcusFrontCache: local cache hit for %s", key);
}
return element;
}

public <T> boolean put(String k, T v) {
if (v == null) {
return false;
}

try {
cache.put(new Element(k, v));
cache.put(k, v);
return true;
} catch (Exception e) {
logger.info("failed to put to the local cache : %s", e.getMessage());
Expand Down

0 comments on commit 88ffa04

Please sign in to comment.