(1)asn no放入eCache中
(2)导入验证采用多线程验证,优化验证速度 (3)asn ip导入方式调整(未采用多线程,因为redis承受不了) (4)asn ip列表展示速度优化 (5)导入方式重写:采用csv模式,限制采用xlsx格式,加载80万数据不会内存溢出.
This commit is contained in:
147
src/main/java/com/nis/util/AsnCacheUtils.java
Normal file
147
src/main/java/com/nis/util/AsnCacheUtils.java
Normal file
@@ -0,0 +1,147 @@
|
||||
package com.nis.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
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;
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.Element;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
Element element = getCache(ASN_NO_CACHE).get(key/cache_rage);
|
||||
if(element!=null) {
|
||||
Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue();
|
||||
if(map.containsKey(key)) {
|
||||
return map.get(key);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Map<Long,ConfigGroupInfo> getMap(Object key) {
|
||||
Element element = getCache(ASN_NO_CACHE).get(key);
|
||||
return (Map<Long,ConfigGroupInfo>)element.getObjectValue();
|
||||
}
|
||||
/**
|
||||
* 初始化缓存
|
||||
*/
|
||||
//@PostConstruct
|
||||
public synchronized static void init() {
|
||||
long start=System.currentTimeMillis();
|
||||
logger.warn("AsnCacheUtils init start...");
|
||||
Cache cache=getCache(ASN_NO_CACHE);
|
||||
//查询总量
|
||||
Long count=configGroupInfoDao.getCountByType(4);
|
||||
//缓存key
|
||||
boolean loadDatabase=false;
|
||||
if(cache.getKeys().size()==0) {
|
||||
loadDatabase=true;
|
||||
}else {
|
||||
long c=0l;
|
||||
for(Object key:cache.getKeys()) {
|
||||
c+=getMap(key).size();
|
||||
}
|
||||
if(c!=count) {
|
||||
loadDatabase=true;
|
||||
}
|
||||
}
|
||||
if(loadDatabase) {
|
||||
List<ConfigGroupInfo> list=configGroupInfoDao.findAllList(4);
|
||||
Map<Long,Map<Long,ConfigGroupInfo>> 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<Long,ConfigGroupInfo> m=Maps.newHashMap();
|
||||
m.put(configGroupInfo.getAsnId(), configGroupInfo);
|
||||
groupMap.put(configGroupInfo.getAsnId()/cache_rage, m);
|
||||
}
|
||||
}
|
||||
for(Entry<Long, Map<Long, ConfigGroupInfo>> e:groupMap.entrySet()) {
|
||||
Element element = new Element(e.getKey(), e.getValue());
|
||||
cache.put(element);
|
||||
}
|
||||
}
|
||||
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;
|
||||
Element 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);
|
||||
}else {
|
||||
Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue();
|
||||
map.put(key, value);
|
||||
element = new Element(_key, map);
|
||||
}
|
||||
getCache(ASN_NO_CACHE).put(element);
|
||||
}
|
||||
/**
|
||||
* 从缓存中移除
|
||||
* @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;
|
||||
Element element = getCache(ASN_NO_CACHE).get(_key);
|
||||
if(element!=null) {
|
||||
Map<Long,ConfigGroupInfo> map=(Map<Long,ConfigGroupInfo>)element.getObjectValue();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user