Browse Source

redisservice

master
zhizhi wu 5 years ago
parent
commit
0610afe385
  1. 23
      util/src/main/java/com/ccsens/util/WebConstant.java
  2. 150
      util/src/main/java/com/ccsens/util/redis/AbstractRedisService.java

23
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;
}
}
}

150
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 查询listzset, 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<Object>)value);
} else {
redisUtil.lSet(key, (List<Object>)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<String, Object>)value);
} else {
redisUtil.hmset(key, (Map<String, Object>)value, expireTime);
}
break;
default:
}
}
}
Loading…
Cancel
Save