整理redis的工具類jedis
阿新 • • 發佈:2018-11-21
最近專案中有遇到需加分散式鎖問題,記錄一下整理的Redis工具類,
含有redis儲存Object的幾種方式:1、物件序列化儲存。2、物件轉json串儲存。3、hash型別chun儲存。
以下是程式碼:
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Field; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import cn.cslp.ilea.distribute.entity.Person; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisUtil { private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class); private static final String SUCCESS_OK = "OK"; private static final Long SUCCESS_STATUS_LONG = 1L; // SET IF NOT EXIST,即當key不存在時,我們進行set操作;若key已經存在,則不做任何操作 private static final String SET_IF_NOT_EXIST = "NX"; // 給key加一個過期的設定,具體時間由第五個引數決定 private static final String SET_WITH_EXPIRE_TIME = "PX"; // jedis server url public static final String JEDIS_HOST_URL = PropertyUtil.getPropertyByName("JEDIS_HOST_URL"); // jedis server port public static final int JEDIS_HOST_PORT = Integer.parseInt(PropertyUtil.getPropertyByName("JEDIS_HOST_PORT")); // jedis server paw public static final String JEDIS_HOST_PASSWD = PropertyUtil.getPropertyByName("JEDIS_HOST_PASSWD"); // jedis max active connection public static final int JEDIS_MAX_ACTIVE = Integer.parseInt(PropertyUtil.getPropertyByName("JEDIS_MAX_ACTIVE")); // jedis max idle comnnection public static final int JEDIS_MAX_IDLE = Integer.parseInt(PropertyUtil.getPropertyByName("JEDIS_MAX_IDLE")); // jedis pool public static JedisPool pool = null; public static JedisPool getJedisPool() { if (pool == null) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(JEDIS_MAX_IDLE); config.setTestOnBorrow(true); pool = new JedisPool(config, JEDIS_HOST_URL, JEDIS_HOST_PORT, 0, JEDIS_HOST_PASSWD); } return pool; } /** * 字串set * * @param key * @param value * @return */ public static boolean set(String key, String value) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } String status = jedis.set(key, value); if (SUCCESS_OK.equalsIgnoreCase(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis set 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 字串set * * @param key * @param value * @param seconds * 單位秒,大於0 * @return */ public static boolean set(String key, String value, int seconds) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } String status = jedis.setex(key, seconds, value); if (SUCCESS_OK.equalsIgnoreCase(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis set 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 是否存在key * * @param key * @return */ public static boolean isExists(String key) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } return jedis.exists(key); } catch (Exception e) { LOGGER.error("redis isExists 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 字串 刪除 * * @param key * @return */ public static boolean del(String key) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } Long status = jedis.del(key); if (SUCCESS_STATUS_LONG == status) { ret = true; } } catch (Exception e) { LOGGER.error("redis del 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 字串獲取 * * @param key * @return */ public static String get(String key) { String ret = null; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } ret = jedis.get(key); } catch (Exception e) { LOGGER.error("redis get 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 獲取分散式鎖 * * @param lockKey * key為鎖 * @param requestId * 加鎖請求 * @param expireTime * key的過期時間 * @return */ public static boolean getDistributedLock(String lockKey, String requestId, int expireTime) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } String status = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime); if (SUCCESS_OK.equalsIgnoreCase(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis 獲取分散式鎖 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 釋放分散式鎖 * * @param lockKey * @param requestId */ public static boolean releaseDistributedLock(String lockKey, String requestId) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } /* * 其他請求誤解鎖問題 if(requestId.equals(jedis.get(lockKey))) { jedis.del(lockKey); } */ String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object status = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId)); if (SUCCESS_STATUS_LONG.equals(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis 釋放分散式鎖 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 序列化存入物件 * * @param key * @param obj * @return */ public static boolean set(byte[] key, Object obj) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); String status = jedis.set(key, baos.toByteArray()); if (SUCCESS_OK.equalsIgnoreCase(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis set 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 取序列化物件 * * @param key * @return */ public static Object getObj(byte[] key) { Object ret = null; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } byte[] rets = jedis.get(key); ByteArrayInputStream bais = new ByteArrayInputStream(rets); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { LOGGER.error("redis get 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * hash資料型別儲存物件 * * @param key * @param obj * @return */ public static boolean setHm(String key, Object obj) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } Map<String, String> hash = objToMap(obj); String status = jedis.hmset(key, hash); if (SUCCESS_OK.equalsIgnoreCase(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis setHm 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 修改物件屬性 * * @param key * @param field * @param value * @return */ public static boolean setHm(String key, String field, String value) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } Long status = jedis.hset(key, field, value); if (0L == status) { ret = true; } } catch (Exception e) { LOGGER.error("redis setHm 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 根據fields 查詢key物件屬性列表 * * @param key * @param fields * @return */ public static List<String> getHm(String key, String... fields) { List<String> ret = null; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return null; } ret = jedis.hmget(key, fields); } catch (Exception e) { LOGGER.error("redis getHm 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 根據field 查詢key物件屬性 * * @param key * @param fields * @return */ public static String getHm(String key, String field) { String ret = null; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return null; } ret = jedis.hget(key, field); } catch (Exception e) { LOGGER.error("redis getHm 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * 根據key查詢物件 * * @param key * @param fields * @return */ public static Object getHm(String key, Class clazz) { Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return null; } Map<String, String> map = jedis.hgetAll(key); return mapToObj(map, clazz); } catch (Exception e) { LOGGER.error("redis getHm 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return null; } /** * json格式存物件 * @param key * @param obj * @return */ public static boolean setJson(String key,Object obj) { boolean ret = false; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } ObjectMapper mapper = new ObjectMapper(); String status = jedis.set(key, mapper.writeValueAsString(obj)); if (SUCCESS_OK.equalsIgnoreCase(status)) { ret = true; } } catch (Exception e) { LOGGER.error("redis setJson 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } /** * json格式取物件 * @param key * @param clazz * @return */ public static Object getJson(String key,Class clazz) { Object ret = null; Jedis jedis = null; try { jedis = getJedisPool().getResource(); if (jedis == null) { return ret; } String str = jedis.get(key); ObjectMapper mapper = new ObjectMapper(); ret = mapper.readValue(str, clazz); } catch (Exception e) { LOGGER.error("redis getJson 出錯", e); pool.returnBrokenResource(jedis); } finally { if (null != jedis) { pool.returnResource(jedis); } } return ret; } private static HashMap<String, String> objToMap(Object obj) { if(null == obj) { return null; } HashMap<String, String> map = new HashMap<String, String>(); Field[] fields = obj.getClass().getDeclaredFields(); try { for (int i = 0; i < fields.length; i++) { String varName = fields[i].getName(); boolean accessFlag = fields[i].isAccessible(); fields[i].setAccessible(true); Object o = fields[i].get(obj); if (o != null) { map.put(varName, o.toString()); } fields[i].setAccessible(accessFlag); } } catch (Exception e) { } return map; } private static Object mapToObj(Map<String, String> map, Class clazz) { if(null == map) { return null; } try { Object obj = clazz.newInstance(); Field[] fields = obj.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { String varName = fields[i].getName(); String type = fields[i].getGenericType().getTypeName(); Object value = null; switch(type) { case "java.lang.Integer" : value = Integer.valueOf(map.get(varName)); break; case "java.lang.Long" : value = Long.valueOf(map.get(varName)); break; default: value = map.get(varName); } fields[i].setAccessible(true); fields[i].set(obj, value); } return obj; } catch (Exception e) { e.printStackTrace(); } return null; } }
讀取配置檔案的工具類PropertyUtil
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertyUtil { private static final String propertyPath = "config.properties"; public static String getPropertyByName(String name) { return getPropertyByName(propertyPath, name); } //path: WEB-INF/classes // 讀取配置檔案資訊 private static String getPropertyByName(String path, String name) { String result = ""; InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(path); Properties prop = new Properties(); try { prop.load(in); result = prop.getProperty(name).trim(); } catch (IOException e) { e.printStackTrace(); } return result; } }
分散式鎖參照部落格Redis分散式鎖的正確實現方式 ,寫的很詳細。