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 ...
随机推荐
- jsp get参数乱码问题
摘自:username2.iteye.com/blog/1597917个人理解中文传送的时后需要转码: js代码: 要进行两次转码才不会出现乱码(默认为UTF-) encodeURI(encodeUR ...
- SharePoint 多行文本字段设置默认值
前言 最近有这样一个需求,创建一个表单,里面有多行文本字段,但是要求内容默认带一个表格,这样用户新建项目的时候,就可以直接填表格了. 好吧,这样的需求我们可以通过JavaScript实现. 1.默认的 ...
- JavaScript比较两个对象的值是否相等
JavaScript比较两个对象的值是否相等 function isObjectValueEqual(a, b) { var aProps = Object.getOwnPropertyNames(a ...
- What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
来自:http://stackoverflow.com/questions/510632/whats-the-difference-between-concurrenthashmap-and-coll ...
- 【SqlServer】SqlServer的异常处理
在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...
- C#调用接口注意要点 socket,模拟服务器、客户端通信 在ASP.NET Core中构建路由的5种方法
C#调用接口注意要点 在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用, 所以首先需要通过调用登录接口来保存c ...
- U盘安装CentOS 7卡住在 mounting configuration file system
使用UltraISO PE 9.6.0.3000刻录CentOS 7.2到U盘之后,在PC机上安装,一直卡住在此界面 网上各路大神各显神通,提供了各种各样的办法,后来根据一位网友的说法,顺利安装完成 ...
- [android开发教程] 一个神奇的Demo 帮你掌握所有android控件
(本文内容来源:http://www.eoeandroid.com/thread-182392-1-1.html 转载请注明出处!) 2.jpg (23.78 KB, 下载次数: 0) 下载附件 ...
- Unity3D Adam Demo的学习与研究
1.简述 这篇文章是对Adam各种相关资料了解后进行一些精简的内容.如果你想仔细研究某个技术请跳转至unity相关页面. Adam官方页面: https://unity3d.com/cn/page ...
- [svc]linux iptables实战
参考: http://blog.51yip.com/linux/1404.html 链和表 参考: https://aliang.org/Linux/iptables.html 配置 作为服务器 用途 ...