private Logger logger = LoggerFactory.getLogger(this.getClass());
private static Logger cacheLogger = LoggerFactory.getLogger("unionCachePerf4j");
private final static String EMPTY_VALUE = "null";
@Autowired
@Qualifier("union_template")
private RedisTemplate redisTemplate;
private ValueOperations<String, String> stringOperations;
private HashOperations hashOperations;
private ListOperations listOperations;
@PostConstruct
public void init() {
stringOperations = redisTemplate.opsForValue();
hashOperations = redisTemplate.opsForHash();
listOperations = redisTemplate.opsForList();
}
/**
* 缓存存入空值
*
* @param key
* @param time
* @param timeUnit
*/
public void writeEmpty(String key, long time, TimeUnit timeUnit) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
logger.debug("返回值为null,缓存空值,key={},time={},timeUnit={}", key, time, timeUnit);
stringOperations.set(key, "", time, timeUnit);
logger.debug("空值写入成功,key={}", key);
stopWatch.lap("redis.writeEmpty");
}
/**
* 写入缓存
*
* @param key
* @param value
* @param time
* @param timeUnit
*/
public void writeCache(String key, String value, long time, TimeUnit timeUnit) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
logger.debug("写入缓存,key={},value={},time={},timeUnit={}", key, value, time, timeUnit);
stringOperations.set(key, value, time, timeUnit);
logger.debug("缓存写入成功,key={}", key);
stopWatch.lap("redis.writeCache");
}
/**
* 获取缓存值
*
* @return
*/
public String getValue(String key) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
String value = stringOperations.get(key);
stopWatch.lap("redis.getValue");
return value;
}
/**
* 存入hash
*
* @param key
* @param value
* @param <T>
*/
public <T> void writeHash(String key, T value) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
Map<String, ?> mappedHash = null;
try {
mappedHash = ObjectUtil.convertBean(value);
stopWatch.lap("redis.writeHash.convertHashToMap");
} catch (Exception e) {
e.printStackTrace();
}
hashOperations.putAll(key, mappedHash);
stopWatch.lap("redis.writeHash");
}
/**
* 缓存hash空值
*
* @param key
* @param time
* @param timeUnit
*/
public void writeHashEmpty(String key, long time, TimeUnit timeUnit) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
logger.debug("返回值为null,缓存空值,key={},time={},timeUnit={}", key, time, timeUnit);
hashOperations.put(key, EMPTY_VALUE, EMPTY_VALUE);
this.setHashExpireTime(key, time, timeUnit);
logger.debug("空值写入成功,key={}", key);
stopWatch.lap("redis.writeHashEmpty");
}
/**
* 读取hash
*
* @param key
* @param beanClass
* @param <T>
* @return
*/
public <T> T loadHash(String key, Class<T> beanClass) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
Map<String, ?> loadedHash = hashOperations.entries(key);
if (loadedHash.isEmpty() || (loadedHash.containsKey(EMPTY_VALUE) && loadedHash.containsValue(EMPTY_VALUE))) {
return null;
}
Object obj = null;
try {
obj = ObjectUtil.convertMap(beanClass, loadedHash);
stopWatch.lap("redis.loadHash.convertHashToObject");
} catch (Exception e) {
e.printStackTrace();
}
stopWatch.lap("redis.loadHash");
return (T) obj;
}
/**
* 设置hash类型key的过期时间
*
* @param key
* @param timeOut
* @param timeUnit
* @return
*/
public Boolean setHashExpireTime(String key, long timeOut, TimeUnit timeUnit) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
Boolean result = redisTemplate.boundHashOps(key).expire(timeOut, timeUnit);
stopWatch.lap("redis.setHashExpireTime");
return result;
}
/**
* 写入list
*
* @param key
* @param value
* @param <T>
* @return
*/
public <T> Long writeList(String key, List<T> value) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
if (!CollectionUtils.isEmpty(value)) {
return listOperations.leftPushAll(key, value);
}
stopWatch.lap("redis.writeList");
return 0L;
}
/**
* 获取List
*
* @param key
* @param start
* @param end
*/
public List loadList(String key, long start, long end) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
List list = listOperations.range(key, start, end);
stopWatch.lap("redis.loadList");
return list;
}
/**
* 获取所有列表
*
* @param key
* @return
*/
public List loadListAll(String key) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
Long size = listOperations.size(key);
List list = listOperations.range(key, 0, size);
stopWatch.lap("redis.loadListAll");
return list;
}
/**
* 设置list过期时间
*
* @param key
* @param timeOut
* @param timeUnit
* @return
*/
public Boolean setListExpireTime(String key, int timeOut, TimeUnit timeUnit) {
StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
Boolean result = redisTemplate.boundListOps(key).expire(timeOut, timeUnit);
stopWatch.lap("redis.setListExpireTime");
return result;
}
spring data redis操作redis工具类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...