Authored by qinchao

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

@@ -107,10 +107,6 @@ @@ -107,10 +107,6 @@
107 <version>0.1.53</version> 107 <version>0.1.53</version>
108 </dependency> 108 </dependency>
109 109
110 - <dependency>  
111 - <groupId>redis.clients</groupId>  
112 - <artifactId>jedis</artifactId>  
113 - </dependency>  
114 110
115 <dependency> 111 <dependency>
116 <groupId>commons-net</groupId> 112 <groupId>commons-net</groupId>
@@ -6,6 +6,8 @@ import org.slf4j.Logger; @@ -6,6 +6,8 @@ import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
7 import org.springframework.util.CollectionUtils; 7 import org.springframework.util.CollectionUtils;
8 import redis.clients.jedis.Jedis; 8 import redis.clients.jedis.Jedis;
  9 +import redis.clients.jedis.ScanParams;
  10 +import redis.clients.jedis.ScanResult;
9 import redis.clients.jedis.exceptions.JedisDataException; 11 import redis.clients.jedis.exceptions.JedisDataException;
10 12
11 import java.util.*; 13 import java.util.*;
@@ -281,12 +283,80 @@ public class RedisCommonUtil { @@ -281,12 +283,80 @@ public class RedisCommonUtil {
281 return result; 283 return result;
282 } 284 }
283 285
  286 + /**
  287 + * redis 清除缓存 ,根据前缀
  288 + */
  289 + public static void removeRedisKeyByPrefix(String host,int port,int selectIndex,String prefix){
  290 + if(StringUtils.isBlank(prefix)){
  291 + return;
  292 + }
  293 +
  294 + Jedis client =null;
  295 + try {
  296 + client=new Jedis(host, port);
  297 + ScanParams scanParams = new ScanParams();
  298 + scanParams.match(prefix+"*");
284 299
  300 + Long total=0L;
  301 + List<String> list;
  302 + ScanResult<String> sr;
  303 + String nextCursor="0";
  304 + do {
  305 + sr = client.scan(nextCursor,scanParams);
  306 + nextCursor=new String(sr.getCursorAsBytes(),"UTF-8");
  307 + if(sr!=null){
  308 + list = sr.getResult();
  309 + if(list!=null&&list.size()>0){
  310 + total = total+(list.size());
  311 + System.out.println(list);
  312 + for(String key:list){
  313 + client.del(key);
  314 + }
  315 + }
  316 + }
  317 + System.out.println(nextCursor);
  318 + }while (!nextCursor.equals("0"));
  319 +
  320 +System.out.println("total is "+total);
  321 + }catch (Exception e){
  322 + logger.error(e.toString());
  323 + }finally {
  324 + if(client!=null){
  325 + client.close();
  326 + }
  327 + }
  328 + }
  329 +
  330 +/* public static Map<String,String> cmd_set(String host,int port,int selectIndex,String keyName,String value){
  331 + Map<String,String> result=new HashMap<String,String>();
  332 + Jedis client =null;
  333 + try {
  334 + client=new Jedis(host, port);
  335 + // client.select(selectIndex); redis数据库支持select,但是twemproxy不支持select
  336 + client.set(keyName,value);
  337 + String rtn = client.get(keyName);
  338 + result.put("result",rtn);
  339 + result.put("flag","suc");
  340 + }catch (Exception e){
  341 + logger.error(e.toString());
  342 + result.put("result",e.toString());
  343 + result.put("flag","fail");
  344 + }finally {
  345 + if(client!=null){
  346 + client.close();
  347 + }
  348 + }
  349 + return result;
  350 + }*/
285 351
286 public static void main(String args[]){ 352 public static void main(String args[]){
  353 + /* for(int i=0;i<1000;i++){
  354 + System.out.println(cmd_set("192.168.103.93",16379,1,"name"+i,""+i));
  355 + }*/
287 //System.out.println(getRedisInfo("172.16.6.104",6389)); 356 //System.out.println(getRedisInfo("172.16.6.104",6389));
288 // System.out.println(getRedisInfo("192.168.103.94",16379)); 357 // System.out.println(getRedisInfo("192.168.103.94",16379));
289 - System.out.println(cmd_get("192.168.103.93", 16379,1, "qc")); 358 + // System.out.println(cmd_get("192.168.103.93", 16379,1, "qc"));
  359 + removeRedisKeyByPrefix("192.168.103.93",16379,1,"name");
290 //System.out.println(getRedisIsSlave("192.168.102.222", 6379, "192.168.102.222",6379)); 360 //System.out.println(getRedisIsSlave("192.168.102.222", 6379, "192.168.102.222",6379));
291 } 361 }
292 } 362 }
@@ -41,7 +41,7 @@ public class RedisOperateServiceImpl implements RedisOperateService { @@ -41,7 +41,7 @@ public class RedisOperateServiceImpl implements RedisOperateService {
41 //aws 同步 ; aws 不同步 ;qcloud 同步 ; qcloud 不同步 ;大数据aws ;大数据qcloud 41 //aws 同步 ; aws 不同步 ;qcloud 同步 ; qcloud 不同步 ;大数据aws ;大数据qcloud
42 String[] typeInfoNames={"aws_redis_sync","aws_redis_no_sync","qcloud_redis_sync","qcloud_no_sync_redis","bigdata_redis_aws","bigdata_redis_qcloud"}; 42 String[] typeInfoNames={"aws_redis_sync","aws_redis_no_sync","qcloud_redis_sync","qcloud_no_sync_redis","bigdata_redis_aws","bigdata_redis_qcloud"};
43 String[] typeInfoNames_CN={"AWS-同步","AWS-不同步","QCLOUD-同步","QCLOUD-不同步","大数据_aws","大数据_qcloud"}; 43 String[] typeInfoNames_CN={"AWS-同步","AWS-不同步","QCLOUD-同步","QCLOUD-不同步","大数据_aws","大数据_qcloud"};
44 - String[] clouds={"AWS","AWS","QCLOUD","QCLOUD","AWS","QCLOUD"}; 44 + //String[] clouds={"AWS","AWS","QCLOUD","QCLOUD","AWS","QCLOUD"};
45 Map<String,List<Map<String,String>>> map= Maps.newLinkedHashMap(); 45 Map<String,List<Map<String,String>>> map= Maps.newLinkedHashMap();
46 for(int i=0;i<typeInfoNames.length;i++){ 46 for(int i=0;i<typeInfoNames.length;i++){
47 String typeInfoName=typeInfoNames[i]; 47 String typeInfoName=typeInfoNames[i];