SpringBoot配置Redis自定义过期时间操作

 更新时间:2021年7月28日 15:00  点击:1755

SpringBoot配置Redis自定义过期时间

Redis配置依赖

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-redis</artifactId>
        <version>1.4.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.8.1.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
</dependency>

SpringBoot-Reids配置文件

package com.regs.tms.common.redis;

@Configuration
@EnableCaching// 启用缓存,这个注解很重要
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedisCacheConfig extends CachingConfigurerSupport {
	private String host;
	private Integer port;
	private Integer database;
	private String password;

	@Bean("redisTemplate")
	public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
	    StringRedisTemplate template = new StringRedisTemplate();
	    template.setConnectionFactory(factory);
	    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
	    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
	    ObjectMapper mapper = new ObjectMapper();
	    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	    serializer.setObjectMapper(mapper);
	    template.setValueSerializer(serializer);
	    template.setHashValueSerializer(serializer);
	    // 设置键(key)的序列化采用StringRedisSerializer。
	    template.setKeySerializer(new StringRedisSerializer());
	    template.setHashKeySerializer(new StringRedisSerializer());
	    //打开事务支持
	    template.setEnableTransactionSupport(true);
	    template.afterPropertiesSet();
	    return template;
	}

	@Bean
	public PlatformTransactionManager transactionManager(DataSource dataSource) throws SQLException {
	    //配置事务管理器
	    return new DataSourceTransactionManager(dataSource);
	}

	@Bean("stringRedisTemplate")
	public StringRedisTemplate stringRedisTemplate() {
	    Integer port = this.port == null ? 6379 : this.port;
	    JedisConnectionFactory jedis = new JedisConnectionFactory();
	    jedis.setHostName(host);
	    jedis.setPort(port);
	    if (StringUtils.isNotEmpty(password)) {
	        jedis.setPassword(password);
	    }
	    if (database != null) {
	        jedis.setDatabase(database);
	    } else {
	        jedis.setDatabase(0);
	    }
	    // 初始化连接pool
	    jedis.afterPropertiesSet();
	    // 获取连接template
	    StringRedisTemplate temple = new StringRedisTemplate();
	    temple.setConnectionFactory(jedis);
	    return temple;
	}
}

自定义失效注解

package com.regs.tms.common.redis.annotation;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CacheDuration {
    //Sets the expire time (in seconds).
    public long duration() default 60;
}

自定义失效配置

package com.regs.tms.common.redis.annotation;

 /**
 * ExpireCacheManager,继承自RedisCacheManager,
 * 用于对@CacheExpire解析及有效期的设置
 */
public class RedisExpireCacheManager extends RedisCacheManager implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;

    public RedisExpireCacheManager(RedisTemplate redisTemplate) {
        super(redisTemplate);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        parseCacheExpire(applicationContext);
    }

    private void parseCacheExpire(ApplicationContext applicationContext) {
        final Map<String, Long> cacheExpires = new HashMap<>(16);
        //扫描有注解
        String[] beanNames = applicationContext.getBeanNamesForAnnotation(Cacheable.class);
        for (String beanName : beanNames) {
            final Class clazz = applicationContext.getType(beanName);
            addCacheExpires(clazz, cacheExpires);
        }
        //设置有效期
        super.setExpires(cacheExpires);
    }

    private void addCacheExpires(final Class clazz, final Map<String, Long> cacheExpires) {
        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(method);
                //根据CacheExpire注解获取时间
                CacheExpire cacheExpire = findCacheExpire(clazz, method);
                if (cacheExpire != null) {
                    Cacheable cacheable = findAnnotation(method, Cacheable.class);
                    String[] cacheNames = isEmpty(cacheable.value()) ? new String[]{} : cacheable.value();
                    for (String cacheName : cacheNames) {
                        cacheExpires.put(cacheName, cacheExpire.expire());
                    }
                }
            }
        }, new ReflectionUtils.MethodFilter() {
            @Override
            public boolean matches(Method method) {
                return null != findAnnotation(method, Cacheable.class);
            }
        });
    }

    /**
     * CacheExpire标注的有效期,优先使用方法上标注的有效期
     *
     * @param clazz
     * @param method
     * @return
     */
    private CacheExpire findCacheExpire(Class clazz, Method method) {
        CacheExpire methodCache = findAnnotation(method, CacheExpire.class);
        if (null != methodCache) {
            return methodCache;
        }
        CacheExpire classCache = findAnnotation(clazz, CacheExpire.class);
        if (null != classCache) {
            return classCache;
        }
        return null;
    }
}

spring boot 使用redis 超时时间重新设置

如果要计算每24小时的下单量,

通常的做法是,取出旧值,进行加一在设置回去,

但是这样就出现了一个问题

第二次设置值的时候,把超时时间重新设置成个24小时

这样无疑的记录24小时的数量是不准确的

并且spring boot 中,默认使用了spring 来操作redis ,使存在每个redis中的值,都会加前面加入一些东西

1) "\xac\xed\x00\x05t\x00\x0bREDISUALIST"

我们在查找每个值的时候,并不知道在key前面需要加点什么.

所以我们必须要用keys 这个命令 ,来匹配 我们需要查找的key,来取第一个

然后我们用 ttl 命令 返回指定key的剩余时间 ,重新设置回去,而不是设置24小时,这样就实现了24小时累加一次

在redisService 中,增加一个方法

/**
     * 获取指定key的剩余超时时间,key最好是唯一的,有特点的,最好不要匹配出多个 例子 *111 取出 "\xac\xed\x00\x05t\x00\x0b111"
     * 返回剩余秒数
     * @param key
     * @return
     * create by jcd
     */
    public Long ttlByKey(@NotNull String key){
        Set<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().keys(key.getBytes());
        byte[] bytes = keys.stream().findFirst().get();
        Long ttl = redisTemplate.getConnectionFactory().getConnection().ttl(bytes);
        return ttl;
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • 详解如何清理redis集群的所有数据

    这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
  • Redis连接池配置及初始化实现

    这篇文章主要介绍了Redis连接池配置及初始化实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • 详解redis desktop manager安装及连接方式

    这篇文章主要介绍了redis desktop manager安装及连接方式,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-15
  • 浅谈redis key值内存消耗以及性能影响

    这篇文章主要介绍了浅谈redis key值内存消耗以及性能影响,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-07
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • lua读取redis数据的null判断示例代码

    最近在工作中遇到了一个问题,通过查找相关资料才得知原因是因为返回结果的问题,下面这篇文章主要给大家介绍了关于lua读取redis数据的null判断的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下...2020-06-30
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • redis setIfAbsent和setnx的区别与使用说明

    这篇文章主要介绍了redis setIfAbsent和setnx的区别与使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-04
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Redis的Expire与Setex区别说明

    这篇文章主要介绍了Redis的Expire与Setex区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-15
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • 详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14
  • SpringBoot接口接收json参数解析

    这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
  • springboot中使用@Transactional注解事物不生效的坑

    这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • 查看Redis内存信息的命令

    Redis 是一个开源、高性能的Key-Value数据库,被广泛应用在服务器各种场景中。本文介绍几个查看Redis内存信息的命令,包括常用的info memory、info keyspace、bigkeys等。...2021-01-15
  • springboot多模块包扫描问题的解决方法

    这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16