8 -- 深入使用Spring -- 5...3 使用@CacheEvict清除缓存
8.5.3 使用@CacheEvict清除缓存
被@CacheEvict注解修饰的方法可用于清除缓存,使用@CacheEvict注解时可指定如下属性:
⊙ value : 必须属性。用于指定该方法用于清除哪个缓存区的数据。
⊙ allEntries : 该属性指定是否清空整个缓存区。
⊙ beforeInvocation : 该属性指定是否在执行方法之前清除缓存。默认是在方法成功完成之后才清除缓存。
⊙ condition : 该属性指定一个SpEL表达式,只有当该表达式为true时才清除缓存。
⊙ key : 通过SpEL表达式显示指定缓存的key。多个参数组合的key 用#name + #age + ...
Class : UserServiceImpl
package edu.pri.lime._8_5_3.cacheevict.service.impl; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import edu.pri.lime._8_5_3.cacheevict.bean.User;
import edu.pri.lime._8_5_3.cacheevict.service.UserService; @Service
@Cacheable(value="users")
public class UserServiceImpl implements UserService{ @Override
public User getUserByNameAndAge(String name, int age) {
System.out.println("----------正在执行getUserByNameAndAge()查询方法----------");
return new User(name,age);
} @Override
public User getAnotherUser(String name, int age) {
System.out.println("----------正在执行getAnotherUser()查询方法----------");
return new User("Oracle",22);
} @Override
@CacheEvict(value="users")
public void evictUser(String name, int age) {
System.out.println("--正在清空" + name + "," + age + "对应的缓存--");
} @Override
@CacheEvict(value="users",allEntries=true)
public void evictAll() {
System.out.println("--正在情况整个缓存--");
} }
XML :
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描Spring的组件 -->
<context:component-scan base-package="edu.pri.lime._8_5_3.cacheevict.service.impl"/> <!-- 启用Spring缓存 -->
<cache:annotation-driven cache-manager="cacheManager" /> <!-- 使用simpleCacheManager缓存管理器 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<!-- 配置缓存区 -->
<property name="caches">
<set>
<!-- 使用ConcurrentMapCacheFactoryBean工程Bean生产缓存区 -->
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<!-- 定义缓存区名称 -->
<property name="name" value="users" />
</bean>
</set>
</property>
</bean>
</beans>
Class : SpringTest
package edu.pri.lime._8_5_3.cacheevict; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import edu.pri.lime._8_5_3.cacheevict.bean.User;
import edu.pri.lime._8_5_3.cacheevict.service.UserService; public class SpringTest { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_5_3_cacheevict.xml");
UserService userService = ctx.getBean("userService",UserService.class);
User userA = userService.getUserByNameAndAge("lime", 24);
User userB = userService.getAnotherUser("lime", 24);
System.out.println(userA == userB);
User userC = userService.getUserByNameAndAge("Oracle", 24);
User userD = userService.getAnotherUser("Oracle", 24);
System.out.println(userD == userC); userService.evictUser("lime", 24); User userE = userService.getUserByNameAndAge("lime", 24);
User userF = userService.getAnotherUser("Oracle", 24); userService.evictAll(); User userG = userService.getUserByNameAndAge("lime", 24);
User userH = userService.getAnotherUser("Oracle", 24); }
}
Console :
----------正在执行getUserByNameAndAge()查询方法----------
true
----------正在执行getUserByNameAndAge()查询方法----------
true
--正在清空lime,24对应的缓存--
----------正在执行getUserByNameAndAge()查询方法----------
--正在情况整个缓存--
----------正在执行getUserByNameAndAge()查询方法----------
----------正在执行getAnotherUser()查询方法----------
啦啦啦
8 -- 深入使用Spring -- 5...3 使用@CacheEvict清除缓存的更多相关文章
- Spring Boot使用redis做数据缓存
		
1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...
 - Spring Boot中使用EhCache实现缓存支持
		
SpringBoot提供数据缓存功能的支持,提供了一系列的自动化配置,使我们可以非常方便的使用缓存.,相信非常多人已经用过cache了.因为数据库的IO瓶颈.一般情况下我们都会引入非常多的缓存策略, ...
 - SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存
		
系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...
 - Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
		
Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
 - 在Spring、Hibernate中使用Ehcache缓存(2)
		
这里将介绍在Hibernate中使用查询缓存.一级缓存.二级缓存,整合Spring在HibernateTemplate中使用查询缓存.,这里是hibernate3,使用hibernate4类似,不过不 ...
 - spring aop + xmemcached 配置service层缓存策略
		
Memcached 作用与使用 基本介绍 1,对于缓存的存取方式,简言之,就是以键值对的形式将数据保存在内存中.在日常业务中涉及的操作无非就是增删改查.加入缓存机制后,查询的时候,对数据进行缓存,增删 ...
 - spring boot整合reids 然后实现缓存分页(方法之一) 以及RedisTemplate存到reids 里面get 就消失的坑
		
业务需求 首页 实现缓存分页 spring boot 整合redis (我的是2.0.3版本的) 在pom 文件写上依赖包即可 <dependency><!--依赖包--> ...
 - 8 -- 深入使用Spring -- 5...2 使用@Cacheable执行缓存
		
8.5.2 使用@Cacheable执行缓存 @Cacheable可用于修饰类或修饰方法,当使用@Cacheable修饰类时,用于告诉Spring在类级别上进行缓存 ------ 程序调用该类的实例的 ...
 - (转)为Spring集成的Hibernate配置二级缓存
		
http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...
 
随机推荐
- Android Studio下加入百度地图的使用 (一)——环境搭建
			
最近有学生要做毕业设计,会使用到定位及地图信息的功能,特此研究了一下,供大家参考,百度定位SDK已经更新到了5.0,地图SDK已经更新到了3.5,但是在AndroidStudio中使用还是存在一些不稳 ...
 - Spring AOP获取拦截方法的参数名称跟参数值
			
注意:这种方式需要JDK1.8版本支持 开始:http://www.cnblogs.com/wing7319/p/9592184.html 1.aop配置: <aop:aspectj-autop ...
 - 服务器能远程连接,网络连接正常,但是外网域名Ping不通,浏览器中打不开网站
			
服务器能远程连接成功,但在浏览器中打不开任何网站,出现这个问题一般是安装什么软件引起IE的相关设置做了变动或者是服务器中了病毒引起的,或是服务器的DNS设置是错误的. 一.先检查服务器DNS是否正确 ...
 - 微软BI 之SSIS 系列 - 带有 Header 和 Trailer 的不规则的平面文件输出处理技巧
			
案例背景与需求介绍 之前做过一个美国的医疗保险的项目,保险提供商有大量的文件需要发送给比如像银行,医疗协会,第三方服务商等.比如像与银行交互的 ACH 文件,传送给协会的 ACH Credit 等文件 ...
 - 转 c#中stringbuilder的使用
			
String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况 ...
 - SVN的Branch和Tag管理
			
dev:开发主线 branch: 部分特殊客户的定制化版本 tag: 主线的某个发布版本 release: 主线的里程碑式的发布版本(相比上一里程碑版本,改动非常大,并且当前已经很稳定的) 你可以在b ...
 - MYSQL 中query_cache_size小结
			
1 原理 MySQL查询缓存保存查询返回的完整结果.当查询命中该缓存,会立刻返回结果,跳过了解析,优化和执行阶段. 查询缓存会跟踪查询中涉及的每个表,如果这写表发生变化,那么和这个表相关的所有缓 ...
 - 新手如何学习 jQuery?
			
可以看张晓菲的<锋利的jQuery>,重点是自己理解函数用法并自行实现一些常用的效果.如果需要快速查阅可以用这个api,每个函数都附有简单的示例:http://api.jquery.com ...
 - C++11模版元编程
			
1.概述 模版元编程(template metaprogram)是C++中最复杂也是威力最强大的编程范式,它是一种可以创建和操纵程序的程序.模版元编程完全不同于普通的运行期程序,它很独特,因为模版元程 ...
 - 每日英语:Upgrade Your Life: How to speed up your PC (or Mac)
			
Is your desktop or laptop computer starting to feel a little poky? Even after just a few months of u ...