com.github.wzc789376152.springboot.config.redis.RedisConfig Maven / Gradle / Ivy
The newest version!
package com.github.wzc789376152.springboot.config.redis;
import com.github.wzc789376152.springboot.config.redis.service.RedisPlatformService;
import jodd.util.StringUtil;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置
*
* @author jackw
*/
@Configuration
@EnableCaching
@ConditionalOnProperty(prefix = "wzc.redis", name = "enable", havingValue = "true")
@ConfigurationProperties(prefix = "wzc.redis")
public class RedisConfig extends CachingConfigurerSupport {
private Boolean enable = false;
@Value("${spring.redis.host:}")
private String host;
@Value("${spring.redis.port:}")
private Long port;
@Value("${spring.redis.password:}")
private String password;
@Autowired(required = false)
private RedisConnectionFactory redisConnectionFactory;
@Bean
public Redisson redisson() {
if (!this.enable) {
return null;
}
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port);
if (StringUtil.isNotEmpty(password)) {
config.useSingleServer().setPassword(password);
}
return (Redisson) Redisson.create(config);
}
@Bean
public RedisTemplate