redis 做默认缓存(2)

生产使用:

 依赖:

    
            org.springframework.boot
            spring-boot-starter-data-redis
        

 

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;

/***
 * *************************************************************************
 * 
 *  @ClassName:    : RedisConfig 
 *
 *  @Description:    : 使用 redis  做默认缓存
 *
 *  @Creation Date   : 25 Feb 2019 3:54:19 PM
 *
 *  @Author          :  Sea
 *
 * 
**************************************************************************
*/ @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 getRedisCacheConfigurationMap() { Map 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 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 getCountry3CodeAndNameMap() {
        log.info("^^^^^enter into the CountryAndLocationCacheHandler class-->getCountryCodeAndNameMap method ^^^^^^");
      ....return country3CodeAndNameMap;
    }

 

转载于:https://www.cnblogs.com/lshan/p/11531711.html

你可能感兴趣的:(redis 做默认缓存(2))