session共享

登录缓存清理
缓存更换为redis缓存代码提交
This commit is contained in:
段冬梅
2018-12-16 11:04:25 +08:00
parent 22920b84cc
commit ed45211de9
11 changed files with 310 additions and 82 deletions

View File

@@ -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;
}