悠然哈哈哈

导航

spring boot redis CacheManager

// 1.0
@Value("${erp.cache.expireation:32400}")  // in seconds 9小时
private long cacheExpiration;

@Value("#{${erp.cache.expires:{'activity:goods':600,'ordercount':300}}}")
private Map<String, Long> expires;

@Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); // 开启使用缓存名称最为key前缀 redisCacheManager.setUsePrefix(false); //这里可以设置一个默认的过期时间 单位是秒 redisCacheManager.setDefaultExpiration(cacheExpiration); // 设置缓存的过期时间 redisCacheManager.setExpires(expires); return redisCacheManager; }
//2.0
 @Bean
    public CacheManager 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);
        //设置默认超过期时间是600秒
        defaultCacheConfig.entryTtl(Duration.ofSeconds(cacheExpiration)); 

//初始化RedisCacheManager

return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}

来自:https://www.cnblogs.com/wjx6270/p/11568202.html
https://www.cnblogs.com/dadiwm321/p/springboot_redis.html
https://www.cnblogs.com/ffaiss/p/11435775.html

 

@Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
//spring cache注解序列化配置
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getKeySerializer())) //key序列化方式
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer())) //value序列化方式
                .disableCachingNullValues() //不缓存null值
                .entryTtl(Duration.ofSeconds(cacheExpiration)); //默认缓存过期时间

    // 对每个缓存名称应用不同的配置,自定义过期时间
        Map<String, RedisCacheConfiguration> expires=new HashMap<>();
        expires.put("activity:goods",redisCacheConfiguration.entryTtl(Duration.ofSeconds(600)));
        expires.put("ordercount",redisCacheConfiguration.entryTtl(Duration.ofSeconds(300)));

        RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisTemplate.getConnectionFactory())
                .cacheDefaults(redisCacheConfiguration)
                .transactionAware()
                .initialCacheNames(expires.keySet()) // 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置
                .withInitialCacheConfigurations(expires)
                .build();
        return redisCacheManager;
    }

来自:https://www.cnblogs.com/wjx6270/p/11568202.html
https://www.cnblogs.com/dadiwm321/p/springboot_redis.html
https://www.cnblogs.com/ffaiss/p/11435775.html
 

 

 

 

 

 @Override
    public void cacheMsgShop() {

        List<MsgUnreadCount> list=msgShopMapper.findShop();
        if(list==null||list.size()==0)return;
        logger.info("--mgs size:{}",list.size());
        final Cache cache = cacheManager.getCache(CACHE_NAME);
        if(cache == null){
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            MsgUnreadCount msg=list.get(i);
            long shopId=msg.getShopId();
            int num=msg.getNum();
            cache.put(shopId,num);
        }

    }

    @Override
    public int findByShopId(long shopId) {

        final Cache cache = cacheManager.getCache(CACHE_NAME);
        if(cache == null){
            return 0;
        }
        Cache.ValueWrapper valueWrapper=cache.get(shopId);
        if(valueWrapper==null){
            return 0;
        }
        return Integer.valueOf( valueWrapper.get().toString());
    }


 

posted on 2020-09-18 09:18  悠然886  阅读(1551)  评论(0编辑  收藏  举报