pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties
#redis 配置
spring.redis.host=192.168.122.128
spring.redis.password=123456
spring.redis.port=6379
spring.redis.timeout=3000
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
RedisConfig.java
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public StringRedisTemplate stingRedisTemplate() {
StringRedisTemplate template = new StringRedisTemplate(factory);
template.setDefaultSerializer(new StringRedisSerializer());
return template;
}
}