RedisConfig.java 3.51 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package com.tanpu.community.config;

import org.springframework.beans.factory.annotation.Value;
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.util.StringUtils;

import java.lang.reflect.Method;

@Configuration
@EnableCaching
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.max-active}")
    private int maxActive;
    @Value("${spring.redis.max-wait}")
    private long maxWaitMillis;
    @Value("${spring.redis.max-idle}")
    private int maxIdle;

刘基明's avatar
刘基明 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
//    @Bean
//    public JedisPoolConfig jedisPoolConfig() {
//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        // maximum connection
//        jedisPoolConfig.setMaxTotal(maxActive);
//        // Maximum wait time when no connection is available in the pool
//        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
//        // Maximum number of idle connections
//        jedisPoolConfig.setMinIdle(maxIdle);
//        // Other properties can be added by yourself
//        return jedisPoolConfig;
//    }
//
//    @Bean
//    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//        JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
//                .poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setPort(port);
//        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
//        return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
//    }
//
//    @Bean
//    public RedisTemplate<String, Object> redisTemplate() {
//        RedisTemplate<String, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
//
//        return template;
//    }
//
//    /**
//     * cache
//     */
//    @Bean
//    public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
//        return (builder) -> RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(jedisPoolConfig()))
//                .withCacheConfiguration("tempCache",
//                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
//                .withCacheConfiguration("longCache",
//                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)));
//    }
刘基明's avatar
刘基明 committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    @Bean("communityKeyGenerator")
    public KeyGenerator keyGenerator() {
        return new RedisConfig.CommunityKeyGenerator();
    }

    public static class CommunityKeyGenerator implements KeyGenerator {
        public Object generate(Object target, Method method, Object... params) {
            // todo prefix 加到common
            return "new_community_" + target.getClass().getSimpleName() + "_"
                    + method.getName() + "_"
                    + StringUtils.arrayToDelimitedString(params, "_");
        }
    }
}