引入依赖
java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
application.properties中
java
redis.host=192.168.1.252
redis.port=6379
redis.database=1
redis.password=1234
redis.alive=600s
redis配置类
java
@Data
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
private String host;
private String port;
private String database;
private String password;
private String alive;
}
CacheManager与Redis整合配置类
java
@EnableConfigurationProperties(RedisProperties.class)
@Configuration
@EnableCaching
public class RedisConfig {
@Autowired
RedisProperties redisProperties;
/**
* jedis连接工厂
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration);
return factory;
}
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory jedisConnectionFactory) {
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 设置过期时间和序列化类型
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(cacheProperties.getAlive()).computePrefixWith(cacheName -> "demoService".concat(":").concat(cacheName).concat(":")).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
return RedisCacheManager.builder(jedisConnectionFactory)
.cacheDefaults(config).build();
}
Springboot 集成redis后使用缓存
Service 层
java
@Service
public class StuService {
//@Cacheable: 标记方法的结果可以被缓存,下次调用时直接从缓存中获取结果,如果缓存中不存在,则执行方法并将结果放入缓存
@Cacheable(value = "stus", key = "#id")
public Student getStuById(Long id) {
// 从数据库中获取用户信息
return stuRepository.findById(id);
}
//@CachePut: 标记方法的结果可以被缓存,每次调用方法都会执行方法并将结果放入缓存,适用于更新缓存数据的场景
@CachePut(value = "stus", key = "#stu.id")
public Student updateStu(Student stu) {
// 更新数据库中的用户信息
return stuRepository.save(stu);
}
//@CacheEvict:标记方法会从缓存中移除指定的数据,可以通过指定的条件来决定移除哪些缓存数据
@CacheEvict(value = "stus", key = "#id")
public void deleteStuById(Long id) {
// 从数据库中删除学生信息
stuRepository.deleteById(id);
}
//@Caching:可以同时使用多个缓存注解,用于组合多个缓存操作
@Caching(
cacheable = {
@Cacheable(value = "stus", key = "#id")
},
evict = {
@CacheEvict(value = "stus", key = "#stu.id")
}
)
public Student getStuByIdAndUpdate(Long id, Student stu) {
// 更新数据库中的学生信息
return stuRepository.save(stu);
}
}
参考博客:https://blog.csdn.net/yu619251940/article/details/130244927
https://blog.csdn.net/yiqiu1959/article/details/126151591
https://blog.csdn.net/weixin_45962741/article/details/120710578?spm=1001.2014.3001.5506