stringboot 集成redisTemplate并开启过期回调

本文介绍了如何在Spring Boot应用中集成RedisTemplate,并配置实现键的过期监听功能,包括引入依赖、配置文件设置、装配redisTemplate、创建工具类、装配工具类以及配置监听器。
摘要由CSDN通过智能技术生成

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值