Spring实战之清除缓存操作示例

作者:cakincqm 时间:2023-05-31 07:46:26 

本文实例讲述了Spring实战之清除缓存操作。分享给大家供大家参考,具体如下:

一 配置文件


<?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/beans
  http://www.springframework.org/schema/beans/spring-beans-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">
  <context:component-scan
     base-package="org.crazyit.app.service" />
  <cache:annotation-driven
     cache-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" />
  <!-- 配置默认的缓存区 -->
 <defaultCache
   maxElementsInMemory="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); // 输出false
   System.out.println(u3 == u6); // 输出false
 }
}

六 测试结果

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

希望本文所述对大家java程序设计有所帮助。

来源:https://blog.csdn.net/chengqiuming/article/details/101614731

标签:Spring,清除缓存
0
投稿

猜你喜欢

  • Java单例模式下的MongoDB数据库操作工具类

    2023-11-20 12:55:01
  • mybatis-plus自动填充插入更新时间有8小时时差

    2021-07-26 07:18:40
  • java selenium元素定位大全

    2023-10-23 01:07:42
  • 一文带你入门SpringMVC的配置与使用

    2021-06-24 12:30:52
  • 简单谈谈Java中的栈和堆

    2022-07-30 05:33:01
  • 本地jvm执行flink程序带web ui的操作

    2022-09-03 20:49:00
  • IDEA配置maven环境的详细教程(Unable to import maven project报错问题的解决)

    2022-04-09 13:53:40
  • Java项目实现寻找迷宫出路

    2022-10-05 14:08:46
  • Java+Swing实现医院管理系统的完整代码

    2023-03-17 00:40:21
  • java实现列表、集合与数组之间转化的方法

    2023-11-29 10:17:38
  • Java8新特性之默认方法(default)浅析

    2023-10-03 10:41:13
  • springboot2.0如何通过fastdfs实现文件分布式上传

    2022-03-20 16:49:24
  • SpringBoot 整合 ElasticSearch操作各种高级查询搜索

    2023-03-25 17:12:40
  • 学习Java的Date、Calendar日期操作

    2023-09-04 22:26:38
  • IDEA实现添加 前进后退 到工具栏的操作

    2021-08-30 21:34:48
  • 使用Java开发实现OAuth安全认证的应用

    2023-07-16 13:55:44
  • Android Studio gradle 编译提示‘default not found’ 解决办法

    2023-07-19 09:23:06
  • 如何安装java的运行环境IDEA

    2022-09-20 10:42:38
  • SpringTask实现定时任务方法讲解

    2022-06-11 11:43:42
  • 一篇文章带你入门Java数据类型

    2022-06-10 09:25:44
  • asp之家 软件编程 m.aspxhome.com