zoukankan      html  css  js  c++  java
  • 在spring boot中多次控制redis过期时间

    首先建立配置类,实例化2个CacheManager的bean,且实现json序列化,其中一个设为primary,,然后,在实际的service层注解中,用cachemanage引用这个两个不同的bean,,

    @Configuration
    public class RedisManager {
        
         @Bean
         @Primary
         public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
         //初始化一个RedisCacheWriter
         RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
         //设置CacheManager的值序列化方式为json序列化
         RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
         RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);
         RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                 .serializeValuesWith(pair).entryTtl(Duration.ofHours(3));
    
         //设置默认超过时期是3小时
         //初始化RedisCacheManager
         return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
         }
        
        @Bean
        public CacheProperties    cacheProperties() {
            CacheProperties cp = new CacheProperties();
            //cp.getRedis().setTimeToLive(Duration.ofHours(4));
            return cp;
        } 
        
        @Bean(name = "zhouyi")
         public RedisCacheManager cacheManager1(RedisConnectionFactory redisConnectionFactory) {
             //初始化一个RedisCacheWriter
             RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
             //设置CacheManager的值序列化方式为json序列化
             RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
             RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);
             RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                     .serializeValuesWith(pair).entryTtl(Duration.ZERO);
    
             //设置默认超过时期为永久
            
             //初始化RedisCacheManager
             return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
             }
        
    }

    在实际的操作中,通过cacheManager注解引用

    @Cacheable(cacheNames = "ZhouyiYao",key = "#rawId+':'+#bianYao",cacheManager = "zhouyi")
        public ZhouyiYao getZhaoyiYaoByZhanbu(Integer rawId, Integer bianYao) {
            return zhouyiYaoMapper.getZhaoyiYaoByZhanbu(rawId, bianYao);
        }

    实际操作,因为初始化时,yml中需要提升连接超时时间

    spring:
      redis:
        host: 127.0.0.1
        port: 6379
        connect-timeout: 15000
  • 相关阅读:
    react项目中如何解决同时需要多个请求问题
    jq+ajax+bootstrap改写一个动态分页的表格
    Window7+vs2008+QT环境搭建
    mssql charindex
    解决NTLDR is missing,系统无法启动的方法
    基于三汇语音卡的呼叫中心开发(一)
    Wince 或Windows平台 C#调用Bitmap对象后资源应该如何释放
    Anki:插件开发
    java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.ValueStack
    struts2中action之间的一种跳转
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14718200.html
Copyright © 2011-2022 走看看