1. 程式人生 > >封裝的RedisUtil工具類

封裝的RedisUtil工具類

我們都知道redis支援5種資料結構,Jedis是redis官方首選的Java客戶端開發包,大家常用的資料儲存方式是把物件轉化為JSON字串,然後通過set儲存,取出資料的時候,先通過get取出JSON字串,然後轉化為物件,如果碰到複雜的物件就比較頭疼,下面封裝了個通用RedisUtil工具,可以實現像Memcache一樣快速的存取物件,不用擔心轉化的問題。
實現原理其實就是資料儲存之前自己手動序列化key、value,這裡使用的是java自帶的序列化工具,如果對效能要求比較高的話,推薦大家使用protostuff,號稱效能最強的序列化工具。然後通過set(final byte[] key, final byte[] value)方法儲存(Jedis繼承了BinaryJedis)。由於redis是單執行緒的,序列化寫在服務端的好處是可以增加redis的效能,增加吞吐量,直接上程式碼:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayspass.datacenter.common.util.SerializableUtil;
import com.dayspass.datacenter.common.util.StringUtils;

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

public
final class RedisUtil { private static Logger logger = LoggerFactory.getLogger(RedisUtil.class); //Redis伺服器IP private static String ADDR = "127.0.0.1"; //Redis的埠號 private static int PORT = 6379; //訪問密碼 private static String AUTH = "admin"; //可用連線例項的最大數目,預設值為8; //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis例項,則此時pool的狀態為exhausted(耗盡)。
private static int MAX_ACTIVE = 1024; //控制一個pool最多有多少個狀態為idle(空閒的)的jedis例項,預設值也是8。 private static int MAX_IDLE = 200; //等待可用連線的最大時間,單位毫秒,預設值為-1,表示永不超時。如果超過等待時間,則直接丟擲JedisConnectionException; private static int MAX_WAIT = 10000; private static int TIMEOUT = 10000; //在borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的; private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null; /** * 初始化Redis連線池 */ static { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取Jedis例項 * @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } /** * 釋放jedis資源 * @param jedis */ public static void returnResource(final Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } /** * 獲取redis鍵值-object * * @param key * @return */ public Object getObject(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); byte[] bytes = jedis.get(key.getBytes()); if(!StringUtils.isEmpty(bytes)) { return SerializableUtil.unserializable(bytes); } } catch (Exception e) { logger.error("getObject獲取redis鍵值異常:key=" + key + " cause:" + e.getMessage()); } finally { jedis.close(); } return null; } /** * 設定redis鍵值-object * @param key * @param value * @param expiretime * @return */ public String setObject(String key, Object value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.set(key.getBytes(), SerializableUtil.serializable(value)); } catch (Exception e) { logger.error("setObject設定redis鍵值異常:key=" + key + " value=" + value + " cause:" + e.getMessage()); return null; } finally { if(jedis != null) { jedis.close(); } } } public String setObject(String key, Object value,int expiretime) { String result = ""; Jedis jedis = null; try { jedis = jedisPool.getResource(); result = jedis.set(key.getBytes(), SerializableUtil.serializable(value)); if(result.equals("OK")) { jedis.expire(key.getBytes(), expiretime); } return result; } catch (Exception e) { logger.error("setObject設定redis鍵值異常:key=" + key + " value=" + value + " cause:" + e.getMessage()); } finally { if(jedis != null) { jedis.close(); } } return result; } /** * 刪除key */ public Long delkeyObject(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.del(key.getBytes()); }catch(Exception e) { e.printStackTrace(); return null; }finally{ if(jedis != null) { jedis.close(); } } } public Boolean existsObject(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.exists(key.getBytes()); }catch(Exception e) { e.printStackTrace(); return null; }finally{ if(jedis != null) { jedis.close(); } } } }