redis工具類

dw7n 8年前發布 | 6K 次閱讀 Java Redis

pom.xml

<!-- redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.1.0</version>
            <type>jar</type>
        </dependency>

redis.propertis

#\u6700\u5927\u5206\u914d\u7684\u5bf9\u8c61\u6570
redis.pool.maxActive=1024
#\u6700\u5927\u80fd\u591f\u4fdd\u6301idel\u72b6\u6001\u7684\u5bf9\u8c61\u6570
redis.pool.maxIdle=200
#\u5f53\u6c60\u5185\u6ca1\u6709\u8fd4\u56de\u5bf9\u8c61\u65f6\uff0c\u6700\u5927\u7b49\u5f85\u65f6\u95f4
redis.pool.maxWait=10000
#\u5f53\u8c03\u7528borrow Object\u65b9\u6cd5\u65f6\uff0c\u662f\u5426\u8fdb\u884c\u6709\u6548\u6027\u68c0\u67e5
redis.pool.testOnBorrow=true
#\u5f53\u8c03\u7528return Object\u65b9\u6cd5\u65f6\uff0c\u662f\u5426\u8fdb\u884c\u6709\u6548\u6027\u68c0\u67e5
redis.pool.testOnReturn=true
redis.pool.password=3.1414926
#IP
redis.ip=192.168.0.12
#Port
redis.port=6379

java:

package com.ibm.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author 加內特
 */
public class PropUtils {

    protected static final String REDIS_RESOURCES_PROPERTIES_FILE_NAME = "redis.properties";

    /**
     * 取得所有屬性值
     * 
     * @param propertiesFileName
     * @return
     * @throws IOException
     */
    protected static Properties getProperties(String propertiesFileName) throws IOException {
        Properties prop = new Properties();
        InputStream is = PropUtils.class.getClassLoader().getResourceAsStream(propertiesFileName);

        try {
            prop.load(is);
        } catch (IOException e) {
            throw e;
        }

        return prop;
    }


    /**
     * 取得redis文件
     *
     * @return
     * @throws IOException
     */
    public static Properties getRedisResourcesProperties() {
        try {
            return getProperties(REDIS_RESOURCES_PROPERTIES_FILE_NAME);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }


    /**
     * 取得redis文件的屬性值
     *
     * @return
     * @throws IOException
     */
    public static String getRedisValue(String key) {
        Properties properties = getRedisResourcesProperties();
        return properties.getProperty(key);
    }

}
package com.ibm.common.cache;

import com.ibm.common.util.PropUtils;

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

/**
 * Redis操作接口
 *
 * @author 加內特
 */
public class RedisUtils {
    private static JedisPool pool = null;
    private static ThreadLocal<JedisPool> poolThreadLocal = new ThreadLocal<JedisPool>();

    /**
     * 構建redis連接池
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
    public static JedisPool getPool() {
        if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxActive(Integer.valueOf(PropUtils.getRedisValue("redis.pool.maxActive")));
            config.setMaxIdle(Integer.valueOf(PropUtils.getRedisValue("redis.pool.maxIdle")));
            config.setMaxWait(Long.valueOf(PropUtils.getRedisValue("redis.pool.maxWait")));
            config.setTestOnBorrow(Boolean.valueOf(PropUtils.getRedisValue("redis.pool.testOnBorrow")));
            config.setTestOnReturn(Boolean.valueOf(PropUtils.getRedisValue("redis.pool.testOnReturn")));
            // 測試環境
            // pool = new JedisPool(config, bundle.getString("redis.ip"),
            // Integer.valueOf(bundle.getString("redis.port")));
            // 線上環境
            pool = new JedisPool(config, PropUtils.getRedisValue("redis.ip"), Integer.valueOf(PropUtils.getRedisValue("redis.port")),
                    100000, PropUtils.getRedisValue("redis.pool.password"));
        }
        return pool;
    }

    public static JedisPool getConnection() {
        // ②如果poolThreadLocal沒有本線程對應的JedisPool創建一個新的JedisPool,將其保存到線程本地變量中。
        if (poolThreadLocal.get() == null) {
            pool = RedisUtils.getPool();
            poolThreadLocal.set(pool);
            return pool;
        } else {
            return poolThreadLocal.get();// ③直接返回線程本地變量
        }
    }

    /**
     * 返還到連接池
     * 
     * @param pool
     * @param redis
     */
    public static void returnResource(JedisPool pool, Jedis redis) {
        if (redis != null) {
            pool.returnResource(redis);
        }
    }

    /**
     * 獲取數據
     * 
     * @param key
     * @return
     */
    public static String get(String key) {
        String value = null;

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return value;
    }

    /**
     * 獲取數據
     * 
     * @param key
     * @return
     */
    public static byte[] get(byte[] key) {
        byte[] value = null;

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return value;
    }

    /**
     * 刪除數據
     * 
     * @param key
     * @return
     */
    public static Long del(String key) {
        Long value = null;

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return value;
    }

    /**
     * 刪除數據
     * 
     * @param key
     * @return
     */
    public static Long del(byte[] key) {
        Long value = null;

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return value;
    }

    /**
     * 判斷是否存在
     * 
     * @param key
     * @return
     */
    public static Boolean exists(String key) {
        Boolean value = null;

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return value;
    }

    /**
     * 賦值數據
     * 
     * @param key
     * @param value
     * @param expireSeconds(過期時間,秒)
     * @return value
     */
    public static Long set(String key, String value, int expireSeconds) {
        Long result = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            jedis.set(key, value);
            result = jedis.expire(key, expireSeconds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return result;
    }

    /**
     * 設置過期時間
     * 
     * @param key
     * @param expireSeconds(過期時間,秒)
     * @return value
     */
    public static Long expire(String key, int expireSeconds) {
        Long result = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            result = jedis.expire(key, expireSeconds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return result;
    }

    /**
     * 賦值數據
     * 
     * @param key
     * @return
     */
    public static String set(String key, String value) {
        String result = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            result = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return result;
    }

    /**
     * 賦值數據
     * 
     * @param key
     * @return
     */
    public static Long sadd(String key, String value) {
        Long result = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            result = jedis.sadd(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return result;
    }

    /**
     * 判斷set中是否有值
     * 
     * @param key
     * @return
     */
    public static Boolean sismember(String key, String member) {
        Boolean result = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            result = jedis.sismember(key, member);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 釋放redis對象
            pool.returnBrokenResource(jedis);
            // 返還到連接池
            returnResource(pool, jedis);
        }

        return result;
    }

}
 本文由用戶 dw7n 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!