stringboot 集成redisTemplate并开启过期回调
1 引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.添加配置文件
# redis配置
spring.redis.host=127.0.0.1
spring.redis.timeout=3000
spring.redis.password=
spring.redis.port=6379
spring.redis.database=5
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=0
3装配redisTemplate
import com.alan.lawyer.redis.RedisUtils;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* 自动装配
*
* @author gxp
*/
@Import({
RedisUtils.class})
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
RedisSerializer<Object> json = new GenericFastJsonRedisSerializer();
redisTemplate.setValueSerializer(json);
redisTemplate.setHashValueSerializer(json);
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
4创建工具类
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Redis 工具类
*
* @author gxp
*/
@SuppressWarnings({"unchecked", "unused"})
public class RedisUtils implements InitializingBean {
@Resource
private RedisTemplate<String, Object> redisTemplate;
private static RedisTemplate<String, Object> template;
@Override
public void afterPropertiesSet() {
template = redisTemplate;
}
/* ------------------- key 相关操作--------------------- */
public static class KeyOps {
/**
* 删除key
*
* @param key /
*/
public static void delete(String key) {
template.delete(key);
}
/**
* 批量删除key
*
* @param keys /
*/
public static void delete(Collection<String> keys) {
template.delete(keys);
}
/**
* 序列化key
*
* @param key /
* @return /
*/
public static byte[] dump(String key) {
return template.dump(key);
}
/**
* 是否存在key
*
* @param key /
* @return /
*/
public static Boolean hasKey(String key) {
return template.hasKey(key);
}
/**
* 设置过期时间
*
* @param key /
* @param timeout /
* @param unit /
* @return /
*/
public static Boolean expire(String key, long timeout, TimeUnit unit) {
return template.expire(key, timeout, unit);
}
/**
* 设置过期时间
*
* @param key /
* @param date /
* @return /
*/
public static Boolean expireAt(String key, Date date) {
return template.expireAt(key, date);
}
/**
* 查找匹配的key
*
* @param pattern /
* @return /
*/
public static Set<String> keys(String pattern) {
return template.keys(pattern);
}
/**
* 将当前数据库的 key 移动到给定的数据库 db 当中
*
* @param key /
* @param dbIndex /
* @return /
*/
public static Boolean move(String key, int dbIndex) {
return template.move(key, dbIndex);
}
/**
* 移除 key 的过期时间,key 将持久保持
*
* @param key /
* @return /
*/
public static Boolean persist(String key) {
return template.persist(key);
}
/**
* 返回 key 的剩余的过期时间
*
* @param key /
* @param unit /
* @return /
*/
public static Long getExpire(String key, TimeUnit unit) {
return template.getExpire(key, unit);
}
/**
* 返回 key 的剩余的过期时间
*
* @param key /
* @return /
*/
public static Long getExpire(String key) {
return template.getExpire(key);
}
/**
* 从当前数据库中随机返回一个 key
*
* @return /
*/
public static String randomKey() {
return template.ra