修复 Ehcache 可能会导致Jvm缓存对象共享问题

v1.4.1
Parker 4 years ago
parent bd5854cb9a
commit cc15f8f5a5

@ -33,14 +33,6 @@ public interface EhCachePlugin {
*/ */
boolean put(String cacheName, String key, Object value); boolean put(String cacheName, String key, Object value);
/**
*
* @param cacheName
* @param key
* @return
*/
Object get(String cacheName, String key);
/** /**
* *
* @param cacheName * @param cacheName

@ -28,9 +28,10 @@ public enum EhCacheMsg implements BaseMsg {
/** 缓存未开启 */ /** 缓存未开启 */
EXCEPTION_ENABLE(90001,"本地缓存未开启!"), EXCEPTION_ENABLE(90001,"本地缓存未开启!"),
EXCEPTION_PUT(90001,"添加缓存失败"), EXCEPTION_PUT(90002,"添加缓存失败"),
EXCEPTION_GET(90001,"获取缓存数据失败"), EXCEPTION_GET(90003,"获取缓存数据失败"),
EXCEPTION_DEL(90001,"删除缓存数据失败"), EXCEPTION_GET_JAVA(90004,"获取缓存数据失败, 转化Java类型失败, 失败类型[{}]"),
EXCEPTION_DEL(90005,"删除缓存数据失败"),
; ;

@ -15,13 +15,13 @@
*/ */
package org.opsli.plugins.cache.service; package org.opsli.plugins.cache.service;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.plugins.cache.msg.EhCacheMsg;
import org.springframework.cache.Cache;
import org.opsli.plugins.cache.EhCachePlugin; import org.opsli.plugins.cache.EhCachePlugin;
import org.opsli.plugins.cache.msg.EhCacheMsg;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -37,6 +37,9 @@ import org.springframework.stereotype.Service;
@Service @Service
public class EhCachePluginImpl implements EhCachePlugin { public class EhCachePluginImpl implements EhCachePlugin {
/** Ehcache Json Key */
private static final String EHCACHE_JSON_KEY = "ehcache_tmp_json";
@Autowired(required = false) @Autowired(required = false)
CacheManager cacheManager; CacheManager cacheManager;
@ -49,7 +52,11 @@ public class EhCachePluginImpl implements EhCachePlugin {
try { try {
Cache cache = cacheManager.getCache(cacheName); Cache cache = cacheManager.getCache(cacheName);
if(cache != null){ if(cache != null){
cache.put(key,value); // 强制转化为 String 字符串 用来解决EhCache jvm共用对象问题
// 则统一转换为 JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put(EHCACHE_JSON_KEY, value);
cache.put(key,jsonObject.toJSONString());
ret = true; ret = true;
} }
} catch (Exception e) { } catch (Exception e) {
@ -58,23 +65,6 @@ public class EhCachePluginImpl implements EhCachePlugin {
return ret; return ret;
} }
@Override
public Object get(String cacheName, String key) {
if(cacheManager == null){
return null;
}
try {
Cache cache = cacheManager.getCache(cacheName);
if(cache != null){
// 深克隆数据 防止 ehcache在jvm数据串行
return ObjectUtil.cloneByStream(cache.get(key));
}
} catch (Exception e) {
log.error(EhCacheMsg.EXCEPTION_GET.getMessage()+"{}",e.getMessage());
}
return null;
}
@Override @Override
public <V> V get(String cacheName, String key, Class<V> vClass) { public <V> V get(String cacheName, String key, Class<V> vClass) {
if(cacheManager == null){ if(cacheManager == null){
@ -83,9 +73,21 @@ public class EhCachePluginImpl implements EhCachePlugin {
try { try {
Cache cache = cacheManager.getCache(cacheName); Cache cache = cacheManager.getCache(cacheName);
if(cache != null){ if(cache != null){
// 深克隆数据 防止 ehcache在jvm数据串行 V v = null;
Object obj = ObjectUtil.cloneByStream(cache.get(key,vClass)); String jsonStr = cache.get(key, String.class);
return WrapperUtil.transformInstance(obj, vClass); JSONObject jsonObject = JSONObject.parseObject(jsonStr);
if(jsonObject != null){
JSONObject dataJson = jsonObject.getJSONObject(EHCACHE_JSON_KEY);
if(dataJson != null){
try {
v = dataJson.toJavaObject(vClass);
}catch (Exception e){
String message = EhCacheMsg.EXCEPTION_GET_JAVA.getMessage();
log.error(StrUtil.format(message, vClass.getName())+"{}", e.getMessage());
}
}
}
return v;
} }
} catch (Exception e) { } catch (Exception e) {
log.error(EhCacheMsg.EXCEPTION_GET.getMessage()+"{}", e.getMessage()); log.error(EhCacheMsg.EXCEPTION_GET.getMessage()+"{}", e.getMessage());

Loading…
Cancel
Save