zoukankan      html  css  js  c++  java
  • redis 做默认缓存(2)

    生产使用:

     依赖:

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

    application:

    spring.cache.type=REDIS
    spring.redis.database=0
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    #spring.redis.password=root
    #springboot2.x  need unit   ,db connect time out 
    spring.redis.timeout=60s
    spring.redis.jedis.pool.max-active=-1
    spring.redis.jedis.pool.max-wait=-1s
    spring.redis.jedis.pool.max-idle=300
    spring.redis.jedis.pool.min-idle=5

    config: (个人比较推荐的配置)

    package com.icil.elsa.subscribe.milestone.common.config;
    
    
    import java.time.Duration;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.interceptor.KeyGenerator;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.cache.RedisCacheWriter;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /***
     * *************************************************************************
     * <PRE>
     *  @ClassName:    : RedisConfig 
     *
     *  @Description:    : 使用 redis  做默认缓存
     *
     *  @Creation Date   : 25 Feb 2019 3:54:19 PM
     *
     *  @Author          :  Sea
     *
     * </PRE>
     **************************************************************************
     */
    @SuppressWarnings("all")
    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {
    
         @Bean
            public KeyGenerator simpleKeyGenerator() {
                return (o, method, objects) -> {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(o.getClass().getSimpleName());
                    stringBuilder.append(".");
                    stringBuilder.append(method.getName());
                    stringBuilder.append("[");
                    for (Object obj : objects) {
                        stringBuilder.append(obj.toString());
                    }
                    stringBuilder.append("]");
    
                    return stringBuilder.toString();
                };
            }
    
            
            
           @Bean
            public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
                return new RedisCacheManager(
                    RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
                    this.getRedisCacheConfigurationWithTtl(5), // 默认策略,未配置的 key 会使用这个
                    this.getRedisCacheConfigurationMap() // 指定 key 策略
                );
            }
           
                /**
                 * @how to use   eg:
                  @Cacheable(value = "MIN10", keyGenerator = "simpleKeyGenerator") //10min
                  @Cacheable(value = "MIN30", keyGenerator = "simpleKeyGenerator") // 30min
                  @Cacheable(value = "MIN60", keyGenerator = "simpleKeyGenerator") // 60min,未指定的key,使用默认策略
                     ****/      
           private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
                Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
                redisCacheConfigurationMap.put("MIN10", this.getRedisCacheConfigurationWithTtl(10));
                redisCacheConfigurationMap.put("MIN30", this.getRedisCacheConfigurationWithTtl(30));
                redisCacheConfigurationMap.put("MIN60", this.getRedisCacheConfigurationWithTtl(60));
                return redisCacheConfigurationMap;
            }
           
           
           /**
            * @param Minute
            * @return
            */
           private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer Minute) {
                Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
                ObjectMapper om = new ObjectMapper();
                om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
                om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
                jackson2JsonRedisSerializer.setObjectMapper(om);
    
                RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
                redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                    RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(jackson2JsonRedisSerializer)
                ).entryTtl(Duration.ofMinutes(Minute));
    
                return redisCacheConfiguration;
            }
           
           
    }

    使用:

        
        @Cacheable(value = "MIN30", keyGenerator = "simpleKeyGenerator") // 30min
        public Map<String, String> getCountry3CodeAndNameMap() {
            log.info("^^^^^enter into the CountryAndLocationCacheHandler class-->getCountryCodeAndNameMap method ^^^^^^");
          ....return country3CodeAndNameMap;
        }
  • 相关阅读:
    ionic -v2版本项目结构
    Ionic -v1初始项目结构
    Ionic创建混合App(一)
    basename函数不能获取url路径中文文件名的问题
    TP框架中的A方法和R方法
    TP框架中的M、D、C、I、A、S方法
    TP中如何去掉index.php
    利用vsftpd在Linux构建安全的FTP服务
    linux上使用netstat查看当前服务和监听端口
    juniper防火墙清空配置恢复出厂设置命令
  • 原文地址:https://www.cnblogs.com/lshan/p/11531711.html
Copyright © 2011-2022 走看看