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清除缓存的更多相关文章

  1. Spring Boot使用redis做数据缓存

    1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...

  2. Spring Boot中使用EhCache实现缓存支持

     SpringBoot提供数据缓存功能的支持,提供了一系列的自动化配置,使我们可以非常方便的使用缓存.,相信非常多人已经用过cache了.因为数据库的IO瓶颈.一般情况下我们都会引入非常多的缓存策略, ...

  3. SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存

    系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...

  4. Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

    Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

  5. 在Spring、Hibernate中使用Ehcache缓存(2)

    这里将介绍在Hibernate中使用查询缓存.一级缓存.二级缓存,整合Spring在HibernateTemplate中使用查询缓存.,这里是hibernate3,使用hibernate4类似,不过不 ...

  6. spring aop + xmemcached 配置service层缓存策略

    Memcached 作用与使用 基本介绍 1,对于缓存的存取方式,简言之,就是以键值对的形式将数据保存在内存中.在日常业务中涉及的操作无非就是增删改查.加入缓存机制后,查询的时候,对数据进行缓存,增删 ...

  7. spring boot整合reids 然后实现缓存分页(方法之一) 以及RedisTemplate存到reids 里面get 就消失的坑

    业务需求 首页 实现缓存分页 spring boot 整合redis   (我的是2.0.3版本的) 在pom 文件写上依赖包即可 <dependency><!--依赖包--> ...

  8. 8 -- 深入使用Spring -- 5...2 使用@Cacheable执行缓存

    8.5.2 使用@Cacheable执行缓存 @Cacheable可用于修饰类或修饰方法,当使用@Cacheable修饰类时,用于告诉Spring在类级别上进行缓存 ------ 程序调用该类的实例的 ...

  9. (转)为Spring集成的Hibernate配置二级缓存

    http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...

随机推荐

  1. spring和springMVC的面试问题总结

    1.Spring中AOP的应用场景.Aop原理.好处? 答:AOP--Aspect Oriented Programming面向切面编程:用来封装横切关注点,具体可以在下面的场景中使用: Authen ...

  2. MX4_ADB

    一.Ubuntu环境1. 建立或修改文件 ~/.android/adb_usb.ini,在文件开头或末尾添加一行,内容是0x2a45. 2. 建立或修改文件 /etc/udev/rules.d/51- ...

  3. DotNetty z

    使用说明 客户端的代码和服务端的代码相差很少,体现了Netty统一的编程模型.有几个不同点: 客户端的Bootstrap不是ServerBootstrap: 客户端不需要主线程组,只有工作线程组,消息 ...

  4. VMWare Station 问题汇总

    1.开机黑屏,不启动系统 解决方法: 命令行窗口cmd—输入下面代码,然后重启计算机. netsh winsock reset 2.提示磁盘被锁无法打开 解决方法: 虚拟机目录下面的.lck文件都删了

  5. 学员会诊之02:SVN协作以及Page类的设计

    三层架构的学生管理系统是我们第一个稍微大型的项目:分层.一个解决方案多个Project,所以值得我们停下来好好审查审查. 1.测试SVN服务器地址 我们的作业要求学员创建自己的SVN服务器,并且将代码 ...

  6. 如何在IntelliJ IDEA中使用Git .ignore插件忽略不必要提交的文件

    参考  https://blog.csdn.net/qq_34590097/article/details/56284935 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDE ...

  7. 如何用C#动态编译、执行代码

    在开始之前,先熟悉几个类及部分属性.方法:CSharpCodeProvider.ICodeCompiler.CompilerParameters.CompilerResults.Assembly. 一 ...

  8. java 查看线程的信息

    的代码上加上 断点 运行 进入Terminal jps 查看进程号 jstack 进程号  查看线程的信息 jstack pid 此时进去DEBUG 端F9 跑完程序 再回到Terminal 中 就能 ...

  9. 微软BI 之SSIS 系列 - 通过 OLE DB 连接访问 Excel 2013 以及对不同 Sheet 页的数据处理

    文章更新历史 2014年9月7日 - 加入了部分更新内容,在文章最后提到了关于不同 Office Excel 版本间的连接问题. 开篇介绍 这篇文章主要总结在 SSIS 中访问和处理 Excel 数据 ...

  10. php调试用的几个小方法

    1.PHP把一个对象转化成json字符串 $json_string = json_encode($object, JSON_FORCE_OBJECT); json_encode($object); / ...