一 redis 配置
package com.example.statistics.config;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import java.nio.charset.StandardCharsets;
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
/**
* 设置 redisTemplate 的序列化设置
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 1.创建 redisTemplate 模版
RedisTemplate<Object, Object> template = new RedisTemplate<>();
// 2.关联 redisConnectionFactory
template.setConnectionFactory(redisConnectionFactory);
// 3.创建 序列化类
RedisSerializer<String> keyRedisSerializer = new StringRedisSerializer();
GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class);
// RedisSerializer<Object> redisSerializer = new RedisSerializer<>() {
// @Override
// public byte[] serialize(Object o) throws SerializationException {
// return JSONObject.toJSONString(o).getBytes(StandardCharsets.UTF_8);
// }
// @Override
// public Object deserialize(byte[] bytes) throws SerializationException {
// if (bytes == null) {
// return null;
// }
// return new String(bytes, StandardCharsets.UTF_8);
// }
// };
// 6.序列化类,对象映射设置
// 7.设置 value 的转化格式和 key 的转化格式
// template.setValueSerializer(genericToStringSerializer);
template.setKeySerializer(new StringRedisSerializer());
// template.afterPropertiesSet();
template.setConnectionFactory(redisConnectionFactory);
// template.setKeySerializer(keyRedisSerializer);
template.setValueSerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new StringRedisSerializer());
return template;
}
}
二 redis 工具类
package com.example.statistics.util;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
/**
* @program: media
* @description: RedisUtil
* @author: 庄霸.liziye
* @create: 2021-12-15 10:02
**/
@Resource
private RedisTemplate redisTemplate;
// @Resource
// private RedisTemplate redisTemplates;
// =============================common============================
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean