pom.xml 依赖包:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
application.properties
配置如下:
# ############## redis config##########
redis.host= xx.xx.xx.xx
redis.port=6579
redis.password=xxxx
redis.master=xx
redis.host.proxy=xxxx
redis.port.proxy=6579
redis.password.proxy=xxxx
#redis cluster
redis.cache.clusterNodes=xxx:xxx,xxx:xxx,xxx:xxx
redis.cache.password=xxx
redis.cache.commandTimeout=1000
redis.cache.soTimeout=3000
redis.cache.maxAttempts=5
redis.cache.expireSeconds=120
redis.pool.maxActive=30
redis.pool.maxIdle=10
redis.pool.maxTotal=1024
redis.pool.maxWaitMillis=2000
redis.pool.testOnBorrow=false
# ############## redis config end##########
代码如下:
@ConfigurationProperties(prefix = “redis”) 表示读取 以redis 开头的配置。
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.*;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
@Configuration
@ConfigurationProperties(prefix = "redis")
@Slf4j
public class RedisProxyConfig {
@Value("${redis.host.proxy}")
private String host;
@Value("${redis.port.proxy}")
private int port;
@Value("${redis.password.proxy}")
private String password;
@Value("${redis.pool.maxActive}")
private int maxActive;
@Value("${redis.pool.maxIdle}")
private int maxIdle;
@Value("${redis.pool.maxTotal}")
private int maxTotal;
@Value("${redis.pool.maxWaitMillis}")
private int maxWaitMillis;
@Value("${redis.pool.testOnBorrow}")
private boolean testOnBorrow;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jc = new JedisPoolConfig();
jc.setMaxTotal(maxTotal);
jc.setMaxIdle(maxIdle);
jc.setMaxWaitMillis(maxWaitMillis);
jc.setTestOnBorrow(testOnBorrow);
return jc;
}
@Bean
@Autowired
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
log.info("RedisConfig jedisConnectionFactory Proxy Bean!");
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(host);
factory.setPort(port);
if (StringUtils.isNotEmpty(password)) {
factory.setPassword(password);
}
factory.setPoolConfig(jedisPoolConfig);
factory.setUsePool(true);
return factory;
}
@Bean
@Autowired
public StringRedisTemplate stringRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {
return new StringRedisTemplate(jedisConnectionFactory);
}
@Bean
public RedisTemplate redisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
RedisSerializer<Object> jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
return redisTemplate;
}
@Bean
@Primary
CacheManager cacheManager(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {
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);
RedisCacheManager redisCacheManager = RedisCacheManager.builder(jedisConnectionFactory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.entryTtl(Duration.ofMinutes(10)))
.build();
// redisCacheManager.setUsePrefix(true);
// redisCacheManager.setDefaultExpiration(600);
return redisCacheManager;
}
/**
* 默认1小时有效期,在cacheable注解中未指明时默认使用此cacheManager
*
* @param jedisConnectionFactory
* @return
*/
@Bean
CacheManager cacheManagerOneHour(@Autowired RedisConnectionFactory jedisConnectionFactory) {
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);
RedisCacheManager redisCacheManager = RedisCacheManager.builder(jedisConnectionFactory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
//entryTtl 表示缓存的时间
.entryTtl(Duration.ofMinutes(60)))
.build();
return redisCacheManager;
}
}
@Cacheable 注解的使用
详情见:https://blog.csdn.net/sinat_32502451/article/details/134310654