From cc15f8f5a507a5267024e1e776f0737f501da537 Mon Sep 17 00:00:00 2001 From: Parker Date: Tue, 24 Nov 2020 10:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Ehcache=20=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E4=BC=9A=E5=AF=BC=E8=87=B4Jvm=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=85=B1=E4=BA=AB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../opsli/plugins/cache/EhCachePlugin.java | 8 --- .../opsli/plugins/cache/msg/EhCacheMsg.java | 7 +-- .../cache/service/EhCachePluginImpl.java | 52 ++++++++++--------- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java index 30899e4..0cfb4b1 100644 --- a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java +++ b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/EhCachePlugin.java @@ -33,14 +33,6 @@ public interface EhCachePlugin { */ boolean put(String cacheName, String key, Object value); - /** - * 获取缓存数据 - * @param cacheName - * @param key - * @return - */ - Object get(String cacheName, String key); - /** * 获取缓存数据 * @param cacheName diff --git a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/msg/EhCacheMsg.java b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/msg/EhCacheMsg.java index 0735d50..e1e7641 100644 --- a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/msg/EhCacheMsg.java +++ b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/msg/EhCacheMsg.java @@ -28,9 +28,10 @@ public enum EhCacheMsg implements BaseMsg { /** 缓存未开启 */ EXCEPTION_ENABLE(90001,"本地缓存未开启!"), - EXCEPTION_PUT(90001,"添加缓存失败"), - EXCEPTION_GET(90001,"获取缓存数据失败"), - EXCEPTION_DEL(90001,"删除缓存数据失败"), + EXCEPTION_PUT(90002,"添加缓存失败"), + EXCEPTION_GET(90003,"获取缓存数据失败"), + EXCEPTION_GET_JAVA(90004,"获取缓存数据失败, 转化Java类型失败, 失败类型[{}]"), + EXCEPTION_DEL(90005,"删除缓存数据失败"), ; diff --git a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java index d20f274..f87217e 100644 --- a/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java +++ b/opsli-plugins/opsli-plugins-ehcache/src/main/java/org/opsli/plugins/cache/service/EhCachePluginImpl.java @@ -15,13 +15,13 @@ */ 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 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.msg.EhCacheMsg; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.stereotype.Service; @@ -37,6 +37,9 @@ import org.springframework.stereotype.Service; @Service public class EhCachePluginImpl implements EhCachePlugin { + /** Ehcache Json Key */ + private static final String EHCACHE_JSON_KEY = "ehcache_tmp_json"; + @Autowired(required = false) CacheManager cacheManager; @@ -49,7 +52,11 @@ public class EhCachePluginImpl implements EhCachePlugin { try { Cache cache = cacheManager.getCache(cacheName); 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; } } catch (Exception e) { @@ -58,23 +65,6 @@ public class EhCachePluginImpl implements EhCachePlugin { 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 public V get(String cacheName, String key, Class vClass) { if(cacheManager == null){ @@ -83,9 +73,21 @@ public class EhCachePluginImpl implements EhCachePlugin { try { Cache cache = cacheManager.getCache(cacheName); if(cache != null){ - // 深克隆数据 防止 ehcache在jvm数据串行 - Object obj = ObjectUtil.cloneByStream(cache.get(key,vClass)); - return WrapperUtil.transformInstance(obj, vClass); + V v = null; + String jsonStr = cache.get(key, String.class); + 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) { log.error(EhCacheMsg.EXCEPTION_GET.getMessage()+":{}", e.getMessage());