资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Spring怎么清除缓存

这篇文章主要介绍“Spring怎么清除缓存”,在日常操作中,相信很多人在Spring怎么清除缓存问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring怎么清除缓存”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联建站成立于2013年,是专业互联网技术服务公司,拥有项目成都网站设计、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元华坪做网站,已为上家服务,为华坪各地企业和个人服务,联系电话:028-86922220

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

一 配置文件

             

二 属性文件

         

三 领域模型

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()查询方法--falsetrue--正在清空整个缓存----正在执行findAnotherUser()查询方法----正在执行findAnotherUser()查询方法--falsefalse

到此,关于“Spring怎么清除缓存”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


分享标题:Spring怎么清除缓存
文章URL:http://cdkjz.cn/article/pochhj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220