From 0610afe385dcfef72747b7cde2b1f0b9c61d7d02 Mon Sep 17 00:00:00 2001 From: zhizhi wu <2377881365@qq.com> Date: Thu, 3 Sep 2020 09:34:15 +0800 Subject: [PATCH] redisservice --- .../java/com/ccsens/util/WebConstant.java | 23 +++ .../util/redis/AbstractRedisService.java | 150 ++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 util/src/main/java/com/ccsens/util/redis/AbstractRedisService.java diff --git a/util/src/main/java/com/ccsens/util/WebConstant.java b/util/src/main/java/com/ccsens/util/WebConstant.java index 6afa7e7c..0e78721e 100644 --- a/util/src/main/java/com/ccsens/util/WebConstant.java +++ b/util/src/main/java/com/ccsens/util/WebConstant.java @@ -1001,4 +1001,27 @@ public class WebConstant { } //wbs表时间类型==================================================================/ + /** + * redis类型 + */ + public enum RedisType{ + /**redis类型**/ + STRING(0,"字符串类型"), + LIST_INDEX(1,"列表-某一个"), + LIST_RANGE(2,"列表-范围"), + SET(3,"集合"), + SORT_SET_RANGE(4,"有序集合-index"), + SORT_SET_SCORE(5,"有序集合-分数"), + HASH_ITEM(6, "哈希-某一项"), + HASH(7, "哈希-全部"), + ; + /**类型code*/ + public int type; + /**类型*/ + public String message; + RedisType(int type, String message) { + this.type = type; + this.message = message; + } + } } diff --git a/util/src/main/java/com/ccsens/util/redis/AbstractRedisService.java b/util/src/main/java/com/ccsens/util/redis/AbstractRedisService.java new file mode 100644 index 00000000..2407ebb6 --- /dev/null +++ b/util/src/main/java/com/ccsens/util/redis/AbstractRedisService.java @@ -0,0 +1,150 @@ +package com.ccsens.util.redis; + +import com.ccsens.util.RedisUtil; +import com.ccsens.util.WebConstant; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.Resource; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * redis查询 + * @author whj + */ +@Slf4j +public abstract class AbstractRedisService { + + @Resource + private RedisUtil redisUtil; + + public Object queryRedis(String key, WebConstant.RedisType type, Long expireTime, Object... indexArr) { + Object obj = queryByType(key, type, indexArr); + log.info("key:{},value:{}", key, obj); + if (!isEmpty(obj)) { + return obj; + } + synchronized (this) { + Object obj2 = queryByType(key, type, indexArr); + if (!isEmpty(obj2)) { + return obj2; + } + // 查询数据库业务 + Object o = queryDb(key); + log.info("Key:{}查询数据库的结果是:{}", key, o); + if (isEmpty(o)) { + return null; + } + saveByType(key, o, expireTime, type, indexArr); + return o; + } + } + + private boolean isEmpty(Object obj) { + if (obj == null) { + return true; + } + + if (obj instanceof Collection) { + Collection collection = (Collection)obj; + if (collection.isEmpty()) { + return true; + } + } + if (obj instanceof Map) { + Map map = (Map)obj; + return map.isEmpty(); + } + + return false; + } + + /** + * 根据redis_key 查询数据库中对应的值 + * @param key redis key + * @return value + */ + public abstract Object queryDb(String key); + + + /** + * 查询不同类型的值 + * @param key redis key + * @param type 查询redis类型 + * @param indexArr 查询list,zset, hash时需要的其他字段 + * @return value + */ + private Object queryByType(String key, WebConstant.RedisType type, Object... indexArr) { + switch (type) { + case STRING: return redisUtil.get(key); + case LIST_INDEX: return redisUtil.lGetIndex(key, (Long)indexArr[0]); + case LIST_RANGE: return redisUtil.lGet(key, (Long)indexArr[0], (Long)indexArr[1]); + case SET: return redisUtil.sGet(key); + case SORT_SET_RANGE: return redisUtil.zsGet(key, (Long)indexArr[0], (Long)indexArr[1]); + case SORT_SET_SCORE: return redisUtil.zsGetByScore(key, (Double) indexArr[0], (Double)indexArr[1]); + case HASH_ITEM: return redisUtil.hget(key, (String)indexArr[0]); + case HASH: return redisUtil.hmget(key); + default: return null; + } + } + + /** + * 保存redis的值 + * @param key redisKey + * @param value value + * @param expireTime 过期时间,null为永久 + * @param type 类型 + * @param hashItem 仅hash类型时需传值,读取某一个item时使用 + * + */ + private void saveByType(String key, Object value, Long expireTime, WebConstant.RedisType type, Object... hashItem) { + switch (type) { + case STRING: + if (expireTime == null) { + redisUtil.set(key, value); + } else { + redisUtil.set(key, value, expireTime); + } + break; + case LIST_INDEX: + if (expireTime == null) { + redisUtil.lSet(key, value); + } else { + redisUtil.lSet(key, value, expireTime); + } + break; + case LIST_RANGE: + if (expireTime == null) { + redisUtil.lSet(key, (List)value); + } else { + redisUtil.lSet(key, (List)value, expireTime); + } + break; + case SET: + case SORT_SET_RANGE: + case SORT_SET_SCORE: + if (expireTime == null) { + redisUtil.sSet(key, value); + } else { + redisUtil.sSet(key, value, expireTime); + } + break; + case HASH_ITEM: + if (expireTime == null) { + redisUtil.hset(key, (String)hashItem[0], value); + } else { + redisUtil.hset(key, (String)hashItem[0], value, expireTime); + } + break; + case HASH: + if (expireTime == null) { + redisUtil.hmset(key, (Map)value); + } else { + redisUtil.hmset(key, (Map)value, expireTime); + } + break; + default: + } + } +}