From 0856a3adcb414c503cc8338fe6aa93d2ecc80685 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Fri, 15 Jul 2016 09:03:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=BC=93=E5=AD=98CacheUtils?= =?UTF-8?q?=E4=BD=BF=E7=94=A8shiro=E7=BC=93=E5=AD=98=EF=BC=8C=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=BC=93=E5=AD=98=E6=94=AF=E6=8C=81redis=EF=BC=8C?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=88=86=E5=B8=83=E5=BC=8F=E7=9A=84=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E3=80=82=E7=94=B1=E4=BA=8E=E6=98=AF=E7=A7=BB=E6=A4=8D?= =?UTF-8?q?=E8=BF=87=E6=9D=A5=E7=9A=84=E5=8A=9F=E8=83=BD=E8=BF=98=E5=BE=85?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=8B=A5=E6=9C=89?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=8F=AF=E9=80=9A=E8=BF=87qq=E7=BE=A4?= =?UTF-8?q?=E6=89=BE=E5=88=B0=E6=88=91=E6=88=96=E5=8F=91=E9=82=AE=E4=BB=B6?= =?UTF-8?q?thinkgem@163.com?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/filter/PageCachingFilter.java | 9 +- .../shiro/cache/JedisCacheManager.java | 10 ++- .../jeesite/common/utils/CacheUtils.java | 88 ++++++++++++++----- .../jeesite/common/utils/JedisUtils.java | 18 ++++ src/main/resources/cache/ehcache-local.xml | 32 +++---- src/main/resources/cache/ehcache-rmi.xml | 56 ++++++------ src/main/resources/spring-context-jedis.xml | 4 + src/main/resources/spring-context-shiro.xml | 6 +- 8 files changed, 154 insertions(+), 69 deletions(-) diff --git a/src/main/java/com/thinkgem/jeesite/common/filter/PageCachingFilter.java b/src/main/java/com/thinkgem/jeesite/common/filter/PageCachingFilter.java index ebe5d4c453..37d641a41f 100644 --- a/src/main/java/com/thinkgem/jeesite/common/filter/PageCachingFilter.java +++ b/src/main/java/com/thinkgem/jeesite/common/filter/PageCachingFilter.java @@ -3,11 +3,11 @@ */ package com.thinkgem.jeesite.common.filter; -import com.thinkgem.jeesite.common.utils.CacheUtils; - import net.sf.ehcache.CacheManager; import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter; +import com.thinkgem.jeesite.common.utils.SpringContextHolder; + /** * 页面高速缓存过滤器 * @author ThinkGem @@ -15,9 +15,12 @@ */ public class PageCachingFilter extends SimplePageCachingFilter { + private CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class); + @Override protected CacheManager getCacheManager() { - return CacheUtils.getCacheManager(); + this.cacheName = "pageCachingFilter"; + return cacheManager; } } diff --git a/src/main/java/com/thinkgem/jeesite/common/security/shiro/cache/JedisCacheManager.java b/src/main/java/com/thinkgem/jeesite/common/security/shiro/cache/JedisCacheManager.java index 030b3c67a3..93ba06ca69 100644 --- a/src/main/java/com/thinkgem/jeesite/common/security/shiro/cache/JedisCacheManager.java +++ b/src/main/java/com/thinkgem/jeesite/common/security/shiro/cache/JedisCacheManager.java @@ -175,7 +175,10 @@ public Set keys() { jedis = JedisUtils.getResource(); Set set = jedis.hkeys(JedisUtils.getBytesKey(cacheKeyName)); for(byte[] key : set){ - keys.add((K)key); + Object obj = (K)JedisUtils.getObjectKey(key); + if (obj != null){ + keys.add((K) obj); + } } logger.debug("keys {} {} ", cacheKeyName, keys); return keys; @@ -196,7 +199,10 @@ public Collection values() { jedis = JedisUtils.getResource(); Collection col = jedis.hvals(JedisUtils.getBytesKey(cacheKeyName)); for(byte[] val : col){ - vals.add((V)val); + Object obj = JedisUtils.toObject(val); + if (obj != null){ + vals.add((V) obj); + } } logger.debug("values {} {} ", cacheKeyName, vals); return vals; diff --git a/src/main/java/com/thinkgem/jeesite/common/utils/CacheUtils.java b/src/main/java/com/thinkgem/jeesite/common/utils/CacheUtils.java index 4498d2105b..cb9c1d6cd9 100644 --- a/src/main/java/com/thinkgem/jeesite/common/utils/CacheUtils.java +++ b/src/main/java/com/thinkgem/jeesite/common/utils/CacheUtils.java @@ -3,9 +3,13 @@ */ package com.thinkgem.jeesite.common.utils; -import net.sf.ehcache.Cache; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.Element; +import java.util.Iterator; +import java.util.Set; + +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Cache工具类 @@ -14,8 +18,9 @@ */ public class CacheUtils { - private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager")); - + private static Logger logger = LoggerFactory.getLogger(CacheUtils.class); + private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class); + private static final String SYS_CACHE = "sysCache"; /** @@ -27,6 +32,17 @@ public static Object get(String key) { return get(SYS_CACHE, key); } + /** + * 获取SYS_CACHE缓存 + * @param key + * @param defaultValue + * @return + */ + public static Object get(String key, Object defaultValue) { + Object value = get(key); + return value != null ? value : defaultValue; + } + /** * 写入SYS_CACHE缓存 * @param key @@ -52,10 +68,21 @@ public static void remove(String key) { * @return */ public static Object get(String cacheName, String key) { - Element element = getCache(cacheName).get(key); - return element==null?null:element.getObjectValue(); + return getCache(cacheName).get(getKey(key)); } - + + /** + * 获取缓存 + * @param cacheName + * @param key + * @param defaultValue + * @return + */ + public static Object get(String cacheName, String key, Object defaultValue) { + Object value = get(cacheName, getKey(key)); + return value != null ? value : defaultValue; + } + /** * 写入缓存 * @param cacheName @@ -63,8 +90,7 @@ public static Object get(String cacheName, String key) { * @param value */ public static void put(String cacheName, String key, Object value) { - Element element = new Element(key, value); - getCache(cacheName).put(element); + getCache(cacheName).put(getKey(key), value); } /** @@ -73,26 +99,46 @@ public static void put(String cacheName, String key, Object value) { * @param key */ public static void remove(String cacheName, String key) { - getCache(cacheName).remove(key); + getCache(cacheName).remove(getKey(key)); + } + + /** + * 从缓存中移除所有 + * @param cacheName + */ + public static void removeAll(String cacheName) { + Cache cache = getCache(cacheName); + Set keys = cache.keys(); + for (Iterator it = keys.iterator(); it.hasNext();){ + cache.remove(it.next()); + } + logger.info("清理缓存: {} => {}", cacheName, keys); } /** - * 获得一个Cache,没有则创建一个。 + * 获取缓存键名,多数据源下增加数据源名称前缀 + * @param key + * @return + */ + private static String getKey(String key){ +// String dsName = DataSourceHolder.getDataSourceName(); +// if (StringUtils.isNotBlank(dsName)){ +// return dsName + "_" + key; +// } + return key; + } + + /** + * 获得一个Cache,没有则显示日志。 * @param cacheName * @return */ - private static Cache getCache(String cacheName){ - Cache cache = cacheManager.getCache(cacheName); + private static Cache getCache(String cacheName){ + Cache cache = cacheManager.getCache(cacheName); if (cache == null){ - cacheManager.addCache(cacheName); - cache = cacheManager.getCache(cacheName); - cache.getCacheConfiguration().setEternal(true); + throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。"); } return cache; } - public static CacheManager getCacheManager() { - return cacheManager; - } - } diff --git a/src/main/java/com/thinkgem/jeesite/common/utils/JedisUtils.java b/src/main/java/com/thinkgem/jeesite/common/utils/JedisUtils.java index edb63b904e..a868a27cfc 100644 --- a/src/main/java/com/thinkgem/jeesite/common/utils/JedisUtils.java +++ b/src/main/java/com/thinkgem/jeesite/common/utils/JedisUtils.java @@ -815,6 +815,24 @@ public static byte[] getBytesKey(Object object){ } } + /** + * 获取byte[]类型Key + * @param key + * @return + */ + public static Object getObjectKey(byte[] key){ + try{ + return StringUtils.toString(key); + }catch(UnsupportedOperationException uoe){ + try{ + return JedisUtils.toObject(key); + }catch(UnsupportedOperationException uoe2){ + uoe2.printStackTrace(); + } + } + return null; + } + /** * Object转换byte[]类型 * @param key diff --git a/src/main/resources/cache/ehcache-local.xml b/src/main/resources/cache/ehcache-local.xml index 95d8ebf6fc..b0ff21f18f 100644 --- a/src/main/resources/cache/ehcache-local.xml +++ b/src/main/resources/cache/ehcache-local.xml @@ -3,29 +3,31 @@ - - + + - + - + + + + + + + - + - + + - - - + \ No newline at end of file diff --git a/src/main/resources/cache/ehcache-rmi.xml b/src/main/resources/cache/ehcache-rmi.xml index ac177ba550..d247288888 100644 --- a/src/main/resources/cache/ehcache-rmi.xml +++ b/src/main/resources/cache/ehcache-rmi.xml @@ -13,47 +13,51 @@ - + - + - + - - - - + + + + - + + + + + + + + + + + - - - - - + + + - - - + - --> - + + \ No newline at end of file diff --git a/src/main/resources/spring-context-jedis.xml b/src/main/resources/spring-context-jedis.xml index a66d05eaac..c8c1f82458 100644 --- a/src/main/resources/spring-context-jedis.xml +++ b/src/main/resources/spring-context-jedis.xml @@ -20,6 +20,10 @@ + \ No newline at end of file diff --git a/src/main/resources/spring-context-shiro.xml b/src/main/resources/spring-context-shiro.xml index 3a01e78ae9..1b9b688bc4 100644 --- a/src/main/resources/spring-context-shiro.xml +++ b/src/main/resources/spring-context-shiro.xml @@ -88,8 +88,10 @@ - - + + + +