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的核心架构在继续进行学 ...
随机推荐
- dedecms导入编辑器
<?php GetEditor("info","",450,"Diy"); ?>
- ##DAY14——StoryBoard
•iOS下可视化编程分为两种⽅式:xib和storyboard. •在使用xib和storyboard创建GUI过程中,以XML文件格式 存储在Xcode中,编译时生成nib的二进制文件.在运行时, ...
- python 发送安全邮件
用python 写了一个发送邮件的脚本,配上host 和端口,发现一直报错: smtplib.SMTPException: No suitable authentication method foun ...
- ndk 编译 boost 库,支持serialization
Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C+ ...
- codeforces 632C. The Smallest String Concatenation 排序
题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...
- Archlinux在Btrfs分区上的安装(bios篇)
其实本文所有的内容在Archwiki上都可以找到,并且更新更全面(只是比较零散),我所做的只是对安装流程做一个小小的总结,每一步我都会稍微解释一下,但不会说的特别详细,毕竟这只是一篇安装引导文,而不是 ...
- querySelector $() getElementBy区别
参考 http://stackoverflow.com/questions/14377590/queryselector-and-queryselectorall-vs-getelementsbycl ...
- 在PADS LAYOUT中修改所有元件字体的大小,怎么修改?
1.选中一个字符,Ctrl+Q查看一下属性,是在哪一层. 2.Ctrl+Alt+F(Filter)打开滤波器选项,点Layer,将除字符所在层之外的层全部关掉,即将其前面的"√"去 ...
- ArcMAp对线要素进行平滑处(打断)
一:工具简单介绍 -- ArcMAp10.1的高级编辑工具中提供了对线/面要素进行概括/平滑处理的工具. 概括工具.平滑工具分别例如以下:(首先得开启编辑状态 --- 才干够对要素的属性进行更改).选 ...
- 自己定义标签中tagsupport的一些方法
TagSupport生命周期 TagSupport类分别实现了Tag与IterationTag界面,其预设的回传值是:doStartTag()回传 SKIP_BODY.EVAL_BODY_INCLUD ...