@ENABLECACHING 基于注解的缓存
2024-09-05 17:35:55
@EnableCaching
• @Cacheable
指定一个或多个Cache名字,同属性cacheNames
Spring Cache 使用 ---@EnableCaching @Cacheable 注解
@Cacheable(value ="sampleCache")
@Cacheable(cacheNames="sampleCache")
• @CacheEvict
用于仅清除缓存
例子里的注解 @CacheEvict 中存在有以下几个元素
- value (也可使用 cacheNames) : 同Cacheable注解,可看做命名空间。表示删除哪个命名空间中的缓存
- allEntries: 标记是否删除命名空间下所有缓存,默认为false
- key: 同Cacheable注解,代表需要删除的命名空间下唯一的缓存key。
第一段,与 @Scheduled 注解同时使用,每十秒删除命名空间name下所有的缓存。
第二段,调用此方法后删除命名空间models下, key == 参数 的缓存 同样含有unless与condition
@CacheEvict(value = "models", allEntries = true)
@Scheduled(fixedDelay = 10000)
public void deleteFromRedis() {
} @CacheEvict(value = "models", key = "#name")
public void deleteFromRedis(String name) {
}
• @CachePut
用于仅存放缓存
例子里的注解 @CachePut 中存在有以下几个元素
value: 同上
key: 同上
condition(unless): 同上
比如可用于后台保存配置时及时刷新缓存。
SpringCache缓存初探
@CachePut(value = "models", key = "#name")
public TestModel saveModel(String name, String address) {
return new TestModel(name, address);
}
• @Caching
用于在一个方法或者类上同时指定多个Spring Cache相关的注解
@Caching(cacheable = {@Cacheable(value = "userCache", key = "#a0", unless = "#")
,@Cacheable(value = "userCache", key = "#a0", unless = "#")})
• @CacheConfig
这个注解是用于在同一个类中共享一些基础的cache配置的
一个类级别的注解,允许共享缓存的名称、KeyGenerator、CacheManager 和CacheResolver。
该操作会被覆盖。
//开启缓存注解
@Configuration
@EnableCaching
public class AppConfig {
}
最新文章
- jquery.extend
- Lintcode 375.克隆二叉树
- dojo.publish 和 dojo.subscribe
- Ubuntu 下使用Remmina Remote Desktop client 连接windows server输入法的问题
- linux 批量重命名文件
- iOS开发中打开本地应用、打开appStore应用、给app评分功能实现
- 纯CSS实现三列DIV等高布局
- Java批处理操作
- [Hadoop] - 自定义Mapreduce InputFormat&;OutputFormat
- asp.net web api 构建api帮助文档
- 【react】---17新增的生命周期
- Spring 使用介绍(八)—— 零配置(一)
- MultipartFile(文件的上传)
- C# 1.0 到 4.0 的进化 1
- ss客户端的使用
- QT TCP网络编程
- 硬盘坏道检测工具对比(DiskGenius/HdTunePro/MHDD等)
- 转:只能选择GridView中的一个CheckBox(单选CheckBox)
- 关于mvn install命令执行报错问题
- 【【模板】严格次小生成树[BJWC2010]】