Spring cache 使用说明
package org.cheng.user.client.service; import java.util.HashMap;
import java.util.Map; import org.cheng.user.client.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; /**
* 统一命名缓存 名称
* @author Cheng
*/
@CacheConfig(cacheNames = { "user" })
@Service
public class UserService { @Autowired
private CacheManager cacheManager; /**
* 单个命名缓存名称
* sync 默认为false 为true 需要同步等待
* @param id
* @return
*/
@Cacheable(cacheNames = "user", key = "#id",sync = false)
public User get(Integer id) {
System.out.println("--get---");
User user = new User();
user.setId(id);
return user;
} /**
* 按key存储
* 前置存储条件 condition 为true 存储
* 后置执行条件 unless 为false 存储
*
* @param user
* @return
*/
@CachePut(key = "#user.id", condition = "#user.id<10000", unless = "#result.name.length() < 3")
public User save(User user) {
user.setName("name - " + user.getId());
return user;
} /**
* 按key删除
*
* @param id
* @return
*/
@CacheEvict(key = "#id")
public String del(Integer id) {
System.out.println("--del---");
return String.valueOf(id);
} /**
* 清除全部
*/
@CacheEvict(allEntries = true)
public void removeAll() {
} /**
* 返回所有的缓存信息
* @return
*/
public Map<String,Cache> cache() {
Map<String,Cache> map = new HashMap<>();
for(String name:cacheManager.getCacheNames()) {
map.put(name, cacheManager.getCache(name));
}
return map;
} }
参考文献:
https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/integration.html#cache
https://www.cnblogs.com/OnlyCT/p/7845660.html#t4
Spring cache 使用说明的更多相关文章
- Spring Cache 笔记
@(Java ThirdParty)[Spring Cache] Spring Cache Abstraction 简介 Spring Cache提供了对底层缓存使用的抽象,通过注解的方式使用缓存,减 ...
- Spring Cache缓存框架
一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...
- Spring cache简单使用guava cache
Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...
- 注释驱动的 Spring cache 缓存介绍
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- [转]注释驱动的 Spring cache 缓存介绍
原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...
- Spring Cache 介绍
Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...
- spring Cache注解
如下:不能将缓存注解加在listCate(boolean isShowHide)方法上 因为spring是使用AOP的方法获取缓存,在一个bean中再去调用别一个方法,不会应用缓存 @Cacheabl ...
- 注释驱动的 Spring cache 缓存介绍--转载
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- Spring Cache使用详解
Spring Cache Spring Cache使用方法与Spring对事务管理的配置相似.Spring Cache的核心就是对某个方法进行缓存,其实质就是缓存该方法的返回结果,并把方法参数和结果用 ...
随机推荐
- 测试系统工程师TSE的职责与培养
测试系统工程师TSE的职责与培养 研发资深顾问 杨学明 如今,国内所有的研发型的公司都有测试部门,无论测试团队大小,都有测试组长,测试经理,测试工程师等头衔,但随着产品和业务的质量要求越来越高,产品的 ...
- Android 7.0 fiddler代理抓不到https请求的解决办法
解决方法: 1.在源码res目录下新建xml目录,增加network_security_config.xml文件 (工程名/app/src/main/res/xml/network_security ...
- POST 400 的一次遭遇
使用Springboot开发接口的过程中,使用POST接收提交的数据. @PostMapping("/input") public void inputData(@RequestB ...
- CentOS 7更改yum源与更新系统
在CentOS 7下更改yum源与更新系统. [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo cp /etc/yum.repos.d/CentOS-Base.rep ...
- c/c++ 标准顺序容器 容器的访问,删除 操作
c/c++ 标准顺序容器 容器的访问,删除 操作 pop_front:vector,string不支持 pop_back:forward_list不支持 知识点 1,front, back, at 成 ...
- Python的变量以及类型
1.程序是用来处理数据的,变量就是用来存储数据的 num1 = 100 2.为了更充分的利用内存空间以及更有效率的管理内存,变量是有不同的类型 3.怎样知道一个变量的类型呢? 3.1 在python ...
- 安装Jenkins getting started卡住
前言 jenkins版本:2.32.3 操作系统:windows 卡住信息 如果在安装jenkins时卡在getting startted的界面,如下所示 解决方法 1.打开 运行 输入 servi ...
- 【Teradata】 TPT基础知识
1.TPT Description Teradata Parallel Transporter (TPT) is client software that performs data extract ...
- C - 继续畅通工程 最小生成树
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...
- MySQL高级知识(三)——索引
前言:索引在sql调优部分占据着重要的位置,了解并深入索引对我们来说也是非常重要的.本篇主要介绍MySQL中索引的相关知识点. 1.索引是什么 MySQL官方对索引的定义:索引(Index)是帮助My ...