Redis推荐配置

要是能支持表格就好了。

jedis配置
对象检测参数信息
  1. 代码建议配置
@Configuration
@Data
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {

    @Autowired
    private RedisProperties properties;

    @Bean
    public RedisConnectionFactory getRedisConnectionFactory(RedisProperties properties) {
        JedisPoolConfig poolConfig = (properties.getPool() != null)
                ? jedisPoolConfig(properties) : new JedisPoolConfig();

        //是否在空闲资源监测时通过ping命令监测连接有效性,无效连接将被销毁。
        poolConfig.setTestWhileIdle(true);
        //空闲资源的检测周期(单位为毫秒)
        poolConfig.setMinEvictableIdleTimeMillis(60000);
        //资源池中资源的最小空闲时间(单位为毫秒),达到此值后空闲资源将被移除。
        poolConfig.setTimeBetweenEvictionRunsMillis(30000);
        //做空闲资源检测时,每次检测资源的个数。
        poolConfig.setNumTestsPerEvictionRun(-1);

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
        jedisConnectionFactory.setHostName(properties.getHost());
        jedisConnectionFactory.setPort(properties.getPort());
        if (properties.getPassword() != null) {
            jedisConnectionFactory.setPassword(properties.getPassword());
        }
        return jedisConnectionFactory;
    }

    private JedisPoolConfig jedisPoolConfig(RedisProperties properties) {
        JedisPoolConfig config = new JedisPoolConfig();
        RedisProperties.Pool props = properties.getPool();
        config.setMaxTotal(props.getMaxActive());
        config.setMaxIdle(props.getMaxIdle());
        config.setMinIdle(props.getMinIdle());
        config.setMaxWaitMillis(props.getMaxWait());
        return config;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

你可能感兴趣的:(Redis推荐配置)