|  | 
| 3 | 3 | import org.slf4j.Logger; | 
| 4 | 4 | import org.slf4j.LoggerFactory; | 
| 5 | 5 | import org.springframework.beans.factory.annotation.Autowired; | 
|  | 6 | +import org.springframework.beans.factory.annotation.Value; | 
|  | 7 | +import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer; | 
|  | 8 | +import org.springframework.cache.CacheManager; | 
|  | 9 | +import org.springframework.cache.annotation.CachingConfigurerSupport; | 
| 6 | 10 | import org.springframework.cache.annotation.EnableCaching; | 
| 7 | 11 | import org.springframework.cache.interceptor.KeyGenerator; | 
| 8 | 12 | import org.springframework.context.annotation.Bean; | 
| 9 | 13 | import org.springframework.context.annotation.Configuration; | 
|  | 14 | +import org.springframework.data.redis.cache.RedisCacheConfiguration; | 
| 10 | 15 | import org.springframework.data.redis.cache.RedisCacheManager; | 
|  | 16 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | 
|  | 17 | +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | 
|  | 18 | +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | 
|  | 19 | +import org.springframework.data.redis.core.RedisTemplate; | 
|  | 20 | +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | 
|  | 21 | +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | 
|  | 22 | +import org.springframework.data.redis.serializer.RedisSerializationContext; | 
|  | 23 | +import org.springframework.data.redis.serializer.StringRedisSerializer; | 
| 11 | 24 | 
 | 
| 12 | 25 | import java.lang.reflect.Method; | 
|  | 26 | +import java.time.Duration; | 
| 13 | 27 | import java.util.Arrays; | 
|  | 28 | +import java.util.HashMap; | 
|  | 29 | +import java.util.Map; | 
| 14 | 30 | 
 | 
| 15 | 31 | /** | 
| 16 | 32 |  * RedisCacheConfig | 
|  | 
| 21 | 37 |  */ | 
| 22 | 38 | @Configuration | 
| 23 | 39 | @EnableCaching | 
| 24 |  | -public class RedisCacheConfig { | 
|  | 40 | +public class RedisCacheConfig extends CachingConfigurerSupport { | 
| 25 | 41 |     private Logger logger = LoggerFactory.getLogger(this.getClass()); | 
| 26 | 42 | 
 | 
| 27 |  | -    /** | 
| 28 |  | -     * 重新配置RedisCacheManager | 
| 29 |  | -     */ | 
| 30 |  | -    @Autowired | 
| 31 |  | -    public void configRedisCacheManger(RedisCacheManager rd) { | 
| 32 |  | -        rd.setDefaultExpiration(100L); | 
|  | 43 | +    @Value("${spring.redis.host}") | 
|  | 44 | +    private String host; | 
|  | 45 | + | 
|  | 46 | +    @Value("${spring.redis.port}") | 
|  | 47 | +    private String port; | 
|  | 48 | + | 
|  | 49 | +    @Bean | 
|  | 50 | +    public RedisStandaloneConfiguration getRedisClient() { | 
|  | 51 | +        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, Integer.parseInt(port)); | 
|  | 52 | +        return redisStandaloneConfiguration; | 
| 33 | 53 |     } | 
| 34 | 54 | 
 | 
|  | 55 | +    @Bean | 
|  | 56 | +    public JedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration RedisStandaloneConfiguration) { | 
|  | 57 | +        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(RedisStandaloneConfiguration); | 
|  | 58 | +        return jedisConnectionFactory; | 
|  | 59 | +    } | 
|  | 60 | + | 
|  | 61 | +    @Bean | 
|  | 62 | +    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { | 
|  | 63 | +        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); | 
|  | 64 | +        redisTemplate.setConnectionFactory(cf); | 
|  | 65 | +        return redisTemplate; | 
|  | 66 | +    } | 
|  | 67 | + | 
|  | 68 | +    @Bean | 
|  | 69 | +    public RedisCacheConfiguration redisCacheConfiguration() { | 
|  | 70 | +        return RedisCacheConfiguration | 
|  | 71 | +                .defaultCacheConfig() | 
|  | 72 | +                .serializeKeysWith( | 
|  | 73 | +                        RedisSerializationContext | 
|  | 74 | +                                .SerializationPair | 
|  | 75 | +                                .fromSerializer(new StringRedisSerializer())) | 
|  | 76 | +                .serializeValuesWith( | 
|  | 77 | +                        RedisSerializationContext | 
|  | 78 | +                                .SerializationPair | 
|  | 79 | +                                .fromSerializer(new GenericJackson2JsonRedisSerializer())) | 
|  | 80 | +                .entryTtl(Duration.ofSeconds(600L)); | 
|  | 81 | +    } | 
|  | 82 | + | 
|  | 83 | +    @Bean | 
|  | 84 | +    public CacheManager cacheManager(RedisConnectionFactory cf) { | 
|  | 85 | +        //RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(cf); | 
|  | 86 | +        //RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, RedisCacheConfiguration.defaultCacheConfig()); | 
|  | 87 | +        RedisCacheManager cm = RedisCacheManager.builder(cf).cacheDefaults(redisCacheConfiguration()).build(); | 
|  | 88 | +        return cm; | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    // @Bean | 
|  | 92 | +    // public KeyGenerator keyGenerator() { | 
|  | 93 | +    //     return new KeyGenerator() { | 
|  | 94 | +    //         @Override | 
|  | 95 | +    //         public Object generate(Object o, Method method, Object... objects) { | 
|  | 96 | +    //             StringBuilder sb = new StringBuilder(); | 
|  | 97 | +    //             sb.append(o.getClass().getName()); | 
|  | 98 | +    //             sb.append(method.getName()); | 
|  | 99 | +    //             for (Object obj : objects) { | 
|  | 100 | +    //                 sb.append(obj.toString()); | 
|  | 101 | +    //             } | 
|  | 102 | +    //             return sb.toString(); | 
|  | 103 | +    //         } | 
|  | 104 | +    //     }; | 
|  | 105 | +    // } | 
|  | 106 | + | 
| 35 | 107 |     /** | 
| 36 | 108 |      * 自定义缓存key的生成类实现 | 
| 37 | 109 |      */ | 
|  | 
0 commit comments