1. 程式人生 > >單機版 RedisPoolUtil({基本操作封裝工具類})【一】

單機版 RedisPoolUtil({基本操作封裝工具類})【一】

<!--整合的RedisJAR-->
<!--引入jedis需的jar包-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<!--Spring整合jedis的依賴-->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.1.RELEASE</version>
</dependency>
package com.dsj.gdbd.utils.jedis;

import com.dsj.gdbd.utils.serialize.SerializingUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct; import java.util.HashSet; import java.util.Map; import java.util.Set; public class RedisPoolUtil implements InitializingBean { private static JedisPool pool = null; /** * 預設快取時間 */ private static final int DEFAULT_CACHE_SECONDS = 0;//
單位秒 設定成一個鐘 private static Logger LOGGER = Logger.getLogger(RedisPoolUtil.class); public static JedisPool getPool() { return pool; } @PostConstruct public void init() { if (pool == null) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(10000); config.setMaxIdle(1000); config.setMaxWaitMillis(1000 * 10); //在borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的; config.setTestOnBorrow(true); //new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); //pool = new JedisPool(config, "47.94.15.160", 6379, 10000, "[email protected]"); pool = new JedisPool(config, "192.168.31.206", 6379, 10000); } } public synchronized static Jedis getResource() { if (pool == null) { pool = getPool(); } return pool.getResource(); } public static void closeRedis(Jedis redis) { if (redis != null) { redis.close(); } } public static void set(String key, String value) { Jedis redis = getResource(); try { redis.set(key, value); } finally { closeRedis(redis); } } public static void set(Object key, Object value) { Jedis redis = getResource(); try { redis.set(SerializingUtil.serialize(key), SerializingUtil.serialize(value)); } finally { closeRedis(redis); } } public static String get(String key) { Jedis redis = getResource(); try { return redis.get(key); } finally { closeRedis(redis); } } /** * 根據快取鍵獲取Redis快取中的值.<br/> * * @param key 鍵.<br/> * @return Object .<br/> * @throws Exception */ public static Object get(Object key) { Jedis jedis = null; try { jedis = getResource(); byte[] obj = jedis.get(SerializingUtil.serialize(key)); return obj == null ? null : SerializingUtil.deserialize(obj); } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return null; } finally { closeRedis(jedis); } } /** * 根據快取鍵獲取Redis快取中的值.<br/> * * @param key 鍵.<br/> * @return Object .<br/> * @throws Exception */ public static Object getObj(String key) { Jedis jedis = null; try { jedis = getResource(); byte[] obj = jedis.get(SerializingUtil.serialize(key)); return obj == null ? null : SerializingUtil.deserialize(obj); } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return null; } finally { closeRedis(jedis); } } /** * 根據快取鍵獲取Redis快取中的值.<br/> * * @param key 鍵.<br/> * @return Object .<br/> * @throws Exception */ public static String getStr(String key) { Jedis jedis = null; try { jedis = getResource(); return jedis.get(key); } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return null; } finally { closeRedis(jedis); } } /** * 根據快取鍵獲取Redis快取中的值.<br/> * * @param key 鍵.<br/> * @return Object .<br/> * @throws Exception */ public static byte[] get(byte[] obj) { Jedis jedis = null; try { jedis = getResource(); return jedis.get(obj); } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return null; } finally { closeRedis(jedis); } } /** * 判斷一個key是否存在 * * @param key * @return */ public static Boolean exists(Object key) { Jedis jedis = null; Boolean result = false; try { jedis = getResource(); return jedis.exists(SerializingUtil.serialize(key)); } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return false; } finally { closeRedis(jedis); } } public static Boolean existsKey(String key) { Jedis jedis = null; Boolean result = false; try { jedis = getResource(); return jedis.exists(key); } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return false; } finally { closeRedis(jedis); } } /** * 根據快取鍵清除Redis快取中的值.<br/> * * @param keys * @return * @throws Exception */ public static Boolean del(Object... keys) { Jedis jedis = null; try { jedis = getResource(); jedis.del(SerializingUtil.serialize(keys)); return true; } catch (Exception e) { LOGGER.error("Cache刪除失敗:" + e); return false; } finally { closeRedis(jedis); } } /** * 根據快取鍵清除Redis快取中的值.<br/> * * @param keys * @return * @throws Exception */ public static Long del(String... keys) { Jedis jedis = null; try { jedis = getResource(); jedis.del(keys); return jedis.del(keys); } catch (Exception e) { LOGGER.error("Cache刪除失敗:" + e); return 0l; } finally { closeRedis(jedis); } } /** * 儲存一個物件到Redis中(快取過期時間:使用此工具類中的預設時間) . <br/> * * @param key 鍵 . <br/> * @param object 快取物件 . <br/> * @return true or false . <br/> * @throws Exception */ public static Boolean save(Object key, Object object) { return save(key, object, DEFAULT_CACHE_SECONDS); } /** * 儲存一個物件到redis中並指定過期時間 * * @param key 鍵 . <br/> * @param object 快取物件 . <br/> * @param seconds 過期時間(單位為秒).<br/> * @return true or false . */ public static Boolean save(Object key, Object object, int seconds) { Jedis jedis = null; try { jedis = getResource(); jedis.set(SerializingUtil.serialize(key), SerializingUtil.serialize(object)); jedis.expire(SerializingUtil.serialize(key), seconds); return true; } catch (Exception e) { e.printStackTrace(); LOGGER.error("Cache儲存失敗:" + e); return false; } finally { closeRedis(jedis); } } /** * 刪除Redis中的所有key * * @throws Exception */ public static void flushAll() { Jedis jedis = null; try { jedis = pool.getResource(); jedis.flushAll(); } catch (Exception e) { LOGGER.error("Cache清空失敗:" + e); } finally { closeRedis(jedis); } } /** * 獲取list * * @param <T> * @param key * @return list */ public static <T> Map<String, T> getMap(String key) throws Exception { if (getResource() == null || !getResource().exists(key.getBytes())) { return null; } byte[] in = getResource().get(key.getBytes()); return (Map<String, T>) SerializingUtil.deserialize(in); } /** * 設定 map * * @param <T> * @param key */ public static <T> void setMap(String key, Map<String, T> map) { try { getResource().set(key.getBytes(), SerializingUtil.serialize(map)); } catch (Exception e) { LOGGER.warn("Set key error : " + e); } } public static Long dbSize() { Jedis jedis = null; Long size = 0l; try { jedis = pool.getResource(); size = jedis.dbSize(); } catch (Exception e) { LOGGER.error("查詢庫異常:" + e); } finally { closeRedis(jedis); } return size; } public static Set<String> keys(String pattern) { Jedis jedis = null; try { jedis = pool.getResource(); Set<String> allKey = jedis.keys("*" + pattern + "*"); return allKey; } catch (Exception e) { LOGGER.error("Cache獲取失敗:" + e); return new HashSet<String>(); } finally { closeRedis(jedis); } } public static void flushDB() { Jedis jedis = null; try { jedis = pool.getResource(); jedis.flushDB(); } catch (Exception e) { LOGGER.error("Cache清空失敗:" + e); } finally { closeRedis(jedis); } } public static boolean setex(byte[] key, byte[] value, int expire) { Jedis redis = getResource(); try { redis.setex(key, expire, value); return true; } catch (Exception e) { LOGGER.error("儲存redis:" + e); return false; } finally { closeRedis(redis); } } public static boolean setex(String key, String value, int expire) { Jedis redis = getResource(); try { redis.setex(key, expire, value); return true; } catch (Exception e) { LOGGER.error("儲存redis:" + e); return false; } finally { closeRedis(redis); } } public static boolean setByte(byte[] key, byte[] value) { Jedis redis = getResource(); try { redis.set(key, value); return true; } catch (Exception e) { LOGGER.error("儲存redis:" + e); return false; } finally { closeRedis(redis); } } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub } }