Redis在JAVA中的運用(工具類)


最近項目需要用redis在中間做緩存所以寫了一個工具類作為練習用

redis版本:redis_version:3.0.504

用到阿里的解析JSON的庫:fastjson

import org.apache.log4j.Logger;
import org.apache.log4j.chainsaw.Main;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description:redis工具類
 * @ClassName:
 * @date 2016年10月31日 上午11:25:06
 */
@SuppressWarnings("unused")
public class RedisUtils
{
    private static final String IP = "127.0.0.1"; // ip
    private static final int PORT = 6379;         // 端口
    private static final String AUTH="";          // 密碼(原始默認是沒有密碼)
    private static int   MAX_ACTIVE = 1024;       // 最大連接數
    private static int   MAX_IDLE = 200;          // 設置最大空閑數
    private static int   MAX_WAIT = 10000;        // 最大連接時間
    private static int   TIMEOUT = 10000;         // 超時時間
    private static boolean BORROW = true;         // 在borrow一個事例時是否提前進行validate操作
    private static JedisPool pool = null;
    private static Logger logger = Logger.getLogger(RedisUtils.class);
    /**
     * 初始化線程池
     */
    static
    {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(MAX_ACTIVE);
        config.setMaxIdle(MAX_IDLE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setTestOnBorrow(BORROW);
        pool = new JedisPool(config, IP, PORT, TIMEOUT);
    }
    
    
    /**
     * 獲取連接
     */
    public static synchronized  Jedis getJedis()
    {
        try
        {
            if(pool != null)
            {
                return pool.getResource();
            }
            else
            {
                return null;
            }
        }
        catch (Exception e) {
            logger.info("連接池連接異常");
            return null;
        }
       
    }
    /**
     * @Description:設置失效時間
     * @param @param key
     * @param @param seconds
     * @param @return  
     * @return boolean 返回類型
     */
    public static void disableTime(String key,int seconds)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            jedis.expire(key, seconds);
            
        }
        catch (Exception e) {
            logger.debug("設置失效失敗.");
        }finally {
            getColse(jedis);
        }
    }
    
    
    /**
     * @Description:插入對象
     * @param @param key
     * @param @param obj
     * @param @return  
     * @return boolean 返回類型
     */
    public static boolean addObject(String key,Object obj)
    {
        
        Jedis jedis = null;
        String value = JSONObject.toJSONString(obj);
        try
        {
            jedis = getJedis();
            String code = jedis.set(key, value);
            if(code.equals("ok"))
            {
                return true;
            }
        }
        catch (Exception e) {
            logger.debug("插入數據有異常.");
            return false;
        }finally {
            getColse(jedis);
        }
        return false;
    }
    
    
    
    /**
     * @Description:存儲key~value
     * @param @param key
     * @param @param value  
     * @return void 返回類型
     */
    
    public static boolean addValue(String key,String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            String code = jedis.set(key, value);
            if(code.equals("ok"))
            {
                return true;
            }
        }
        catch (Exception e) {
            logger.debug("插入數據有異常.");
            return false;
        }finally {
            getColse(jedis);
        }
        return false;
    }
    /**
     * @Description:刪除key
     * @param @param key
     * @param @return  
     * @return boolean 返回類型
     */
    public static boolean delKey(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            Long code = jedis.del(key);
            if(code > 1)
            {
                return true;
            }
        }
        catch (Exception e) {
            logger.debug("刪除key異常.");
            return false;
        }finally {
            getColse(jedis);
        }
        return false;
    }
    
    /**
     * @Description: 關閉連接
     * @param @param jedis  
     * @return void 返回類型
     */
    
    public static void getColse(Jedis jedis)
    {
        if(jedis != null)
        {
            jedis.close();
        }
    }
    
}

  

 


免責聲明!

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



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