Python 官方文档:入门教程 => 点击学习
目录引入依赖配置测试使用缓存@Cacheable注解的使用@CacheEvict注解的使用@Caching注解的使用@CachePut注解的使用spring-Cache的不足读模式写
<!-- 引入Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 引入SprinGCache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
自动配置
CacheAutoConfiguration会导入 RedisCacheConfiguration;自动配置好了缓存管理器RedisCacheManager
配置使用redis作为缓存
spring.cache.type=redis
@Cacheable(value = {"cateGory"}, key = "#root.method.name")
@Override
public List<CategoryEntity> findCatelog1() {
System.out.println("查询数据库---");
return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
}
指定缓存数据的存活时间
spring.cache.redis.time-to-live=3600000
将数据保存为JSON格式:配置
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching // 开启缓存
public class MyCacheConfig {
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2jsonRedisSerializer()));
// 将配置文件中的所有配置都生效
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.geTKEyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
缓存的其他自定义配置
# 如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
# spring.cache.redis.key-prefix=CACHE_ # 默认就使用分区名
spring.cache.redis.use-key-prefix=true
# 是否缓存空值。防止缓存穿透
spring.cache.redis.cache-null-values=true
数据一致性中的失效模式
// @CacheEvict(value = "category", key = "'findCatelog1'")// 删除具体key的缓存
@CacheEvict(value = "category", allEntries = true)// 指定删除某个分区下的所有数据
@Transactional
@Override
public void updateCascade(CategoryEntity category) {
this.updateById(category);
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
}
数据一致性中的双写模式,使用@CachePut注解
@Caching(evict = {
@CacheEvict(value = "category", key = "'findCatelog1'"),// 删除缓存
@CacheEvict(value = "category", key = "'getCatalogJson'"),// 删除缓存
})
@Transactional
@Override
public void updateCascade(CategoryEntity category) {
this.updateById(category);
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
}
数据一致性中的双写模式
@CachePut // 双写模式时使用
@Cacheable(value = {"category"}, key = "#root.method.name", sync = true)
sync = true 相当于是加本地锁,可以用来解决击穿问题
常规数据(读多写少,即时性,一致性要求不高的数据),完全可以使用Spring-Cache。写模式:只要缓存的数据有过期时间就足够了
到此这篇关于SpringBoot2.X整合Spring-Cache缓存开发的实现的文章就介绍到这了,更多相关SpringBoot Spring-Cache缓存 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot2.X整合Spring-Cache缓存开发的实现
本文链接: https://www.lsjlt.com/news/165097.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0