redis基本操作之实现线程队列

2018.3.12:更新代码,由于原工具类中每次操作都需要连接和断开Redis,很影响效率;所以改进工具类,使用JedisPool来获取Redis连接并增加新的功能,原来的代码不删除留作对比!

[java]  view plain  copy
  1. package com.yufeng.redis.util;  
  2.   
  3. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;  
  4. import redis.clients.jedis.Jedis;  
  5. import redis.clients.jedis.JedisPool;  
  6. import redis.clients.jedis.JedisPoolConfig;  
  7.   
  8. import java.util.List;  
  9.   
  10. /** 
  11.  * Redis数据库分为16个部分[0-15],默认使用的是0,可以自己指定使用哪个部分 
  12.  * 这个类是对redis中的list进行操作的 
  13.  * 
  14.  * 请大家引以为鉴,redis的连接和关闭是很影响效率的,所以在操作redis时要尽 
  15.  * 可能减少连接和关闭的操作,不要和上一个版本代码一样 
  16.  * 
  17.  * yufeng.fan02@ele.me 2018/1/23 
  18.  */  
  19. public class RedisUtil {  
  20.   
  21.     private static JedisPoolConfig config=null;  
  22.     private static JedisPool jedisPool=null;  
  23.     private static Jedis jedis=null;  
  24.   
  25.     /** 
  26.      * 获得单例的redis连接 
  27.      * @param ip 
  28.      * @param port 
  29.      * @param passwd 
  30.      * @return 
  31.      */  
  32.     public static Jedis getConnection(String ip,int port,String passwd){  
  33.         if(config==null){  
  34.             config=new JedisPoolConfig();  
  35.             config.setMaxTotal(500);  
  36.             config.setMaxIdle(5);  
  37.             config.setMaxWaitMillis(1000*10);  
  38.             //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;  
  39.             config.setTestOnBorrow(true);  
  40.         }  
  41.         if(jedisPool==null){  
  42.             if(passwd==null || passwd.length()==0) {  
  43.                 jedisPool = new JedisPool(config, ip, port, 10000);  
  44.             }else{  
  45.                 jedisPool = new JedisPool(config, ip, port, 10000, passwd);  
  46.             }  
  47.         }  
  48.         if (jedis==null){  
  49.             jedis=jedisPool.getResource();  
  50.         }  
  51.         return jedis;  
  52.     }  
  53.   
  54.     /** 
  55.      * 获得某个key对应的value 
  56.      * @param conn 
  57.      * @param key 
  58.      * @return 
  59.      */  
  60.     public static String getValue(Jedis conn,String key){  
  61.         String value=conn.get(key);  
  62.         return value;  
  63.     }  
  64.   
  65.     /** 
  66.      * 获得某个key对应的value 
  67.      * @param conn 
  68.      * @param key 
  69.      * @param value 
  70.      * @return 
  71.      */  
  72.     public static String setValue(Jedis conn,String key,String value){  
  73.         String result=conn.set(key,value);  
  74.         return result;  
  75.     }  
  76.   
  77.     /** 
  78.      * 获取正在运行的线程的个数 
  79.      * @param conn 
  80.      * @param key 
  81.      * @return 
  82.      */  
  83.     public static Long getRunningTaskLength(Jedis conn,String key){  
  84.         Long length=conn.llen(key);  
  85.         return length;  
  86.     }  
  87.   
  88.     /** 
  89.      * 将新的线程加入正在运行的线程队列 
  90.      * @param conn 
  91.      * @param key 
  92.      * @param threadSpecificId 
  93.      * @return 
  94.      */  
  95.     public static Long rpushRunningTaskList(Jedis conn,String key,String threadSpecificId){  
  96.         Long result=conn.rpush(key,threadSpecificId); //result为1表示成功  
  97.         return result;  
  98.     }  
  99.   
  100.     /** 
  101.      * 将已完成的线程排出 
  102.      * @param conn 
  103.      * @param key 
  104.      * @return 
  105.      */  
  106.     public static String lpopRunningTaskList(Jedis conn,String key){  
  107. //        String taskid=jedis.lpop("runningtask");  
  108.         String taskid=conn.lpop(key);  
  109.         return taskid;  
  110.     }  
  111.   
  112.     /** 
  113.      * 自动运行的线程队列放在队尾 
  114.      * @param conn 
  115.      * @param threadSpecificId 
  116.      * @param key 
  117.      * @return 
  118.      */  
  119.     public static Long rpushTaskList(Jedis conn,String threadSpecificId,String key){  
  120.         Long result=conn.rpush(key,threadSpecificId);  
  121.         return result;  
  122.     }  
  123.   
  124.     /** 
  125.      * 手动运行的线程队列放在队头 
  126.      * @param conn 
  127.      * @param threadSpecificId 
  128.      * @param key 
  129.      * @return 
  130.      */  
  131.     public static Long lpushTaskList(Jedis conn,String threadSpecificId,String key){  
  132.         Long result=conn.lpush(key,threadSpecificId);  
  133.         return result;  
  134.     }  
  135.   
  136.     /** 
  137.      * 将新的进程加入正在运行的线程队列 
  138.      * @param conn 
  139.      * @param key 
  140.      * @return 
  141.      */  
  142.     public static Long popTaskToRunningTaskList(Jedis conn,String key){  
  143.         Long result=conn.rpush(key,conn.lpop(key));  
  144.         return result;  
  145.     }  
  146.   
  147.     /** 
  148.      * 获得将要运行的线程队列长度 
  149.      * @param conn 
  150.      * @param key 
  151.      * @return 
  152.      */  
  153.     public static Long getTaskListLength(Jedis conn,String key){  
  154.         Long length=conn.llen(key);  
  155.         return length;  
  156.     }  
  157.   
  158.     /** 
  159.      * 获得lrange返回的结果 
  160.      * @param conn 
  161.      * @param key 
  162.      * @param start 
  163.      * @param end 
  164.      * @return 
  165.      */  
  166.     public static List<String> getAllMembers(Jedis conn,String key,long start,long end){  
  167.         return conn.lrange(key,start,end);  
  168.     }  
  169.   
  170.     /** 
  171.      * 清空redis中的所有内容,慎用 
  172.      * @param conn 
  173.      * @return 
  174.      */  
  175.     public static String flushAllInRedis(Jedis conn){  
  176.         String resultString=conn.flushAll();  
  177.         return resultString;  
  178.     }  
  179.   
  180.     /** 
  181.      * 关闭redis和redispool 
  182.      */  
  183.     public static void close(){  
  184.         try{  
  185.             jedis.close();  
  186.             jedisPool.close();  
  187.         }catch (Exception e){  
  188.             e.printStackTrace();  
  189.         }  
  190.     }  
  191. }  

旧的Redis工具类,做对比可以发现有缺点

[java]  view plain  copy
  1. import redis.clients.jedis.Jedis;  
  2.   
  3. /** 
  4.  * Redis数据库分为16个部分[0-15],默认使用的是0,可以自己指定使用哪个部分 
  5.  * yufeng 2018/1/23 
  6.  */  
  7. public class CoverageTaskHandler {  
  8.   
  9.     /** 
  10.      * 获得某个key对应的value 
  11.      * @param key 
  12.      * @return 
  13.      */  
  14.     public static String getValue(String key){  
  15.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  16.         String value=jedis.get(key);  
  17.         jedis.close();  
  18.         return value;  
  19.     }  
  20.   
  21.     /** 
  22.      * 获得某个key对应的value 
  23.      * @param key 
  24.      * @return 
  25.      */  
  26.     public static String setValue(String key,String value){  
  27.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  28.         String result=jedis.set(key,value);  
  29.         jedis.close();  
  30.         return result;  
  31.     }  
  32.   
  33.     /** 
  34.      * 获取正在运行的线程的个数 
  35.      * @return 
  36.      */  
  37.     public static Long getRunningTaskLength(String key){  
  38.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  39.         Long length=jedis.llen(key);  
  40.         jedis.close();  
  41.         return length;  
  42.     }  
  43.   
  44.     /** 
  45.      * 将新的线程加入正在运行的线程队列 
  46.      * @param threadSpecificId appid:env:prodCommitId:commitId:方式(手工或者自动) 
  47.      * @return 
  48.      */  
  49.     public static Long rpushRunningTaskList(String key,String threadSpecificId){  
  50.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  51.         Long result=jedis.rpush(key,threadSpecificId); //result为1表示成功  
  52.         jedis.close();  
  53.         return result;  
  54.     }  
  55.   
  56.     /** 
  57.      * 将已完成的线程排出 
  58.      * @return 
  59.      */  
  60.     public static String lpopRunningTaskList(String key){  
  61.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  62. //        String taskid=jedis.lpop("runningtask");  
  63.         String taskid=jedis.lpop(key);  
  64.         jedis.close();  
  65.         return taskid;  
  66.     }  
  67.   
  68.     /** 
  69.      * 自动运行的线程队列放在队尾 
  70.      * @param threadSpecificId appid:env:prodCommitId:commitId:方式(手工或者自动) 
  71.      * @return 
  72.      */  
  73.     public static Long rpushTaskList(String threadSpecificId){  
  74.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  75.         Long result=jedis.rpush("tasklist",threadSpecificId);  
  76.         jedis.close();  
  77.         return result;  
  78.     }  
  79.   
  80.     /** 
  81.      * 手动运行的线程队列放在队头 
  82.      * @param threadSpecificId appid:env:prodCommitId:commitId:方式(手工或者自动) 
  83.      * @return 
  84.      */  
  85.     public static Long lpushTaskList(String threadSpecificId){  
  86.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  87.         Long result=jedis.lpush("tasklist",threadSpecificId);  
  88.         jedis.close();  
  89.         return result;  
  90.     }  
  91.   
  92.     /** 
  93.      * 将新的进程加入正在运行的线程队列 
  94.      * @return 
  95.      */  
  96.     public static Long popTaskToRunningTaskList(){  
  97.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  98.         Long result=jedis.rpush("runningtask",jedis.lpop("tasklist"));  
  99.         jedis.close();  
  100.         return result;  
  101.     }  
  102.     /** 
  103.      * 获得将要运行的线程队列长度 
  104.      * @return 
  105.      */  
  106.     public static Long getTaskListLength(){  
  107.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  108.         Long length=jedis.llen("tasklist");  
  109.         jedis.close();  
  110.         return length;  
  111.     }  
  112.   
  113.     /** 
  114.      * 清空redis中的所有内容,慎用 
  115.      * @return 
  116.      */  
  117.     public static String flushAllInRedis(){  
  118.         Jedis jedis=new Jedis("127.0.0.1",6379);  
  119.         String resultString=jedis.flushAll();  
  120.         jedis.close();  
  121.         return resultString;  
  122.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值