Authored by qinchao

redis增加清除缓存功能,根据key的prefix*

... ... @@ -107,10 +107,6 @@
<version>0.1.53</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
... ...
... ... @@ -6,6 +6,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.exceptions.JedisDataException;
import java.util.*;
... ... @@ -281,12 +283,80 @@ public class RedisCommonUtil {
return result;
}
/**
* redis 清除缓存 ,根据前缀
*/
public static void removeRedisKeyByPrefix(String host,int port,int selectIndex,String prefix){
if(StringUtils.isBlank(prefix)){
return;
}
Jedis client =null;
try {
client=new Jedis(host, port);
ScanParams scanParams = new ScanParams();
scanParams.match(prefix+"*");
Long total=0L;
List<String> list;
ScanResult<String> sr;
String nextCursor="0";
do {
sr = client.scan(nextCursor,scanParams);
nextCursor=new String(sr.getCursorAsBytes(),"UTF-8");
if(sr!=null){
list = sr.getResult();
if(list!=null&&list.size()>0){
total = total+(list.size());
System.out.println(list);
for(String key:list){
client.del(key);
}
}
}
System.out.println(nextCursor);
}while (!nextCursor.equals("0"));
System.out.println("total is "+total);
}catch (Exception e){
logger.error(e.toString());
}finally {
if(client!=null){
client.close();
}
}
}
/* public 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;
try {
client=new Jedis(host, port);
// client.select(selectIndex); redis数据库支持select,但是twemproxy不支持select
client.set(keyName,value);
String rtn = client.get(keyName);
result.put("result",rtn);
result.put("flag","suc");
}catch (Exception e){
logger.error(e.toString());
result.put("result",e.toString());
result.put("flag","fail");
}finally {
if(client!=null){
client.close();
}
}
return result;
}*/
public static void main(String args[]){
/* for(int i=0;i<1000;i++){
System.out.println(cmd_set("192.168.103.93",16379,1,"name"+i,""+i));
}*/
//System.out.println(getRedisInfo("172.16.6.104",6389));
// System.out.println(getRedisInfo("192.168.103.94",16379));
System.out.println(cmd_get("192.168.103.93", 16379,1, "qc"));
// System.out.println(cmd_get("192.168.103.93", 16379,1, "qc"));
removeRedisKeyByPrefix("192.168.103.93",16379,1,"name");
//System.out.println(getRedisIsSlave("192.168.102.222", 6379, "192.168.102.222",6379));
}
}
\ No newline at end of file
... ...
... ... @@ -41,7 +41,7 @@ public class RedisOperateServiceImpl implements RedisOperateService {
//aws 同步 ; aws 不同步 ;qcloud 同步 ; qcloud 不同步 ;大数据aws ;大数据qcloud
String[] typeInfoNames={"aws_redis_sync","aws_redis_no_sync","qcloud_redis_sync","qcloud_no_sync_redis","bigdata_redis_aws","bigdata_redis_qcloud"};
String[] typeInfoNames_CN={"AWS-同步","AWS-不同步","QCLOUD-同步","QCLOUD-不同步","大数据_aws","大数据_qcloud"};
String[] clouds={"AWS","AWS","QCLOUD","QCLOUD","AWS","QCLOUD"};
//String[] clouds={"AWS","AWS","QCLOUD","QCLOUD","AWS","QCLOUD"};
Map<String,List<Map<String,String>>> map= Maps.newLinkedHashMap();
for(int i=0;i<typeInfoNames.length;i++){
String typeInfoName=typeInfoNames[i];
... ...