redis list基本操作,redis工具类

纯手打,转载务请附上本文网址!!!

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

本文中使用的是Redis中的list数据结构,其它结构请参考官方文档

package com.yufeng.redis.util;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

/**
 * Redis数据库分为16个部分[0-15],默认使用的是0,可以自己指定使用哪个部分
 * 这个类是对redis中的list进行操作的
 *
 * 请大家引以为鉴,redis的连接和关闭是很影响效率的,所以在操作redis时要尽
 * 可能减少连接和关闭的操作,不要和上一个版本代码一样
 *
 * yufeng.fan02@ele.me 2018/1/23
 */
public class RedisUtil {

    private static JedisPoolConfig config=null;
    private static JedisPool jedisPool=null;
    private static Jedis jedis=null;

    /**
     * 获得单例的redis连接
     * @param ip
     * @param port
     * @param passwd
     * @return
     */
    public static Jedis getConnection(String ip,int port,String passwd){
        if(config==null){
            config=new JedisPoolConfig();
            config.setMaxTotal(500);
            config.setMaxIdle(5);
            config.setMaxWaitMillis(1000*10);
            //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
            config.setTestOnBorrow(true);
        }
        if(jedisPool==null){
            if(passwd==null || passwd.length()==0) {
                jedisPool = new JedisPool(config, ip, port, 10000);
            }else{
                jedisPool = new JedisPool(config, ip, port, 10000, passwd);
            }
        }
        if (jedis==null){
            jedis=jedisPool.getResource();
        }
        return jedis;
    }

    /**
     * 获得某个key对应的value
     * @param conn
     * @param key
     * @return
     */
    public static String getValue(Jedis conn,String key){
        String value=conn.get(key);
        return value;
    }

    /**
     * 获得某个key对应的value
     * @param conn
     * @param key
     * @param value
     * @return
     */
    public static String setValue(Jedis conn,String key,String value){
        String result=conn.set(key,value);
        return result;
    }

    /**
     * 获取正在运行的线程的个数
     * @param conn
     * @param key
     * @return
     */
    public static Long getRunningTaskLength(Jedis conn,String key){
        Long length=conn.llen(key);
        return length;
    }

    /**
     * 将新的线程加入正在运行的线程队列
     * @param conn
     * @param key
     * @param threadSpecificId
     * @return
     */
    public static Long rpushRunningTaskList(Jedis conn,String key,String threadSpecificId){
        Long result=conn.rpush(key,threadSpecificId); //result为1表示成功
        return result;
    }

    /**
     * 将已完成的线程排出
     * @param conn
     * @param key
     * @return
     */
    public static String lpopRunningTaskList(Jedis conn,String key){
//        String taskid=jedis.lpop("runningtask");
        String taskid=conn.lpop(key);
        return taskid;
    }

    /**
     * 自动运行的线程队列放在队尾
     * @param conn
     * @param threadSpecificId
     * @param key
     * @return
     */
    public static Long rpushTaskList(Jedis conn,String threadSpecificId,String key){
        Long result=conn.rpush(key,threadSpecificId);
        return result;
    }

    /**
     * 手动运行的线程队列放在队头
     * @param conn
     * @param threadSpecificId
     * @param key
     * @return
     */
    public static Long lpushTaskList(Jedis conn,String threadSpecificId,String key){
        Long result=conn.lpush(key,threadSpecificId);
        return result;
    }

    /**
     * 将新的进程加入正在运行的线程队列
     * @param conn
     * @param key
     * @return
     */
    public static Long popTaskToRunningTaskList(Jedis conn,String key){
        Long result=conn.rpush(key,conn.lpop(key));
        return result;
    }

    /**
     * 获得将要运行的线程队列长度
     * @param conn
     * @param key
     * @return
     */
    public static Long getTaskListLength(Jedis conn,String key){
        Long length=conn.llen(key);
        return length;
    }

    /**
     * 获得lrange返回的结果
     * @param conn
     * @param key
     * @param start
     * @param end
     * @return
     */
    public static List<String> getAllMembers(Jedis conn,String key,long start,long end){
        return conn.lrange(key,start,end);
    }

    /**
     * 清空redis中的所有内容,慎用
     * @param conn
     * @return
     */
    public static String flushAllInRedis(Jedis conn){
        String resultString=conn.flushAll();
        return resultString;
    }

    /**
     * 关闭redis和redispool
     */
    public static void close(){
        try{
            jedis.close();
            jedisPool.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

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

 

import redis.clients.jedis.Jedis;

/**
 * Redis数据库分为16个部分[0-15],默认使用的是0,可以自己指定使用哪个部分
 * yufeng 2018/1/23
 */
public class CoverageTaskHandler {

    /**
     * 获得某个key对应的value
     * @param key
     * @return
     */
    public static String getValue(String key){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        String value=jedis.get(key);
        jedis.close();
        return value;
    }

    /**
     * 获得某个key对应的value
     * @param key
     * @return
     */
    public static String setValue(String key,String value){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        String result=jedis.set(key,value);
        jedis.close();
        return result;
    }

    /**
     * 获取正在运行的线程的个数
     * @return
     */
    public static Long getRunningTaskLength(String key){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Long length=jedis.llen(key);
        jedis.close();
        return length;
    }

    /**
     * 将新的线程加入正在运行的线程队列
     * @param threadSpecificId appid:env:prodCommitId:commitId:方式(手工或者自动)
     * @return
     */
    public static Long rpushRunningTaskList(String key,String threadSpecificId){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Long result=jedis.rpush(key,threadSpecificId); //result为1表示成功
        jedis.close();
        return result;
    }

    /**
     * 将已完成的线程排出
     * @return
     */
    public static String lpopRunningTaskList(String key){
        Jedis jedis=new Jedis("127.0.0.1",6379);
//        String taskid=jedis.lpop("runningtask");
        String taskid=jedis.lpop(key);
        jedis.close();
        return taskid;
    }

    /**
     * 自动运行的线程队列放在队尾
     * @param threadSpecificId appid:env:prodCommitId:commitId:方式(手工或者自动)
     * @return
     */
    public static Long rpushTaskList(String threadSpecificId){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Long result=jedis.rpush("tasklist",threadSpecificId);
        jedis.close();
        return result;
    }

    /**
     * 手动运行的线程队列放在队头
     * @param threadSpecificId appid:env:prodCommitId:commitId:方式(手工或者自动)
     * @return
     */
    public static Long lpushTaskList(String threadSpecificId){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Long result=jedis.lpush("tasklist",threadSpecificId);
        jedis.close();
        return result;
    }

    /**
     * 将新的进程加入正在运行的线程队列
     * @return
     */
    public static Long popTaskToRunningTaskList(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Long result=jedis.rpush("runningtask",jedis.lpop("tasklist"));
        jedis.close();
        return result;
    }
    /**
     * 获得将要运行的线程队列长度
     * @return
     */
    public static Long getTaskListLength(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        Long length=jedis.llen("tasklist");
        jedis.close();
        return length;
    }

    /**
     * 清空redis中的所有内容,慎用
     * @return
     */
    public static String flushAllInRedis(){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        String resultString=jedis.flushAll();
        jedis.close();
        return resultString;
    }
}

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值