一 配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:component-scanbase-package="org.crazyit.app.service" /><cache:annotation-drivencache-manager="cacheManager" /><!-- 配置EhCache的CacheManager 通过configLocation指定ehcache.xml文件的位置 --><bean id="ehCacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"p:configLocation="classpath:ehcache.xml"  p:shared="false" /><!-- 配置基于EhCache的缓存管理器 并将EhCache的CacheManager注入该缓存管理器Bean --><bean id="cacheManager"class="org.springframework.cache.ehcache.EhCacheCacheManager"p:cacheManager-ref="ehCacheManager"></bean>
</beans>

二 属性文件

<?xml version="1.0" encoding="gbk"?>
<ehcache><diskStore path="java.io.tmpdir" /><!-- 配置默认的缓存区 --><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"/><!-- 配置名为users的缓存区 --><cache name="users"maxElementsInMemory="10000"eternal="false"overflowToDisk="true"timeToIdleSeconds="300"timeToLiveSeconds="600" />
</ehcache>

三 领域模型

package org.crazyit.app.domain;public class User
{private String name;private int age;public User(){}public User(String name, int age){super();this.name = name;this.age = age;}public String getName(){return name;}public void setName(String name){this.name = name;}public int getAge(){return age;}public void setAge(int age){this.age = age;}}

四 Service

1 接口类

package org.crazyit.app.service;
import org.crazyit.app.domain.User;public interface UserService
{User getUsersByNameAndAge(String name, int age);User getAnotherUser(String name, int age);
}

2 实现类

package org.crazyit.app.service.impl;import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Scope;@Service("userService")
@Cacheable(value = "users")
public class UserServiceImpl implements UserService
{public User getUsersByNameAndAge(String name, int age){System.out.println("--正在执行findUsersByNameAndAge()查询方法--");return new User(name, age);}public User getAnotherUser(String name, int age){System.out.println("--正在执行findAnotherUser()查询方法--");return new User(name, age);}// 指定根据name、age参数清除缓存@CacheEvict(value = "users")public void evictUser(String name, int age){System.out.println("--正在清空"+ name+ " , " + age + "对应的缓存--");}// 指定清除user缓存区所有缓存数据@CacheEvict(value = "users" , allEntries=true)public void evictAll(){System.out.println("--正在清空整个缓存--");}
}

五 测试类

package lee;import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest
{public static void main(String[] args){ApplicationContext ctx =new ClassPathXmlApplicationContext("beans.xml");UserService us = ctx.getBean("userService" , UserService.class);// 调用us对象的2个带缓存的方法,系统会缓存两个方法返回的数据User u1 = us.getUsersByNameAndAge("孙悟空", 500);User u2 = us.getAnotherUser("猪八戒", 400);//调用evictUser()方法清除缓存中指定的数据us.evictUser("猪八戒", 400);// 由于前面根据"猪八戒", 400缓存的数据已经被清除了,// 因此下面代码会重新执行,方法返回的数据将被再次缓存。User u3 = us.getAnotherUser("猪八戒", 400);  // ①System.out.println(u2 == u3); // 输出false// 由于前面前面已经缓存了参数为"孙悟空", 500的数据,// 因此下面代码不会重新执行,直接利用缓存中的数据。User u4 = us.getAnotherUser("孙悟空", 500);   // ②System.out.println(u1 == u4); // 输出true// 清空整个缓存。us.evictAll();// 由于整个缓存都已经被清空,因此下面两行代码都会重新执行User u5 = us.getAnotherUser("孙悟空", 500);User u6 = us.getAnotherUser("猪八戒", 400);System.out.println(u1 == u5); // 输出falseSystem.out.println(u3 == u6); // 输出false}
}

六 测试结果

--正在执行findUsersByNameAndAge()查询方法--
--正在执行findAnotherUser()查询方法--
--正在清空猪八戒 , 400对应的缓存--
--正在执行findAnotherUser()查询方法--
false
true
--正在清空整个缓存--
--正在执行findAnotherUser()查询方法--
--正在执行findAnotherUser()查询方法--
false
false

Spring实战——清除缓存相关推荐

  1. Spring实战——缓存

    缓存 提到缓存,你能想到什么?一级缓存,二级缓存,web缓存,redis-- 你所能想到的各种包罗万象存在的打着缓存旗号存在的各种技术或者实现,无非都是宣扬缓存技术的优势就是快,无需反复查询等. 当然 ...

  2. redis 清空缓存_「镜头回放」简直了!spring中清除redis缓存导致应用挂死

    异常场景 springWeb应用一直运行正常,同事最近反应,每次版本更新完毕,刷新缓存,就会导致应用挂死.只有重启redis应用才恢复正常. 项目概况 springWeb项目,常用配置表做了redis ...

  3. 实战SSM_O2O商铺_47【Redis缓存】清除缓存接口的开发

    文章目录 概述 接口层改造 CacheService接口 CacheService接口实现类 工具类中的方法 单元测试 Github地址 概述 设计如下: 在接口层传入缓存key的前缀,通过匹配的方式 ...

  4. 基于Spring的Web缓存

    缓存的基本思想其实是以空间换时间.我们知道,IO的读写速度相对内存来说是非常比较慢的,通常一个web应用的瓶颈就出现在磁盘IO的读写上.那么,如果我们在内存中建立一个存储区,将数据缓存起来,当浏览器端 ...

  5. Spring Boot使用缓存功能

    缓存的好处不言而喻,比如查询商品的价格,如果可以放到缓存中,而不用每次都到数据库中查询,这将会大大提升系统性能,因为和缓存交互比访问数据库要快很多.或者在缓存中存放临时数据,而不用放到数据库中. 在学 ...

  6. Spring Cache抽象-缓存注解

    文章目录 概述 Spring缓存的基本原理 @Cacheable :主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 键生成器 带条件的缓存 @Cacheable 注解参数说明 示例-缓存管理 ...

  7. 《spring实战第四版》的读书笔记

    <spring实战第四版>的读书笔记 1 概述 <Spring实战第四版>描述了Spring4架构的设计,看完了以后,最大感觉是Spring的IOC与aop理念实在是太强大了, ...

  8. springboot 插入返回id_实战SpringBoot缓存开发

    点击上方"Java专栏",选择"置顶或者星标" 第一时间阅读精彩文章! ☞ 程序员进阶必备资源免费送「21种技术方向!」 点击查看☜ 作者:Yrion cnbl ...

  9. 缓存插件 Spring支持EHCache缓存

    Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能.其中,spring对EHCache提供了很好的支持. 在介绍Spring的缓 ...

最新文章

  1. Android架构篇-4 架构模式MVVM
  2. jdbc mysql连接测试_JDBC测试计划-连接mysql
  3. 解决zabbix3.4图表显示中文乱码问题
  4. C语言代码规范(九)运算符优先级使用括号提高阅读性
  5. Real提示“作为受限用户,您无足够的windows操作权限”的解决办法
  6. AI论文解读丨融合视觉、语义、关系多模态信息的文档版面分析架构VSR
  7. 智能客户端(Smart Client )中文文档及案例(转贴)
  8. 关于“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”的解决
  9. Android 车载应用开发与分析(5) - CarLauncher(一)
  10. 怎么获取计算机用户权限,如何获取电脑的最高管理权限|细讲电脑最高管理权限的获取方式...
  11. snmp++ linux 编译出错_成为linux高手的第二步
  12. 从Scrum之父探源敏捷方法论
  13. 联发科mt8516价格_联发科MT8516销量破两亿:揭秘背后的故事
  14. transition过渡的基本使用
  15. PRD 算法 Golang 实现
  16. windows下编程可执行程序加载.dll动态库失败
  17. 【C#】Excel舍入函数Round、RoundUp、RoundDown的C#版
  18. 你真的知道什么是元音什么是辅音吗?
  19. elementUI实现table表头展示上、下角标
  20. 2021 Google 开发者大会进行时:汇聚开发者合力,共建全球技术生态

热门文章

  1. Photon PUN和Photon Server连接问题及解决方法(Unity开发)
  2. 【EF】Entity Framework快速入门
  3. 2021年高处安装、维护、拆除考试题库及高处安装、维护、拆除模拟考试题库
  4. 大数据落地背后,是未来“数据主义”的前奏?
  5. 怎么利用软文发布营销推广企业品牌?
  6. 51单片机——让一个LED灯闪烁
  7. 一张图看懂 USDT三种类型地址 Omni、ERC20、TRC20的区别
  8. python安装python-ladp失败
  9. 欧盟芯片法案正式通过,不希望再受制于美国芯片,两兄弟闹掰了
  10. php阴性,科学网—可发表阴性结果的期刊 - 许培扬的博文