1. 程式人生 > >redis連線相關基礎工具類

redis連線相關基礎工具類

public class RedisUtil {

    private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);

    private static Map<Integer, JedisPool> pools = new ConcurrentHashMap<Integer, JedisPool>();

    static {
        JedisPoolConfig poolConfig = initPoolConfig();
        pools.put(0, new JedisPool(poolConfig, HQ_REDIS_IP, HQ_REDIS_PORT, 0, HQ_REDIS_PASSWORD, 0));
    }

    /**
     * 初始化 JedisPoolConfig
     *
     * @return
     */
    private static JedisPoolConfig initPoolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(500);
        poolConfig.setMaxIdle(5);
        poolConfig.setMaxWaitMillis(1000 * 100);
        poolConfig.setTestOnBorrow(true);

        return poolConfig;
    }

    /**
     * 初始化連線池
     *
     * @param database
     * @return
     */
    private static JedisPool initPoolMap(int database) {
        JedisPool jedisPool = null;
        JedisPoolConfig poolConfig = initPoolConfig();
        pools.put(database, new JedisPool(poolConfig, CommonConstants.HQ_REDIS_IP, CommonConstants.HQ_REDIS_PORT, 0, HQ_REDIS_PASSWORD, database));
        jedisPool = pools.get(database);

        return jedisPool;
    }

    /***
     * 切換連線池
     * @param database
     * @return
     */
    private static JedisPool select(int database) {
        // 切換
        JedisPool jedisPool = pools.get(database);
        // 不存在,初始化
        if (null == jedisPool) {
            jedisPool = initPoolMap(database);
        }
        return jedisPool;
    }

    /***
     * 返還連線池
     * @param jedis
     * @param database
     */
    @Deprecated
    private static void returnResource(Jedis jedis, int database) {
        pools.get(database).returnResource(jedis);
    }

    @Deprecated
    private static void returnBrokenResource(Jedis jedis, int database) {
        pools.get(database).returnBrokenResource(jedis);
    }

    /***
     * 關閉連線
     * @param jedis
     */
    private static void closeResource(Jedis jedis) {
        if (jedis != null)
            jedis.close();
    }

    /***
     * 從連線池獲取一個例項
     * @param database
     * @return
     */
    public static Jedis getResource(int database) {
        return select(database).getResource();
    }


    /**
     * 獲取資料
     *
     * @param key
     * @return
     */
    public static String get(String key, int market) {
        String value = null;
        Jedis jedis = null;
        try {
            jedis = getResource(market);
            value = jedis.get(key);
        } finally {
            // 返還到連線池
            closeResource(jedis);
        }

        return value;
    }

    /**
     * 設定資料
     *
     * @param key
     * @return
     */
    public static void set(String key, String value, int market) {
        Jedis jedis = null;
        try {
            jedis = getResource(market);
            jedis.set(key, value);
        } finally {
            // 返還到連線池
            closeResource(jedis);
        }
    }

    public static void main(String[] args) {
//        set("300033", "80.2", 8);
//        set("300034", "70.2", 15);
//        set("300035", "60.2", 8);
//        set("300036", "50.2", 15);
//
//        System.out.println(get("300033", 8));
//        System.out.println(get("300034", 15));
//        System.out.println(get("300035", 8));
//        System.out.println(get("300036", 19));


    }

}