spring boot RedisTemplateHelper
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisTemplateHelper {
/** 初始化 */
private static RedisTemplateHelper redisTemplateHelper;
@Resource
private RedisTemplate<String,Object> stringObjectRedisTemplate;
@PostConstruct
public void init() {
redisTemplateHelper = this;
}
@Bean(name="stringObjectRedisTemplate")
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String,Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 值采用json序列化
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
/**
* 写入缓存
* @param key
* @param value
* @return
*/
public static boolean set(String key, Object value) {
boolean result = false;
try {
ValueOperations<String, Object> operations = redisTemplateHelper.stringObjectRedisTemplate.opsForValue();
operations.set(key,value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
* @param key
* @param value
* @return
*/
public static boolean set(String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<String, Object> operations = redisTemplateHelper.stringObjectRedisTemplate.opsForValue();
operations.set(key, value);
redisTemplateHelper.stringObjectRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量删除对应的value
* @param keys
*/
public static void remove(String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
* @param pattern
*/
public static void removePattern(String pattern) {
Set<String> keys = redisTemplateHelper.stringObjectRedisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplateHelper.stringObjectRedisTemplate.delete(keys);
}
/**
* 删除对应的value
* @param key
*/
public static void remove(String key) {
if (exists(key)) {
redisTemplateHelper.stringObjectRedisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
* @param key
* @return
*/
public static boolean exists(String key) {
return redisTemplateHelper.stringObjectRedisTemplate.hasKey(key);
}
/**
* 读取缓存
* @param key
* @return
*/
public static Object get(String key) {
Object result = null;
ValueOperations<String, Object> operations = redisTemplateHelper.stringObjectRedisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 哈希 添加
* @param key
* @param hashKey
* @param value
*/
public static void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplateHelper.stringObjectRedisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* 哈希获取数据
* @param key
* @param hashKey
* @return
*/
public static Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplateHelper.stringObjectRedisTemplate.opsForHash();
return hash.get(key,hashKey);
}
/**
* 列表添加
* @param k
* @param v
*/
public static void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplateHelper.stringObjectRedisTemplate.opsForList();
list.rightPush(k,v);
}
/**
* 列表删除
*/
public static void lrem(String k,Object v){
ListOperations<String, Object> list = redisTemplateHelper.stringObjectRedisTemplate.opsForList();
list.remove(k, 1, v);
}
/**
* 列表获取
* @param k
* @param l
* @param l1
* @return
*/
public static List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplateHelper.stringObjectRedisTemplate.opsForList();
return list.range(k,l,l1);
}
/**
* 集合添加
* @param key
* @param value
*/
public static void add(String key,Object value){
SetOperations<String, Object> set = redisTemplateHelper.stringObjectRedisTemplate.opsForSet();
set.add(key,value);
}
/**
* 集合获取
* @param key
* @return
*/
public static Set<Object> setMembers(String key){
SetOperations<String, Object> set = redisTemplateHelper.stringObjectRedisTemplate.opsForSet();
return set.members(key);
}
/**
* 有序集合添加
* @param key
* @param value
* @param scoure
*/
public static void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplateHelper.stringObjectRedisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* 有序集合获取
* @param key
* @param scoure
* @param scoure1
* @return
*/
public static Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplateHelper.stringObjectRedisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/243915.html