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

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

<!--整合的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 redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * s
 *
 * @描述: Redis快取工具類.
 * @版本: V1.0
 
*/ public class RedisUtils { private static Logger logger = Logger.getLogger(RedisUtils.class); /** * 預設快取時間 */ private static final int DEFAULT_CACHE_SECONDS = 60 * 60 * 1;// 單位秒 設定成一個鐘 /** * 連線池 **/ private static JedisSentinelPool jedisSentinelPool;
/** * 釋放redis資源 * * @param jedis */ private static void releaseResource(Jedis jedis) { if (jedis != null) { jedisSentinelPool.returnResource(jedis); } } /** * 刪除Redis中的所有key * * @throws Exception */ public static void flushAll() { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); jedis.flushAll(); } catch (Exception e) { logger.error("Cache清空失敗:" + e); } finally { releaseResource(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 = jedisSentinelPool.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 { releaseResource(jedis); } } /** * 根據快取鍵獲取Redis快取中的值.<br/> * * @param key 鍵.<br/> * @return Object .<br/> * @throws Exception */ public static Object get(Object key) { Jedis jedis = null; try { jedis = jedisSentinelPool.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 { releaseResource(jedis); } } /** * 根據快取鍵清除Redis快取中的值.<br/> * * @param key * @return * @throws Exception */ public static Boolean del(Object key) { Jedis jedis = null; try { // System.out.println(key); jedis = jedisSentinelPool.getResource(); jedis.del(SerializingUtil.serialize(key)); return true; } catch (Exception e) { logger.error("Cache刪除失敗:" + e); return false; } finally { releaseResource(jedis); } } /** * 根據快取鍵清除Redis快取中的值.<br/> * * @param keys * @return * @throws Exception */ public static Boolean del(Object... keys) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); jedis.del(SerializingUtil.serialize(keys)); return true; } catch (Exception e) { logger.error("Cache刪除失敗:" + e); return false; } finally { releaseResource(jedis); } } /** * @param key * @param seconds 超時時間(單位為秒) * @return */ public static Boolean expire(Object key, int seconds) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); jedis.expire(SerializingUtil.serialize(key), seconds); return true; } catch (Exception e) { logger.error("Cache設定超時時間失敗:" + e); return false; } finally { releaseResource(jedis); } } /** * 新增一個內容到指定key的hash中 * * @param key * @param field * @param value * @return */ public static Boolean addHash(String key, Object field, Object value) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); jedis.hset(SerializingUtil.serialize(key), SerializingUtil.serialize(field), SerializingUtil.serialize(value)); return true; } catch (Exception e) { logger.error("Cache儲存失敗:" + e); return false; } finally { releaseResource(jedis); } } /** * 從指定hash中拿一個物件 * * @param key * @param field * @return */ public static Object getHash(Object key, Object field) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); byte[] obj = jedis.hget(SerializingUtil.serialize(key), SerializingUtil.serialize(field)); return SerializingUtil.deserialize(obj); } catch (Exception e) { logger.error("Cache讀取失敗:" + e); return null; } finally { releaseResource(jedis); } } /** * 從hash中刪除指定filed的值 * * @param key * @param field * @return */ public static Boolean delHash(Object key, Object field) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); long result = jedis.hdel(SerializingUtil.serialize(key), SerializingUtil.serialize(field)); return result == 1 ? true : false; } catch (Exception e) { logger.error("Cache刪除失敗:" + e); return null; } finally { releaseResource(jedis); } } /** * 拿到快取中所有符合pattern的key * * @param pattern * @return */ public static Set<byte[]> keys(String pattern) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); Set<byte[]> allKey = jedis.keys(("*" + pattern + "*").getBytes()); return allKey; } catch (Exception e) { logger.error("Cache獲取失敗:" + e); return new HashSet<byte[]>(); } finally { releaseResource(jedis); } } /** * 獲得hash中的所有key value * * @param key * @return */ public static Map<byte[], byte[]> getAllHash(Object key) { Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); Map<byte[], byte[]> map = jedis.hgetAll(SerializingUtil.serialize(key)); return map; } catch (Exception e) { logger.error("Cache獲取失敗:" + e); return null; } finally { releaseResource(jedis); } } /** * 判斷一個key是否存在 * * @param key * @return */ public static Boolean exists(Object key) { Jedis jedis = null; Boolean result = false; try { jedis = jedisSentinelPool.getResource(); result = jedis.exists(SerializingUtil.serialize(key)); return result; } catch (Exception e) { logger.error("Cache獲取失敗:" + e); return false; } finally { releaseResource(jedis); } } public void setJedisSentinelPool(JedisSentinelPool jedisSentinelPool) { RedisUtils.jedisSentinelPool = jedisSentinelPool; } public static JedisSentinelPool getJedisSentinelPool() { return jedisSentinelPool; } }