当前位置: 移动技术网 > IT编程>开发语言>Java > Spring实战之清除缓存操作示例

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网