fix 修复 caffeine共享 key冲突导致的数据问题

2.X
疯狂的狮子Li 1 year ago
parent ef962f1160
commit 1325972141

@ -32,17 +32,21 @@ public class CaffeineCacheDecorator implements Cache {
return cache.getNativeCache(); return cache.getNativeCache();
} }
public String getUniqueKey(Object key) {
return cache.getName() + ":" + key;
}
@Override @Override
public ValueWrapper get(Object key) { public ValueWrapper get(Object key) {
Object o = CAFFEINE.get(key, k -> cache.get(key)); Object o = CAFFEINE.get(getUniqueKey(key), k -> cache.get(key));
Console.log("redisson caffeine -> key: " + key + ",value:" + o); Console.log("redisson caffeine -> key: " + getUniqueKey(key) + ",value:" + o);
return (ValueWrapper) o; return (ValueWrapper) o;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(Object key, Class<T> type) { public <T> T get(Object key, Class<T> type) {
Object o = CAFFEINE.get(key, k -> cache.get(key, type)); Object o = CAFFEINE.get(getUniqueKey(key), k -> cache.get(key, type));
Console.log("redisson caffeine -> key: " + key + ",value:" + o); Console.log("redisson caffeine -> key: " + getUniqueKey(key) + ",value:" + o);
return (T) o; return (T) o;
} }
@ -63,7 +67,7 @@ public class CaffeineCacheDecorator implements Cache {
public boolean evictIfPresent(Object key) { public boolean evictIfPresent(Object key) {
boolean b = cache.evictIfPresent(key); boolean b = cache.evictIfPresent(key);
if (b) { if (b) {
CAFFEINE.invalidate(key); CAFFEINE.invalidate(getUniqueKey(key));
} }
return b; return b;
} }
@ -80,8 +84,8 @@ public class CaffeineCacheDecorator implements Cache {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T get(Object key, Callable<T> valueLoader) { public <T> T get(Object key, Callable<T> valueLoader) {
Object o = CAFFEINE.get(key, k -> cache.get(key, valueLoader)); Object o = CAFFEINE.get(getUniqueKey(key), k -> cache.get(key, valueLoader));
Console.log("redisson caffeine -> key: " + key + ",value:" + o); Console.log("redisson caffeine -> key: " + getUniqueKey(key) + ",value:" + o);
return (T) o; return (T) o;
} }

Loading…
Cancel
Save