redis 工具类

package com.dz.im.tools;

 

import java.util.List;

 

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

 

public class RedisUtil {

 

//Redis服务器IP

private static String ADDR = PropertiesUtil.Intance.GetProperty("redis.ip");

    

//Redis的端口号

private static int PORT = Integer.parseInt(PropertiesUtil.Intance.GetProperty("redis.port"));

    

//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。

private static int MAX_ACTIVE = Integer.parseInt(PropertiesUtil.Intance.GetProperty("redis.pool.maxActive"));

    

//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。

private static int MAX_IDLE =Integer.parseInt(PropertiesUtil.Intance.GetProperty("redis.pool.maxIdle"));

    

//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;

private static int MAX_WAIT =Integer.parseInt(PropertiesUtil.Intance.GetProperty("redis.pool.maxWait"));

    

//当调用borrow Object方法时,是否进行有效性检查

     private static String TEST_ON_BORROW = PropertiesUtil.Intance.GetProperty("redis.pool.testOnBorrow");

     

 //当调用return Object方法时,是否进行有效性检查

 private static String TEST_ON_RETURN = PropertiesUtil.Intance.GetProperty("redis.pool.testOnReturn");

     

//超时时间

 private static int TIMEOUT = Integer.parseInt(PropertiesUtil.Intance.GetProperty("redis.timeout"));;

    

private static JedisPool jedisPool = null;

    

    /**

     * 初始化Redis连接池

     */

    private static void initialPool(){

        try {

        JedisPoolConfig config = new JedisPoolConfig();

        config.setMaxActive(MAX_ACTIVE);

            config.setMaxIdle(MAX_IDLE);

            config.setMaxWait(MAX_WAIT);

            config.setTestOnBorrow(Boolean.valueOf(TEST_ON_BORROW));

            config.setTestOnReturn(Boolean.valueOf(TEST_ON_RETURN));

            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);

        } catch (Exception e) {

        e.printStackTrace();

        LoggerUtil.MyLogger.error("init jedispoll error :%s", e);

        }

    }

    

    /**

     * 在多线程环境同步初始化

     */

    private static synchronized void poolInit() {

        if (jedisPool == null) {  

            initialPool();

        }

    }

     

     /**

      * 释放jedis资源

      * @param jedis

      */

     private static void returnResource(final Jedis jedis) {

         if (jedis != null && jedisPool !=null) {

             jedisPool.returnResource(jedis);

         }

     }

     

     /**

      * 销毁连接  

      * @param shardedJedis

      */

     private static void returnBrokenResource(final Jedis jedis) {

         try {

        jedisPool.returnBrokenResource(jedis);

         } catch (Exception e) {

        LoggerUtil.MyLogger.error("returnBrokenResource error", e);

         }

     }

     

     /**

      * 设置String 值

      * @param key

      * @param value

      * @return

      */

     public static boolean setString(String key, String value) {

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 jedis.set(key, value);

             }

             return true;

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("set String error", ex);

             returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return false;

     }

     

     /**

      * 获取String值

      * @param key

      * @return value

      */

     public static String getString(String key){

    String value="";

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

        jedis = jedisPool.getResource(); 

        value=jedis.get(key);

        }

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("get String error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return value;

     }

     

     /**

      * 删除String值

      * @param key

      * @return

      */

     public static boolean delString(String key) {

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

        jedis = jedisPool.getResource(); 

        jedis.del(key);

        }

             return true;

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("del String error.", ex);

             returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return false;

     }

     

     /**

      * 添加到List

      * @param key

      * @param value

      * @return

      */

     public static boolean addList(String key, String value) {

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 jedis.lpush(key, value);

             }

             return true;

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("redis addList  error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return false;

     }

     

     /**

      * 截取List

      * @param key 列表key

      * @param start 开始位置 从0开始

      * @param end 结束位置

      * @return

      */

     public static List<String> rangeList(String key, long start, long end) {

    List<String> msgList=null;

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 //第一个是key,第二个是起始位置,第三个是结束位置, -1表示取得所有 

        msgList=jedis.lrange(key, start, end);

             }

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("redis range list  error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return msgList;

     }

     

     /**

      * 检查List长度

      * @param key

      * @return

      */

     public static long countList(String key) {

    long redisKeyLen=0;

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 redisKeyLen=jedis.llen(key);

             }

         } catch (Exception ex) {

             LoggerUtil.MyLogger.error("countList error", ex);

             returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return redisKeyLen;

     }

     

    /**

     * 根据key,修改List里面的值

     * @param key

     * @param index

     * @param value

     * @return

     */

     public static void updateListByIndex(String key,int index,String value) {

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 jedis.lset(key, index, value);

             }

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("redis updateListByIndex  error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

     }

     

     /**

      * 删除List里面的值

      * @param key

      * @param count

      * @param value

      */

     public static void deleteListByIndex(String key,int count,String value){

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 jedis.lrem(key, count, value);

             }

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("redis updateListByIndex  error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

     }

 

     /**

      * 添加到Set

      * @param key

      * @param value

      * @return

      */

     public static boolean addSet(String key, String value) {

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 jedis.sadd(key, value);

             }

             return true;

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("redis addSet  error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return false;

     }

     

     /**

      * 添加到Map

      * @return

      */

     public static boolean addMap(String key,Map<String,String> map){

    Jedis jedis=null;

    if (jedisPool == null) {  

             poolInit();

         }

         try {

        if (jedisPool != null) {  

                 jedis = jedisPool.getResource(); 

                 jedis.hmset(key, map);

             }

             return true;

         } catch (Exception ex) {

        LoggerUtil.MyLogger.error("redis addSet  error", ex);

        returnBrokenResource(jedis);

         } finally {

        returnResource(jedis);

         }

         return false;

     }

 

}

 

猜你喜欢

转载自taiwei-peng.iteye.com/blog/2322679