2 changed files with 173 additions and 0 deletions
@ -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<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…
Reference in new issue