Authored by qinchao

redis清理缓存

... ... @@ -375,6 +375,56 @@ public class RedisCommonUtil {
return total;
}
public static Long removeRedisKeyByPrefix(Jedis client,String prefix){
logger.info("removeRedisKeyByPrefix begin " ,prefix);
Long total=0L;
if(StringUtils.isBlank(prefix)){
return total;
}
try {
ScanParams scanParams = new ScanParams();
scanParams.count(1000);//每次扫描多少条记录,值越大消耗的时间越短,但会影响redis性能。建议设为一千到一万
scanParams.match(prefix+"*");
List<String> listAllKey=new ArrayList<>();
ScanResult<String> sr;
String nextCursor = ScanParams.SCAN_POINTER_START;
do {
sr = client.scan(nextCursor,scanParams);
if(sr!=null){
List<String> list = sr.getResult();
if(list!=null&&list.size()>0){
listAllKey.addAll(list);
}
}
//nextCursor=new String(sr.getCursorAsBytes(),"UTF-8");
nextCursor=sr.getStringCursor();
logger.info("removeRedisKeyByPrefix nextCursor {}",nextCursor);
}while (!nextCursor.equals("0"));
//删除
logger.info("removeRedisKeyByPrefix {} ",listAllKey);
total = 0L+ listAllKey.size();
if(total>0){
String[] arr = listAllKey.toArray(new String[listAllKey.size()]);
client.del(arr);
}
}catch (Exception e){
logger.error("removeRedisKeyByPrefix error {}",e);
}finally {
//不需要关闭了
if(client!=null){
client.close();
}
}
return total;
}
/* private static Map<String,String> cmd_set(String host,int port,int selectIndex,String keyName,String value){
Map<String,String> result=new HashMap<String,String>();
Jedis client =null;
... ...
... ... @@ -16,11 +16,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by craig.qin on 2017/7/11.
... ... @@ -29,12 +33,16 @@ import java.util.Map;
public class RedisOperateServiceImpl implements RedisOperateService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public static final int TIMEOUT = 2000;
@Autowired
MTypeInfoMapper mTypeInfoMapper;
@Autowired
IMObjectInfoService mobjectService;
Map<String, JedisPool> JEDIS_POOL_MAP = new ConcurrentHashMap<>();
/**
*
* 从cmdb:监控对象中取host数据
... ... @@ -334,7 +342,7 @@ public class RedisOperateServiceImpl implements RedisOperateService {
if(sb.length()>0){
sb.append("<br/>");
}
sb.append("total delete "+RedisCommonUtil.removeRedisKeyByPrefix(ip,port,prefix)+" keys with prefix "+prefix+" at "+ipAndPortStr);
sb.append("total delete "+RedisCommonUtil.removeRedisKeyByPrefix(fecthJedis(ip,port),prefix)+" keys with prefix "+prefix+" at "+ipAndPortStr);
}
... ... @@ -343,4 +351,25 @@ public class RedisOperateServiceImpl implements RedisOperateService {
rtnBaseResponse.setData(sb.toString());
return rtnBaseResponse;
}
private Jedis fecthJedis(String ip, int port) {
String jedisUrl=ip+":"+port;
JedisPool jedisPool = JEDIS_POOL_MAP.get(jedisUrl);
if (null == jedisPool) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(8);
jedisPoolConfig.setMaxTotal(8);
jedisPool = new JedisPool(jedisPoolConfig, ip, port, TIMEOUT);
JEDIS_POOL_MAP.put(jedisUrl, jedisPool);
}
try {
return jedisPool.getResource();
} catch (Exception e) {
logger.error("RedisOperateServiceImpl fecth Jedis client {} , error {}",jedisUrl, e);
}
return null;
}
}
... ...