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

 更新时间:2020年01月14日 09:43:37   作者:cakincqm  
这篇文章主要介绍了Spring实战之清除缓存操作,结合实例形式详细分析了spring清除缓存操作具体步骤、配置、领域模型及相关使用技巧,需要的朋友可以参考下

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

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

相关文章

  • SpringBoot2.x 集成腾讯云短信的详细流程

    SpringBoot2.x 集成腾讯云短信的详细流程

    本文主要对SpringBoot2.x集成腾讯云短信进行简单总结,其中SpringBoot使用的2.4.5版本,本文通过业务流程图实例代码相结合给大家介绍的非常详细,需要的朋友参考下吧
    2021-06-06
  • IDEA中配置文件模板的添加方法

    IDEA中配置文件模板的添加方法

    这篇文章主要介绍了IDEA中配置文件模板的添加方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • 基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能

    基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能

    这篇文章主要为大家详细介绍了基于RxPaparazzo实现图片裁剪、图片旋转、比例放大缩小功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法

    Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法

    这篇文章主要介绍了Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法,需要的朋友可以参考下
    2015-11-11
  • SpringCloud OpenFeign超详细讲解模板化远程通信的实现

    SpringCloud OpenFeign超详细讲解模板化远程通信的实现

    这篇文章主要介绍了SpringCloudSpringboot集成OpenFeign实现模板化远程通信,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2022-07-07
  • java树形菜单对象生成

    java树形菜单对象生成

    这篇文章主要为大家详细介绍了java树形菜单对象生成,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • Java 设计模式以虹猫蓝兔的故事讲解简单工厂模式

    Java 设计模式以虹猫蓝兔的故事讲解简单工厂模式

    简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现
    2022-03-03
  • Spring Cloud Alibaba Nacos 入门详解

    Spring Cloud Alibaba Nacos 入门详解

    这篇文章主要介绍了Spring Cloud Alibaba Nacos入门详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Java try catch finally异常处理组合详解

    Java try catch finally异常处理组合详解

    这篇文章主要介绍了Java try catch finally异常处理组合详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Mybatis空值关联的具体实现

    Mybatis空值关联的具体实现

    在复杂的数据库查询中,处理空值关联是一项常见的需求,本文就来介绍一下Mybatis空值关联的具体实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07

最新评论