Spring Cache使用详解
Spring Cache
- @Cache(value="Cx")//Cache是指定在Cx上面的 或者指定多个@Cache(value={"Cx","Cx1"})
- public User getUser(String id){
- return .....;
- }
-----------------------key:自定义key
- @Cacheable(value="users", key="#id")
- public User find(Integer id) {
- returnnull;
- }
- @Cacheable(value="users", key="#user.id")
- public User find(User user) {
- returnnull;
- }
除了上面使用方法参数作为Key以外,Spring还为我们提供了一个root对象可以生成key。通过root对象我们还可以获取到
#root.method.name
#root.target
#root.args[0]
#root.caches[0],name
- @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
这个则是当用户id为偶数的时候才会缓存。
- @Caching(cacheable=@Cacheable("users"),evict={@CacheEvict("cache2"),@CacheEvict(value="cache3",allEntries=true)})<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
- @Target({ElementType.TYPE, ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- @Cacheable(value="users")
- public @interface MyCacheable {
- }
我们在需要缓存的方法上面使用@MyCacheable可以达到同样的缓存效果。
- <cache:advice id="cacheAdvice" cache-manager="cacheManager">
- <cache:caching cache="users">
- <cache:cacheable method="findById" key="#p0"/>
- <cache:cacheable method="find" key="#user.id"/>
- <cache:cache-evict method="deleteAll" all-entries="true"/>
- </cache:caching>
- </cache:advice>
cache-manager默认是cacheManager。其中指定了将findById和find方法缓存到users中。也可以使用通配符“*”
- <aop:config proxy-target-class="false">
- <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.xxx.UserService.*(..))"/>
- </aop:config>
- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
- <property name="caches">
- <set>
- <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="xxx"/>
- </set>
- </property>
- </bean>
p:name则是我们在注解时候使用的value(也就是使用的哪个cache)。
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcacheManager"/>
- <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache-spring.xml"/>
它是通过指定的ehcache配置文件来生成的一个Ehcache的CacheManager。若未指定则默认规则取classpath路径下的ehcache文件,若还是不存在则取Ehcache对应包的ehcache-failsafe.xml文件作为配置文件。
Spring Cache使用详解的更多相关文章
- Spring Cache抽象详解
缓存简介 缓存,我的理解是:让数据更接近于使用者:工作机制是:先从缓存中读取数据,如果没有再从慢速设备上读取实际数据(数据也会存入缓存):缓存什么:那些经常读取且不经常修改的数据/那些昂贵(CPU/I ...
- 转:Spring Cache抽象详解
缓存简介 缓存,我的理解是:让数据更接近于使用者:工作机制是:先从缓存中读取数据,如果没有再从慢速设备上读取实际数据(数据也会存入缓存):缓存什么:那些经常读取且不经常修改的数据/那些昂贵(CPU/I ...
- spring Cache注解详解
@CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置.在这里@CacheConfig(cacheNames = "users"):配置了该数据访问对象中返回的内容 ...
- Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解
随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- Spring jar包详解
Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...
- Spring——jar包详解(转)
Spring——jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spr ...
- Spring Boot异常处理详解
在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...
- spring事务配置详解
一.前言 好几天没有在对spring进行学习了,由于这几天在赶项目,没有什么时间闲下来继续学习,导致spring核心架构详解没有继续下去,在接下来的时间里面,会继续对spring的核心架构在继续进行学 ...
随机推荐
- C# 微信公众平台开发(1)-- 服务器配置
题记:最近公司需要开发微信服务号,由本人负责,以前虽然听过微信开发,但并没有认真的去了解,项目开发中,也边看文档边开发,记录自己的项目开发经验: 1.注册帐号--填写服务器配置 在https://mp ...
- 风行一时瀑布流网页布局,实现无限加载(jquery)
今天跟大家分享一个瀑布流网页布局,先跟大家分析下实现的思路吧 主要思路:一.根据屏幕宽度和单个浮动层的宽度来确定浮动层列数 var $boxs = $("#main>div" ...
- javascript --推箱子
<head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" ...
- poj 1206
/** 题意: 给定一序列,置换k次之后 输出其状态 置换: 考察循环节长度, 思路: 分别求出每个元素的循环节的大小,用k 模其大小,大的k次之后的位置, 输出即可 **/ #include < ...
- android-通知Notification
发送通知 public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstan ...
- Linux cd命令
1: cd 不加任何参数,它会自动跳到用户的家目录中去! 2: ~ 表示用户的家目录 3: cd ~userNmae/ 这样可以进入指定用户的家目录中去! 4: cd - 跳到上一次所在的目 ...
- perl笔记
更精确的空白字符串匹配 1.水平空白字符 Perl5.10之前,使用\s这种分类,不够精确,容易导致意料外的结果: Perl5.10中引入\h字符组,用来匹配任意水平空白字符(包括Unicode字符集 ...
- tarjan算法大意
Tarjan算法 (以发现者Robert Tarjan命名)是一个在图中寻找强连通分量的算法.算法的基本思想为:任选一结点开始进行深度优先搜索dfs(若深度优先搜索结束后仍有未访问的结点,则再从中任选 ...
- [Ext JS 4] 布局之实战一 - 中间区块不会自动伸展 (tab)
前言 [Ext JS 4] 布局之实战一 - 中间区块不会自动伸展 (tab) 在上一篇中,中间的tab 区块无法自动伸展的原因一句话说就是: 使用contentEL的方式,相关HTML元素不会参与组 ...
- SQL Server索引进阶:第一级,索引简介
这个并不是我翻译的,全文共有15篇,但我发现好多网站已经不全,所以自己整理. 原文地址: Stairway to SQL Server Indexes: Level 1, Introduction t ...