1.配置
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.RedisTemplate;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis配置
*/
@Configuration
public class RedisConfig {
@Bean(name = {"anxkoRedisTemplate"})
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<String , Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
@Bean
public HashMapper<Object, String, Object> hashMapper(){
return new Jackson2HashMapper(true);
}
}
SpringContextUtil工具类:普通Java类中获取Bean工具类(SpringBoot)-CSDN博客
2.工具类
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.hash.HashMapper;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* redis缓存工具类
*/
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public class RedisCacheUtil {
public static final RedisTemplate redisTemplate = SpringContextUtil.getBean("anxkoRedisTemplate", RedisTemplate.class);
public static final HashMapper hashMapper = SpringContextUtil.getBean(HashMapper.class);
/**
* 缓存基本的对象,Integer、String、实体类等
* @param key 键
* @param value 值
*/
public static void set(Object key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 缓存基本的对象,Integer、String、实体类等
* @param key 键
* @param value 值
* @param timeout 过期时间
* @param timeUnit 时间单位
*/
public static void set(Object key, Object value, long timeout, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 获得缓存的基本对象
* @param key 键
* @return 对应的数据
*/
public static Object get(Object key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 删除键值
* @param key 键
* @return true 删除成功 false反之
*/
public static boolean delete(Object key) {
return Boolean.TRUE.equals(redisTemplate.delete(key));
}
/**
* 判断键是否存在
* @param key 键
* @return true存在 false 不存在
*/
public static boolean hasKey(Object key) {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
}
/**
* 设置过期时间
* @param key 键
* @param timeout 过期时间
* @param timeUnit 时间单位
*/
public static void expire(Object key, long timeout, TimeUnit timeUnit) {
redisTemplate.expire(key, timeout, timeUnit);
}
public static Long getExpire(Object key){
return redisTemplate.getExpire(key);
}
public static Long getExpire(Object key, TimeUnit timeUnit){
return redisTemplate.getExpire(key, timeUnit);
}
/**
* 缓存hash
* @param key redis键
* @param hashKey hash键
* @param value 值
*/
public static void setHashCache(Object key, Object hashKey, Object value){
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 缓存hash
* @param key 键
* @param value 值
*/
public static void setHashCache(Object key, Map value){
redisTemplate.opsForHash().putAll(key, value);
}
/**
* 缓存类对象hash
* @param key 键
* @param value 值
* @param <T> 类型
*/
public static <T> void setEntityHashCache(Object key, T value){
redisTemplate.opsForHash().putAll(key, hashMapper.toHash(value));
}
public static <T> void setEntityHashCache(Object key, T value, long timeout, TimeUnit timeUnit){
setEntityHashCache(key, value);
redisTemplate.expire(key, timeout, timeUnit);
}
public static <T> void setEntityHashCache(Object key, T value, Duration duration){
setEntityHashCache(key, value);
redisTemplate.expire(key, duration);
}
/**
* 获取类对象hash缓存
* @param key 键
* @param cls 类型
* @return 类对象
* @param <T> 类型
*/
public static <T> T getEntityHashCache(Object key, Class<T> cls){
return cls.cast(hashMapper.fromHash(redisTemplate.opsForHash().entries(key)));
}
/**
* 获取hash缓存
* @param key 键
* @return map
*/
public static Map<Object, Object> getHashCache(Object key){
return redisTemplate.opsForHash().entries(key);
}
/**
* 获取hash缓存
* @param key redis键
* @param hashKey hash键
* @return 对象
*/
public static Object getHashCache(Object key, Object hashKey){
return redisTemplate.opsForHash().get(key, hashKey);
}
/**
* 判断hash缓存是否存在
* @param key redis键
* @param hashKey hash键
* @return 是否存在
*/
public static boolean hasHashCache(Object key, Object hashKey){
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 删除hash缓存
* @param key redis键
* @param hashKeys 多个hash键
* @return 删除数量
*/
public static Long deleteHashCache(Object key, Object... hashKeys){
return redisTemplate.opsForHash().delete(key, hashKeys);
}
}