Commit d6fb8450 by 陈文顺

简洁代码

parent 77f84d8b
package com.freemud.demo.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
......@@ -13,7 +14,7 @@ import java.util.concurrent.TimeUnit;
public abstract class RedisCache<T> {
@Autowired
protected RedisUitl redisUitl;
protected RedisTemplate redisTemplate;
private Class<T> clazz;
......@@ -32,22 +33,22 @@ public abstract class RedisCache<T> {
/**
* 获取缓存
* @param key
* @param clazz
* @return
*/
public T getCache(Object key){
String redisKey = getRedisKey(getClazz().getName(), key);
T value = redisUitl.get(redisKey);
String redisKey = getRedisKey( key);
T value = (T)redisTemplate.opsForValue().get(redisKey);
if (value != null){
return value;
}
value = searchDB(key);
if (value == null){
redisUitl.setEx(redisKey, "",5, TimeUnit.MINUTES);
redisTemplate.opsForValue().set(redisKey, "",5, TimeUnit.MINUTES);
}else {
redisUitl.setEx( redisKey,value, getExprie(), TimeUnit.HOURS);
redisTemplate.opsForValue().set( redisKey,value, getExprie(), TimeUnit.HOURS);
}
return value;
......@@ -59,14 +60,14 @@ public abstract class RedisCache<T> {
* @param value
*/
public void setCache(Object key, T value){
redisUitl.setEx( getRedisKey(value.getClass().getName(), key),value, getExprie(), TimeUnit.SECONDS);
redisTemplate.opsForValue().set( getRedisKey( key),value, getExprie(), TimeUnit.SECONDS);
}
/**
* 删除缓存
*/
public void delCache(Object key){
redisUitl.getRedisTemplate().delete(getRedisKey(getClazz().getName(),key));
redisTemplate.delete(getRedisKey( key));
}
......@@ -74,9 +75,9 @@ public abstract class RedisCache<T> {
public abstract int getExprie();
private String getRedisKey(String keyPrefix, Object key){
private String getRedisKey( Object key){
return keyPrefix+"_"+key.toString();
return getClazz().getName()+"_"+key.toString();
}
......
package com.freemud.demo.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.concurrent.TimeUnit;
//import org.apache.commons.lang3.StringUtils;
/**
* Created by chenwenshun on 2018/9/4.
*/
@Component
public class RedisUitl{
@Autowired
private RedisTemplate redisTemplate;
public RedisTemplate getRedisTemplate(){
return redisTemplate;
}
public void setEx(String key, Object value, int expire, TimeUnit timeUnit){
redisTemplate.opsForValue().set(key, value,expire,timeUnit);
}
public <T> T get(String key){
return (T)redisTemplate.opsForValue().get(key);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment