diff options
| author | 段冬梅 <[email protected]> | 2018-12-15 20:51:29 +0800 |
|---|---|---|
| committer | 段冬梅 <[email protected]> | 2018-12-15 20:51:29 +0800 |
| commit | 4f65628dc01f32f35e6e17912fe2c0f032ab303b (patch) | |
| tree | 7f4e632b2aafc558b8385046bb01f7190d45d45a /src/main/java | |
| parent | 5087df3a7b6e808d941cbcbd50af24d3890e95d2 (diff) | |
缓存变更为使用shiroRedis缓存
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/com/nis/util/AsnCacheUtils.java | 60 | ||||
| -rw-r--r-- | src/main/java/com/nis/util/CacheUtils.java | 198 |
2 files changed, 129 insertions, 129 deletions
diff --git a/src/main/java/com/nis/util/AsnCacheUtils.java b/src/main/java/com/nis/util/AsnCacheUtils.java index 329aaf0..2cd40ad 100644 --- a/src/main/java/com/nis/util/AsnCacheUtils.java +++ b/src/main/java/com/nis/util/AsnCacheUtils.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.Map.Entry; import org.apache.log4j.Logger; +import org.apache.shiro.cache.Cache; import com.beust.jcommander.internal.Lists; import com.nis.domain.specific.ConfigGroupInfo; @@ -12,8 +13,6 @@ import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.service.SpringContextHolder; import jersey.repackaged.com.google.common.collect.Maps; -import net.sf.ehcache.Cache; -import net.sf.ehcache.Element; /** * asn no缓存工具类 @@ -35,9 +34,12 @@ public class AsnCacheUtils{ * @return */ public static ConfigGroupInfo get(Long key) { - Element element = getCache(ASN_NO_CACHE).get(key/cache_rage); + Cache cache = getCache(ASN_NO_CACHE); + Object element = cache.get(key/cache_rage); + +// Element element = getCache(ASN_NO_CACHE).get(key/cache_rage); if(element!=null) { - Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue(); + Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element; if(map.containsKey(key)) { return map.get(key); } @@ -45,20 +47,19 @@ public class AsnCacheUtils{ return null; } public static Map<Long,ConfigGroupInfo> getMap(Object key) { - Element element = getCache(ASN_NO_CACHE).get(key); - return (Map<Long,ConfigGroupInfo>)element.getObjectValue(); + Object element = getCache(ASN_NO_CACHE).get(key); + return (Map<Long,ConfigGroupInfo>)element; } public static void clearCache() { logger.warn("clear cache!"); - CacheUtils.getCacheManager().removeCache(ASN_NO_CACHE); + getCache(ASN_NO_CACHE).clear(); } public static List<ConfigGroupInfo> getAllAsnGroup(){ List<ConfigGroupInfo> configGroupInfos=Lists.newArrayList(); Cache cache=getCache(ASN_NO_CACHE); - for(Object key:cache.getKeys()) { - Element element = getCache(ASN_NO_CACHE).get(key); - if(element!=null) { - Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue(); + for(Object val : cache.values()) { + if(val!=null) { + Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)val; configGroupInfos.addAll(map.values()); } } @@ -89,19 +90,21 @@ public class AsnCacheUtils{ } } for(Entry<Long, Map<Long, ConfigGroupInfo>> e:groupMap.entrySet()) { - Element element = new Element(e.getKey(), e.getValue()); - cache.put(element); + cache.put(e.getKey(),e.getValue()); } }else { //查询总量 Long count=configGroupInfoDao.getCountByType(4); boolean loadDatabase=false; - if(cache.getKeys().size()==0) { + if(cache.keys().size()==0) { loadDatabase=true; }else { long c=0l; - for(Object key:cache.getKeys()) { - c+=getMap(key).size(); + for(Object key:cache.keys()) { + Map<Long, ConfigGroupInfo> map = getMap(key); + if(map != null) { + c+=getMap(key).size(); + } } if(c!=count) { loadDatabase=true; @@ -121,8 +124,7 @@ public class AsnCacheUtils{ } } for(Entry<Long, Map<Long, ConfigGroupInfo>> e:groupMap.entrySet()) { - Element element = new Element(e.getKey(), e.getValue()); - cache.put(element); + cache.put(e.getKey(), e.getValue()); } } } @@ -137,17 +139,16 @@ public class AsnCacheUtils{ */ public static void put(Long key, ConfigGroupInfo value) { Long _key=key/cache_rage; - Element element = getCache(ASN_NO_CACHE).get(_key); + Object element = getCache(ASN_NO_CACHE).get(_key); if(element==null) { Map<Long,ConfigGroupInfo> map=Maps.newHashMap(); map.put(key, value); - element = new Element(_key, map); + getCache(ASN_NO_CACHE).put(_key,map); }else { - Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue(); + Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element; map.put(key, value); - element = new Element(_key, map); + getCache(ASN_NO_CACHE).put(_key,map); } - getCache(ASN_NO_CACHE).put(element); } /** * 从缓存中移除 @@ -159,30 +160,25 @@ public class AsnCacheUtils{ } public static void remove(Long key) { Long _key=key/cache_rage; - Element element = getCache(ASN_NO_CACHE).get(_key); + Object element = getCache(ASN_NO_CACHE).get(_key); if(element!=null) { - Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue(); + Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element; if(map.containsKey(key)) { map.remove(key); } if(map.isEmpty()) { getCache(ASN_NO_CACHE).remove(_key); }else { - element=new Element(_key,map); - getCache(ASN_NO_CACHE).put(element); + getCache(ASN_NO_CACHE).put(_key, map); } } } private static Cache getCache(String cacheName){ Cache cache = CacheUtils.getCacheManager().getCache(cacheName); - if (cache == null){ - CacheUtils.getCacheManager().addCache(cacheName); - cache = CacheUtils.getCacheManager().getCache(cacheName); - cache.getCacheConfiguration().setEternal(true); - } return cache; } + public static String getCacheName() { return ASN_NO_CACHE; } diff --git a/src/main/java/com/nis/util/CacheUtils.java b/src/main/java/com/nis/util/CacheUtils.java index ca8642d..98d27f2 100644 --- a/src/main/java/com/nis/util/CacheUtils.java +++ b/src/main/java/com/nis/util/CacheUtils.java @@ -1,97 +1,101 @@ -package com.nis.util; - -import com.nis.web.service.SpringContextHolder; - -import net.sf.ehcache.Cache; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.Element; - -/** - * Cache工具类 - * @author darnell - * - */ -public class CacheUtils { - - private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager")); - - private static final String SYS_CACHE = "sysCache"; - - /** - * 获取SYS_CACHE缓存 - * @param key - * @return - */ - public static Object get(String key) { - return get(SYS_CACHE, key); - } - - /** - * 写入SYS_CACHE缓存 - * @param key - * @return - */ - public static void put(String key, Object value) { - put(SYS_CACHE, key, value); - } - - /** - * 从SYS_CACHE缓存中移除 - * @param key - * @return - */ - public static void remove(String key) { - remove(SYS_CACHE, key); - } - - /** - * 获取缓存 - * @param cacheName - * @param key - * @return - */ - public static Object get(String cacheName, String key) { - Element element = getCache(cacheName).get(key); - return element==null?null:element.getObjectValue(); - } - - /** - * 写入缓存 - * @param cacheName - * @param key - * @param value - */ - public static void put(String cacheName, String key, Object value) { - Element element = new Element(key, value); - getCache(cacheName).put(element); - } - - /** - * 从缓存中移除 - * @param cacheName - * @param key - */ - public static void remove(String cacheName, String key) { - getCache(cacheName).remove(key); - } - - /** - * 获得一个Cache,没有则创建一个。 - * @param cacheName - * @return - */ - 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); - } - return cache; - } - - public static CacheManager getCacheManager() { - return cacheManager; - } - -} +package com.nis.util;
+
+
+import org.apache.shiro.cache.Cache;
+import org.crazycake.shiro.RedisCacheManager;
+
+import com.nis.web.service.SpringContextHolder;
+
+
+/**
+ * Cache工具类
+ * @author darnell
+ *
+ */
+public class CacheUtils {
+
+ private static RedisCacheManager cacheManager = (RedisCacheManager)SpringContextHolder.getBean("shiroCacheManager");
+
+ private static final String SYS_CACHE = "sysCache";
+
+ /**
+ * 获取SYS_CACHE缓存
+ * @param key
+ * @return
+ */
+ public static Object get(String key) {
+ return get(SYS_CACHE, key);
+ }
+
+ /**
+ * 写入SYS_CACHE缓存
+ * @param key
+ * @return
+ */
+ public static void put(String key, Object value) {
+ put(SYS_CACHE, key, value);
+ }
+
+ /**
+ * 从SYS_CACHE缓存中移除
+ * @param key
+ * @return
+ */
+ public static void remove(String key) {
+ remove(SYS_CACHE, key);
+ }
+
+ /**
+ * 获取缓存
+ * @param cacheName
+ * @param key
+ * @return
+ */
+ public static Object get(String cacheName, String key) {
+ return getCache(cacheName).get(key);
+ }
+
+ /**
+ * 写入缓存
+ * @param cacheName
+ * @param key
+ * @param value
+ */
+ public static void put(String cacheName, String key, Object value) {
+ Cache cache=cacheManager.getCache(cacheName);
+ cache.put(key, value);
+ }
+
+ /**
+ * 从缓存中移除
+ * @param cacheName
+ * @param key
+ */
+ public static void remove(String cacheName, String key) {
+ getCache(cacheName).remove(key);
+ }
+
+ /**
+ * 获得一个Cache,没有则创建一个。
+ * @param cacheName
+ * @return
+ */
+ /*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);
+ }
+ return cache;
+ }*/
+ private static Cache getCache(String cacheName){
+ Cache cache = cacheManager.getCache(cacheName);
+ return cache;
+ }
+
+ public static RedisCacheManager getCacheManager() {
+ return cacheManager;
+ }
+
+}
|
