RedisUtil: Jedis連接自動釋放


package cloud.app.prod.home.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public class RedisUtil {

    // Redis服務器IP
    private static String ADDR = "127.0.0.1";

    // Redis的端口號
    private static int PORT = 6379;

    // 訪問密碼
    private static String AUTH = "scrm__12345";

    // 可用連接實例的最大數目,默認值為8;
    // 如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。

    private static int MAX_ACTIVE = 1024;

    // 控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例,默認值也是8。
    private static int MAX_IDLE = 200;

    // 等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException;
    private static int MAX_WAIT = 10000;
    private static int TIMEOUT = 10000;

    // 在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

  
    /**
     * 初始化Redis連接池
     */
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxActive(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWait(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 獲取Jedis實例
     * 
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                return resource;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 釋放jedis資源
     * 
     * @param jedis
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedisPool.returnResource(jedis);
        }
    } 
  
    /** 
     * Jedis對象出異常的時候,回收Jedis對象資源 
     *  
     * @param jedis 
     */  
    public static void returnBrokenResource(final Jedis jedis) {  
        if (jedis != null) {  
            jedisPool.returnBrokenResource(jedis);  
        }  
  
    }  
    
    /**
     * 通過Redis的key獲取值,並釋放連接資源
     * @param key
     * @return 成功返回value,失敗返回null
     */
    public static String get(String key) {
        Jedis jedis = null;
        String value = null;

        try {
            jedis = getJedis();
            if (null != jedis) {
                value = jedis.get(key);
            }            
        } catch (Exception e) {
            returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return value; 
    }
    
    /**
     * 向redis存入key和value(如果key已經存在 則覆蓋),並釋放連接資源
     * @param key
     * @param value
     */
    public static void set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis) {
                jedis.set(key, value);
            }            
        } catch (Exception e) {
            returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedis);        
        } 
    }
    
    /**
     * 向redis存入key和value(如果key已經存在 則覆蓋),並且設置存活時間,及釋放連接資源
     * @param key
     * @param value
     * @param seconds
     */
    public static void set(String key, String value, int seconds) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis) {
                jedis.set(key, value);
                jedis.expire(key, seconds);
            }            
        } catch (Exception e) {
            returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedis);        
        } 
    }

    public static String byte2hex(byte[] buffer) {
        String h = "0x";
        for (byte aBuffer : buffer) {
            String temp = Integer.toHexString(aBuffer & 0xFF);
            if (temp.length() == 1) {
                temp = "0" + temp;
            }
            h = h + " " + temp;
        }
        return h;
    }

}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM