diff --git a/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java b/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java index 95cf02fda..31153f0c5 100644 --- a/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java +++ b/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java @@ -85,8 +85,10 @@ public class RedissonLocalCachedMap extends RedissonMap implements R storeMode = options.getStoreMode(); storeCacheMiss = options.isStoreCacheMiss(); + localCacheView = new LocalCacheView<>(options, this); + cache = localCacheView.getCache(); listener = new LocalCacheListener(getRawName(), commandExecutor, this, codec, options, cacheUpdateLogTime) { - + @Override protected void updateCache(ByteBuf keyBuf, ByteBuf valueBuf) throws IOException { CacheKey cacheKey = localCacheView.toCacheKey(keyBuf); @@ -94,12 +96,10 @@ public class RedissonLocalCachedMap extends RedissonMap implements R Object value = codec.getMapValueDecoder().decode(valueBuf, null); cachePut(cacheKey, key, value); } - + }; - cache = listener.createCache(options); - instanceId = listener.getInstanceId(); listener.add(cache); - localCacheView = new LocalCacheView(cache, this); + instanceId = listener.getInstanceId(); if (options.getSyncStrategy() != SyncStrategy.NONE) { invalidateEntryOnChange = 1; diff --git a/redisson/src/main/java/org/redisson/cache/LocalCacheView.java b/redisson/src/main/java/org/redisson/cache/LocalCacheView.java index e346d4ea7..fc103f75d 100644 --- a/redisson/src/main/java/org/redisson/cache/LocalCacheView.java +++ b/redisson/src/main/java/org/redisson/cache/LocalCacheView.java @@ -23,8 +23,12 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; +import com.github.benmanes.caffeine.cache.Caffeine; import org.redisson.RedissonObject; +import org.redisson.api.LocalCachedMapOptions; import org.redisson.misc.Hash; import io.netty.buffer.ByteBuf; @@ -39,10 +43,10 @@ import io.netty.buffer.ByteBuf; public class LocalCacheView { private final RedissonObject object; - private final Map cache; + private final ConcurrentMap cache; - public LocalCacheView(Map cache, RedissonObject object) { - this.cache = cache; + public LocalCacheView(LocalCachedMapOptions options, RedissonObject object) { + this.cache = createCache(options); this.object = object; } @@ -255,5 +259,49 @@ public class LocalCacheView { public CacheKey toCacheKey(ByteBuf encodedKey) { return new CacheKey(Hash.hash128toArray(encodedKey)); } - + + public ConcurrentMap getCache() { + return cache; + } + + public ConcurrentMap createCache(LocalCachedMapOptions options) { + if (options.getCacheProvider() == LocalCachedMapOptions.CacheProvider.CAFFEINE) { + Caffeine caffeineBuilder = Caffeine.newBuilder(); + if (options.getTimeToLiveInMillis() > 0) { + caffeineBuilder.expireAfterWrite(options.getTimeToLiveInMillis(), TimeUnit.MILLISECONDS); + } + if (options.getMaxIdleInMillis() > 0) { + caffeineBuilder.expireAfterAccess(options.getMaxIdleInMillis(), TimeUnit.MILLISECONDS); + } + if (options.getCacheSize() > 0) { + caffeineBuilder.maximumSize(options.getCacheSize()); + } + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.SOFT) { + caffeineBuilder.softValues(); + } + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.WEAK) { + caffeineBuilder.weakValues(); + } + return caffeineBuilder.build().asMap(); + } + + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.NONE) { + return new NoneCacheMap<>(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis()); + } + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.LRU) { + return new LRUCacheMap<>(options.getCacheSize(), options.getTimeToLiveInMillis(), options.getMaxIdleInMillis()); + } + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.LFU) { + return new LFUCacheMap<>(options.getCacheSize(), options.getTimeToLiveInMillis(), options.getMaxIdleInMillis()); + } + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.SOFT) { + return ReferenceCacheMap.soft(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis()); + } + if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.WEAK) { + return ReferenceCacheMap.weak(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis()); + } + throw new IllegalArgumentException("Invalid eviction policy: " + options.getEvictionPolicy()); + } + + }