整合SpringCache
创始人
2025-06-01 21:56:29
0

整合SpringCache

1、引入依赖cache还有redis

org.springframework.bootspring-boot-starter-cache

2、写配置

spring:cache:type: redis

3、测试使用缓存

@Cacheable:触发将数据保存到缓存的操作
@CacheEvict:触发将数据从缓存中删除的操作
@CachePut:不影响方法更新缓存
@Caching:组合以上多个操作
@CacheConfig:在类级别共享缓存的相同配置

1.开启缓存注解@EnableCaching

//指定缓存的名字
@Cacheable({"category"})  //代表当前的方法是需要缓存的,如果缓存中有,方法不调用,如果缓存中没有,则调用方法,最后将方法的结果放入缓存

默认行为:

key是默认自动生成的

缓存的value的值,默认使用jdk序列化机制,将序列化后的数据存到redis中

默认ttl时间-1

自定义操作:

1、指定key:key属性指定,接受一个

2、指定缓存的存活时间

3、将数据保存为json

spring:cache:type: redisredis:time-to-live: 60000   #毫秒
@Cacheable(value = {"category"},key = "'level1Cateogrys'")

修改保存为json格式

创建配置类

@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {@AutowiredCacheProperties cacheProperties;@BeanRedisCacheConfiguration redisCacheConfiguration(){RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();//key的序列化机制config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));//Value的序列化机制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.prefixCacheNameWith(redisProperties.getKeyPrefix());}if (!redisProperties.isCacheNullValues()){config=config.disableCachingNullValues();}if (!redisProperties.isUseKeyPrefix()){config=config.disableKeyPrefix();}return config;}
}

其他的配置

spring:cache:type: redisredis:time-to-live: 60000   #毫秒key-prefix: CACHE_    #前缀cache-null-values: true  #是否缓存空值----能够防止缓存穿透

@CaheEvict的使用

这里的key注意里面是单引号

@CacheEvict(value = "category",key = "'level1Cateogrys'")

在这里插入图片描述

当修改三级分类后缓存会被自动删除

将获取三级分类的json也添加缓存
在这里插入图片描述

 @Cacheable(value = "category",key = "#root.methodName")

访问gulimall.com首页

查看redis中的缓存情况

当修改菜单后,如何将上面这两个缓存同时删除

在更新菜单的方法上实现

使用@Caching注解实现

@Caching(evict = {@CacheEvict(value = "category",key = "'level1Cateogrys'"),@CacheEvict(value = "category",key = "'getCatalogJson'")})

还可以使用

@CacheEvict(value = "category",allEntries = true)

删除category分区下的所有缓存

如果下面这种方式,产生的缓存就会按照下面的图片一样展示

@CacheEvict(value = "category",allEntries = true)

在这里插入图片描述

Spring-Cache的不足

1、读模式:

缓存穿透:查询一个null数据。解决:缓存空数据

缓存击穿:大量并发进来同时查到一个正好过期的数据 。解决:默认无锁;sync=true(加锁)

缓存雪崩:大量key同时过期。解决:加随机时间。加上过期时间

2、写模式(缓存与数据库一致)

读写加锁

引入Canal、感知到Mysql的更新去更新数据库

读多写多,直接去数据库查询就行

总结:常规数据(读多写少,即时性,一致性要求不高的数据,完全可以使用Spring-Cache)

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...