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的核心就是对某个方法进行缓存,其实质就是缓存该方法的返回结果,并把方法参数和结果用 ...
随机推荐
- 小程序实践(五):for循环绑定item的点击事件
微信展示列表效果借助于 wx:for 简单写一个列表(wxml文件中): 对应的数据源(js文件中): 写一个点击监听: 效果: 以上.可以实现列表的item点击效果,但是无法到点击的item对应的 ...
- 六. Redis发布订阅机制
发布订阅(pub/sub)是一种消息通信模式,主要是解除消息发布者和消息订阅者之间通信的耦合. Redis作为一个pub/sub的服务器,在订阅者和发布者之间起到了一个消息路由的功能.订阅者可以通过s ...
- Android-启动页“android:windowBackground”变型?
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...
- html之css选择器学习
相关内容: 什么是css选择器 标签选择器 类选择器 id选择器 并集选择器(分组选择器) 交集选择器 后代选择器 子标签选择器 属性选择器 相邻兄弟选择器 伪类选择器 伪元素选择器(伪对象选择器) ...
- 利用Selenium爬取淘宝商品信息
一. Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...
- Linux 网卡聚合
Linux 网卡聚合的类型: 1.broadcast:传输来自所有端口的每个包 2.roundrobin:以轮播方式传输来自每个端口的包 3.activebackup:故障转移运行程序,监视更改并选择 ...
- OSWatcher使用过程中小问题解决方法
本文介绍一下在使用OSWatcher过程当中遇到的两个问题的解决方法.如有更好的方法,敬请留言. 1:OSWatcher在配置文件里面设置了参数OSW_COMPRESSION为gzip后,OSWatc ...
- 自动化测试基础篇--Selenium cookie操作
摘自https://www.cnblogs.com/sanzangTst/p/8376471.html 在验证浏览器中cookie是否正确时,有时基于真实cookie的测试是无法通过白盒和集成测试进行 ...
- c/c++ 类模板初探
类模板 1,模板类里的函数都是模板函数 2,模板类里的函数,在类外面实现的时候,要用模板函数(方法:push_back)的方式实现,在类内部实现时,不需要用模板函数(方法:show)方式实现. 3,用 ...
- perl语言中的.pm文件和.pl文件区别
perl...呵呵呵 按照惯例,.pm 应该保存 Perl Module,也就是 Perl 模块.例如 Socket.pm.pl 应该保存 Perl Library,也就是 Perl 库文件.例如 p ...