7 changed files with 178 additions and 2 deletions
@ -0,0 +1,54 @@ |
|||||
|
package cn.chjyj.szwh.configure; |
||||
|
|
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory; |
||||
|
import org.springframework.data.redis.core.*; |
||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer; |
||||
|
|
||||
|
/** |
||||
|
* Redis配置 |
||||
|
* |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class RedisConfig { |
||||
|
@Autowired |
||||
|
private RedisConnectionFactory factory; |
||||
|
|
||||
|
@Bean |
||||
|
public RedisTemplate<String, Object> redisTemplate() { |
||||
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); |
||||
|
redisTemplate.setKeySerializer(new StringRedisSerializer()); |
||||
|
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); |
||||
|
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); |
||||
|
redisTemplate.setValueSerializer(new StringRedisSerializer()); |
||||
|
redisTemplate.setConnectionFactory(factory); |
||||
|
return redisTemplate; |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
|
return redisTemplate.opsForHash(); |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) { |
||||
|
return redisTemplate.opsForValue(); |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
|
return redisTemplate.opsForList(); |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
|
return redisTemplate.opsForSet(); |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
|
return redisTemplate.opsForZSet(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package cn.chjyj.szwh.utils; |
||||
|
|
||||
|
/** |
||||
|
* Redis所有Keys |
||||
|
* |
||||
|
* @author Mark sunlightcs@gmail.com |
||||
|
*/ |
||||
|
public class RedisKeys { |
||||
|
|
||||
|
public static String getSysConfigKey(String key){ |
||||
|
return "sys:config:" + key; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,91 @@ |
|||||
|
package cn.chjyj.szwh.utils; |
||||
|
|
||||
|
import com.google.gson.Gson; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.data.redis.core.*; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
/** |
||||
|
* Redis工具类 |
||||
|
* |
||||
|
* @author Mark sunlightcs@gmail.com |
||||
|
*/ |
||||
|
@Component |
||||
|
public class RedisUtils { |
||||
|
@Autowired |
||||
|
private RedisTemplate<String, Object> redisTemplate; |
||||
|
@Autowired |
||||
|
private ValueOperations<String, String> valueOperations; |
||||
|
@Autowired |
||||
|
private HashOperations<String, String, Object> hashOperations; |
||||
|
@Autowired |
||||
|
private ListOperations<String, Object> listOperations; |
||||
|
@Autowired |
||||
|
private SetOperations<String, Object> setOperations; |
||||
|
@Autowired |
||||
|
private ZSetOperations<String, Object> zSetOperations; |
||||
|
/** 默认过期时长,单位:秒 */ |
||||
|
public final static long DEFAULT_EXPIRE = 60 * 60 * 24; |
||||
|
/** 不设置过期时长 */ |
||||
|
public final static long NOT_EXPIRE = -1; |
||||
|
private final static Gson gson = new Gson(); |
||||
|
|
||||
|
public void set(String key, Object value, long expire){ |
||||
|
valueOperations.set(key, toJson(value)); |
||||
|
if(expire != NOT_EXPIRE){ |
||||
|
redisTemplate.expire(key, expire, TimeUnit.SECONDS); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void set(String key, Object value){ |
||||
|
set(key, value, DEFAULT_EXPIRE); |
||||
|
} |
||||
|
|
||||
|
public <T> T get(String key, Class<T> clazz, long expire) { |
||||
|
String value = valueOperations.get(key); |
||||
|
if(expire != NOT_EXPIRE){ |
||||
|
redisTemplate.expire(key, expire, TimeUnit.SECONDS); |
||||
|
} |
||||
|
return value == null ? null : fromJson(value, clazz); |
||||
|
} |
||||
|
|
||||
|
public <T> T get(String key, Class<T> clazz) { |
||||
|
return get(key, clazz, NOT_EXPIRE); |
||||
|
} |
||||
|
|
||||
|
public String get(String key, long expire) { |
||||
|
String value = valueOperations.get(key); |
||||
|
if(expire != NOT_EXPIRE){ |
||||
|
redisTemplate.expire(key, expire, TimeUnit.SECONDS); |
||||
|
} |
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
public String get(String key) { |
||||
|
return get(key, NOT_EXPIRE); |
||||
|
} |
||||
|
|
||||
|
public void delete(String key) { |
||||
|
redisTemplate.delete(key); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Object转成JSON数据 |
||||
|
*/ |
||||
|
private String toJson(Object object){ |
||||
|
if(object instanceof Integer || object instanceof Long || object instanceof Float || |
||||
|
object instanceof Double || object instanceof Boolean || object instanceof String){ |
||||
|
return String.valueOf(object); |
||||
|
} |
||||
|
return gson.toJson(object); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* JSON数据,转成Object |
||||
|
*/ |
||||
|
private <T> T fromJson(String json, Class<T> clazz){ |
||||
|
return gson.fromJson(json, clazz); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue