Redis
含义
redis是以k-v存储的非关系型数据库,它的数据类型有String、list、map、set、zset
RedisTemplate的配置
pom.xml
java
<!-- Redis 相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
配置类
java
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String hostName;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private int database;
// 创建 Redis 连接工厂
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(hostName);
factory.setPort(port);
factory.setPassword(password);
factory.setDatabase(database);
factory.setUsePool(true);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(30);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMinIdle(5);
jedisPoolConfig.setMaxWaitMillis(3000);
factory.setPoolConfig(jedisPoolConfig);
factory.afterPropertiesSet();
return factory;
}
// 创建 RedisTemplate 对象,用于操作 Redis 数据库
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
// 设置默认的 serialize 和 deserialize 的方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory factory) {
// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
// new Jackson2JsonRedisSerializer(Object.class);
//
// //解决查询缓存转换异常的问题
// ObjectMapper mapper = new ObjectMapper();
// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
// jackson2JsonRedisSerializer.setObjectMapper(mapper);
//配置序列化解决乱码问题
RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
// 设置缓存过期时间 为解决缓存雪崩,所以将过期时间加随机值,设置10分钟
.entryTtl(Duration.ofMinutes(10))
//设置缓存前缀
.prefixCacheNameWith("cache:user")
//设置key的序列化
.serializeKeysWith(keyPair())
//设置value的序列化
.serializeValuesWith(valuePair())
//为了防止缓存击穿,允许存null值
.disableCachingNullValues();
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
//设置过期时间30秒
.entryTtl(Duration.ofSeconds(30))
//设置缓存前缀
.prefixCacheNameWith("cache:admin")
.disableCachingNullValues()
.serializeKeysWith(keyPair())
.serializeValuesWith(valuePair());
return RedisCacheManager.builder(factory)
.withCacheConfiguration("user",cacheConfig)
.withCacheConfiguration("admin",redisCacheConfiguration)
.build();
}
/**
* 配置键序列化
* @return StringRedisSerializer
*/
private RedisSerializationContext.SerializationPair<String> keyPair() {
return RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer());
}
/**
* 配置值序列化,使用 GenericJackson2JsonRedisSerializer 替换默认序列化
* @return GenericJackson2JsonRedisSerializer
*/
private RedisSerializationContext.SerializationPair<Object> valuePair() {
return RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
}
}
yml文件
java
spring:
redis:
host: 192.168.64.3
database: 0
port: 6379
password:
jedis:
pool:
max-active: 50
max-idle: 50
timeout: 1000
RedisTemplate常用方法
使用方法如下:RedisTemplate常用方法详细操作