1、配置文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
2、配置文件
redis:
database: 5
host: localhost
port: 6379
password: password
jedis:
pool:
max-active: 200
max-wait: -1
max-idle: 10
min-idle: 0
timeout: 6000
3、redis配置
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
4、redis使用
@Autowired
private RedisTemplate redisTemplate;
//从redis获取和设置值
ValueOperations valueOperations = redisTemplate.opsForValue();
Object redisOpenIdAcount = valueOperations.get("id");
if (redisOpenIdAcount == null) {
valueOperations.set(openid, 1);
if (messageService.insertFeedMessage(feedMessage)) {
logger.info("id不存在");
} else {
valueOperations.set("id", 2);
}