package com.nis.util; import java.util.List; 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; import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.service.SpringContextHolder; import jersey.repackaged.com.google.common.collect.Maps; /** * asn no缓存工具类 * @author Administrator * */ public class AsnCacheUtils{ private static Logger logger=Logger.getLogger(AsnCacheUtils.class); /** * ASN号缓存 */ private static final String ASN_NO_CACHE = "asnNoCache"; private static final int cache_rage = 1000; private final static ConfigGroupInfoDao configGroupInfoDao = SpringContextHolder.getBean(ConfigGroupInfoDao.class); /** * 获取缓存 * @param cacheName * @param key * @return */ public static ConfigGroupInfo get(Long key) { 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 map=(Map)element; if(map.containsKey(key)) { return map.get(key); } } return null; } public static Map getMap(Object key) { Object element = getCache(ASN_NO_CACHE).get(key); return (Map)element; } public static void clearCache() { logger.warn("clear cache!"); getCache(ASN_NO_CACHE).clear(); } public static List getAllAsnGroup(){ List configGroupInfos=Lists.newArrayList(); Cache cache=getCache(ASN_NO_CACHE); for(Object val : cache.values()) { if(val!=null) { Map map=(Map)val; configGroupInfos.addAll(map.values()); } } return configGroupInfos; } /** * * 初始化缓存 * * @param force是否强制刷新 */ //@PostConstruct public synchronized static void init(boolean force) { long start=System.currentTimeMillis(); logger.warn("AsnCacheUtils init start..."); Cache cache=getCache(ASN_NO_CACHE); if(force) { List list=configGroupInfoDao.findAllList(4); Map> groupMap=Maps.newHashMap(); for(ConfigGroupInfo configGroupInfo:list) { if(groupMap.containsKey(configGroupInfo.getAsnId()/cache_rage)) { groupMap.get(configGroupInfo.getAsnId()/cache_rage) .put(configGroupInfo.getAsnId(), configGroupInfo); }else { Map m=Maps.newHashMap(); m.put(configGroupInfo.getAsnId(), configGroupInfo); groupMap.put(configGroupInfo.getAsnId()/cache_rage, m); } } for(Entry> e:groupMap.entrySet()) { cache.put(e.getKey(),e.getValue()); } }else { //查询总量 Long count=configGroupInfoDao.getCountByType(4); boolean loadDatabase=false; if(cache.keys().size()==0) { loadDatabase=true; }else { long c=0l; for(Object key:cache.keys()) { Map map = getMap(key); if(map != null) { c+=getMap(key).size(); } } if(c!=count) { loadDatabase=true; } } if(loadDatabase) { List list=configGroupInfoDao.findAllList(4); Map> groupMap=Maps.newHashMap(); for(ConfigGroupInfo configGroupInfo:list) { if(groupMap.containsKey(configGroupInfo.getAsnId()/cache_rage)) { groupMap.get(configGroupInfo.getAsnId()/cache_rage) .put(configGroupInfo.getAsnId(), configGroupInfo); }else { Map m=Maps.newHashMap(); m.put(configGroupInfo.getAsnId(), configGroupInfo); groupMap.put(configGroupInfo.getAsnId()/cache_rage, m); } } for(Entry> e:groupMap.entrySet()) { cache.put(e.getKey(), e.getValue()); } } } long end=System.currentTimeMillis(); logger.warn("AsnCacheUtils init finish,cost:"+(end-start)); } /** * 写入ASN_NO_CACHE缓存 * @param key * @return */ public static void put(Long key, ConfigGroupInfo value) { Long _key=key/cache_rage; Object element = getCache(ASN_NO_CACHE).get(_key); if(element==null) { Map map=Maps.newHashMap(); map.put(key, value); getCache(ASN_NO_CACHE).put(_key,map); }else { Map map=(Map)element; map.put(key, value); getCache(ASN_NO_CACHE).put(_key,map); } } /** * 从缓存中移除 * @param cacheName * @param key */ public static void remove(String key) { getCache(ASN_NO_CACHE).remove(key); } public static void remove(Long key) { Long _key=key/cache_rage; Object element = getCache(ASN_NO_CACHE).get(_key); if(element!=null) { Map map=(Map)element; if(map.containsKey(key)) { map.remove(key); } if(map.isEmpty()) { getCache(ASN_NO_CACHE).remove(_key); }else { getCache(ASN_NO_CACHE).put(_key, map); } } } private static Cache getCache(String cacheName){ Cache cache = CacheUtils.getCacheManager().getCache(cacheName); return cache; } public static String getCacheName() { return ASN_NO_CACHE; } }