import java.util.List; import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ziku.ms.youle.common.BasicEnums.ErrorCode; import com.ziku.ms.youle.common.BasicEnums.RedisKeyPrefixEnum; import com.ziku.ms.youle.exception.BizException; import lombok.extern.slf4j.Slf4j; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.exceptions.JedisConnectionException; @Slf4j @Component public class CacheKit { private List<JSONObject> resultList; @Autowired private JedisPool jedisPool; /** * 获取Jedis实例 */ public Jedis getJedis() { int timeoutCount = 0; while (true) { try { if (null != jedisPool) { return jedisPool.getResource(); } } catch (Exception e) { if (e instanceof JedisConnectionException) { timeoutCount++; log.warn("getJedis timeoutCount={}", timeoutCount); if (timeoutCount > 3) { break; } } else { log.warn("jedisInfo ... NumActive=" + jedisPool.getNumActive() + ", NumIdle=" + jedisPool.getNumIdle() + ", NumWaiters=" + jedisPool.getNumWaiters() + ", isClosed=" + jedisPool.isClosed()); log.error("GetJedis error,", e); break; } } break; } return null; } /** * * 获取缓存剩余有效时间 返回秒数 * * @Title getExpire * @Description TODO * @param key * @return Long */ public Long getExpire(String key) { Jedis jedis = getJedis(); int timeoutCount = 0; while (true) { if (null != jedis) { break; } else { timeoutCount++; jedis = getJedis(); if (timeoutCount > 3) { break; } } } Long value = jedis.ttl(key); returnResource(jedis); return value; } /** * * 获取缓存剩余有效时间 返回毫秒数 * * @Title getPExpire * @Description TODO * @param key * @return Long */ public Long getPExpire(String key) { Jedis jedis = getJedis(); int timeoutCount = 0; while (true) { if (null != jedis) { break; } else { timeoutCount++; jedis = getJedis(); if (timeoutCount > 3) { break; } } } Long value = jedis.pttl(key); returnResource(jedis); return value; } /** * 释放Jedis资源 * * @param jedis */ public void returnResource(Jedis jedis) { if (null != jedis) { jedis.close(); // jedisPool.returnResourceObject(jedis); } } /** * 绝对获取方法(保证一定能够使用可用的连接获取到 目标数据) Jedis连接使用后放回 * * @param key * @return */ public String safeGet(String key) { Jedis jedis = getJedis(); int timeoutCount = 0; while (true) { if (null != jedis) { break; } else { timeoutCount++; jedis = getJedis(); if (timeoutCount > 3) { break; } } } if (jedis == null) { return null; } String value = jedis.get(key); returnResource(jedis); return value; } /** * 绝对设置方法(保证一定能够使用可用的链接设置 数据) Jedis连接使用后返回连接池 * * @param key * @param time * @param value */ public void safeSet(String key, int seconds, String value) { Jedis jedis = getJedis(); int timeoutCount = 0; while (true) { if (null != jedis) { break; } else { jedis = getJedis(); timeoutCount++; if (timeoutCount > 3) { break; } } } if (jedis == null) { return; } jedis.setex(key, seconds, value); returnResource(jedis); } /** * 绝对设置方法(保证一定能够使用可用的链接设置 数据) Jedis连接使用后返回连接池 * * @param key * @param time * @param value */ public void safeSet(String key, long milliSeconds, String value) { Jedis jedis = getJedis(); int timeoutCount = 0; while (true) { if (null != jedis) { break; } else { jedis = getJedis(); timeoutCount++; if (timeoutCount > 3) { break; } } } if (jedis == null) { return; } jedis.psetex(key, milliSeconds, value); returnResource(jedis); } /** * 绝对删除方法(保证删除绝对有效) Jedis连接使用后返回连接池</span> * * @param key */ public void safeDel(String key) { Jedis jedis = getJedis(); while (true) { if (null != jedis) { break; } else { jedis = getJedis(); } } jedis.del(key); returnResource(jedis); } /** 自定义的一些 get set del 方法,方便使用 **/ public JSONObject getByCache(String key) { String result = safeGet(key); if (result != null) { return (JSONObject) JSONObject.parse(result); } return null; } public String getByCacheToString(String key) { String result = safeGet(key); if (result != null) { return result; } return null; } public List<JSONObject> getArrayByCache(String key) { String result = safeGet(key); if (result != null) { resultList = JSONArray.parseArray(result, JSONObject.class); return resultList; } return null; } public JSONArray getJSONArrayByCache(String key) { String result = safeGet(key); if (result != null) { return JSONArray.parseArray(result); } return null; } public void setByCache(String key, String s) { safeSet(key, 86400, s); } public void setByCacheOneHour(String key, String s) { safeSet(key, 3600, s); } public void setByCacheOneHour(String key, List<JSONObject> json) { safeSet(key, 86400, JSONObject.toJSONString(json)); resultList = json; } public void setByCache(String key, JSONObject json) { safeSet(key, 86400, JSONObject.toJSONString(json)); } public void setByCache(String key, List<JSONObject> list) { safeSet(key, 86400, JSONObject.toJSONString(list)); resultList = list; } public void setByCache(String key, JSONArray array) { safeSet(key, 86400, JSONArray.toJSONString(array)); } public void setByCacheCusTime(String key, String s, int time) { safeSet(key, time, s); } public void delByCache(String key) { if (null != safeGet(key)) { safeDel(key); } } // public JSONObject toJSON(DBObject db) { // return (JSONObject) JSONObject.toJSON(db); // } // // public List<JSONObject> toJSON(List<DBObject> list) { // List<JSONObject> json = new ArrayList<>(); // for (DBObject aList : list) { // json.add((JSONObject) JSONObject.toJSON(aList)); // } // return json; // } public boolean notNull() { return resultList != null && resultList.size() > 0; } public List<JSONObject> getResult() { return resultList; } public Set<String> keys(String key) { Jedis jedis = getJedis(); int timeoutCount = 0; while (true) { if (null != jedis) { break; } else { timeoutCount++; jedis = getJedis(); if (timeoutCount > 3) { break; } } } if (jedis == null) { return null; } Set<String> value = jedis.keys(key + "*"); returnResource(jedis); return value; } /** * * @Title generateOperationLock * @Description TODO 用户操作重复提交锁 * @param redisKeyPrefixEnum * @param suffix * @param milliSeconds * @return void */ public void generateOperationLock(RedisKeyPrefixEnum redisKeyPrefixEnum, String suffix, Long milliSeconds) { if (milliSeconds == null) { milliSeconds = (long) 500; } if (StringUtils.isEmpty(suffix)) { throw new BizException("-1", "参数错误"); } String key = redisKeyPrefixEnum + suffix; Object obj = this.safeGet(key); if (obj != null && obj.toString().equalsIgnoreCase("locked")) { throw new BizException(ErrorCode.OPERATION_FREQUENTLY); } this.safeSet(key, milliSeconds, "locked"); } /** * 获取缓存数据。<br> * 如果key不存在,则返回null * * @param redisKeyPrefixEnum * @param suffix * @return * @throws Exception */ private String getCacheData(RedisKeyPrefixEnum redisKeyPrefixEnum, String suffix) { if (StringUtils.isEmpty(suffix)) { throw new BizException("-1", "参数错误"); } String key = redisKeyPrefixEnum.getKey() + suffix; Object obj = this.safeGet(key); if (obj != null) { return obj.toString(); } return null; } /** * 生成缓存数据。<br> * 如果key存在,则不生成,直接返回 * * @param redisKeyPrefixEnum * @param suffix * @param value * @param seconds * @return * @throws Exception */ private String generateCacheData(RedisKeyPrefixEnum redisKeyPrefixEnum, String suffix, String value, Integer seconds) { if (seconds == null) { seconds = 7200; } if (StringUtils.isEmpty(suffix)) { throw new BizException("-1", "参数错误"); } String key = redisKeyPrefixEnum.getKey() + suffix; Object obj = this.safeGet(key); if (obj != null) { return obj.toString(); } this.safeSet(key, seconds, value); return value; } public String generateWechartAccessToken(RedisKeyPrefixEnum redisKeyPrefixEnum, String suffix, String value, Integer seconds) { if (seconds == null) { seconds = 7200; } return this.generateCacheData(redisKeyPrefixEnum, suffix, value, seconds); } public String getWechartAccessToken(RedisKeyPrefixEnum redisKeyPrefixEnum, String suffix) { return this.getCacheData(redisKeyPrefixEnum, suffix); } }
redis工具类
最新推荐文章于 2021-02-13 17:33:23 发布