Spring实战之方法级别缓存用法示例

 更新时间:2020年01月14日 09:37:26   作者: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"/>
   <!-- 配置名为users1的缓存区 -->
  <cache name="users1"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600" />
  <cache name="users2"
    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.context.annotation.Scope;
@Service("userService")
public class UserServiceImpl implements UserService
{
  @Cacheable(value = "users1")
  public User getUsersByNameAndAge(String name, int age)
  {
    System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
    return new User(name, age);
  }
  @Cacheable(value = "users2")
  public User getAnotherUser(String name, int age)
  {
    System.out.println("--正在执行findAnotherUser()查询方法--");
    return new User(name, age);
  }
}

五 测试类

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对象的方法时会执行该方法,并缓存方法的结果
    User u1 = us.getUsersByNameAndAge("孙悟空", 500);
    // 由于getAnotherUser()方法使用另一个缓存区,
    // 因此无法使用getUsersByNameAndAge()方法缓存区的数据。
    User u2 = us.getAnotherUser("孙悟空", 500);
    System.out.println(u1 == u2); // 输出false
    // getAnotherUser("孙悟空", 500)已经执行过一次,故下面代码使用缓存
    User u3 = us.getAnotherUser("孙悟空", 500);
    System.out.println(u2 == u3); // 输出true
  }
}

六 测试结果

--正在执行findUsersByNameAndAge()查询方法--
--正在执行findAnotherUser()查询方法--
false
true

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

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

相关文章

  • 利用IDEA工具修改Maven多模块项目标识包名全过程记录

    利用IDEA工具修改Maven多模块项目标识包名全过程记录

    当我们为甲方服务提供软件开发服务时,需要按照甲方的要求去修改软件的标识,对于Maven项目来说就对应着groupId,一般地写对方公司的域名,如com.example,接下来通过本文给大家分享IDEA修改Maven多模块项目标识包名,感兴趣的朋友一起看看吧
    2022-09-09
  • JAVA中简单的for循环异常踩坑

    JAVA中简单的for循环异常踩坑

    这篇文章主要为大家介绍了JAVA中简单的for循环异常踩坑避雷详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • SpringBoot HikariCP连接池详解

    SpringBoot HikariCP连接池详解

    这篇文章主要介绍了SpringBoot2.0 中 HikariCP 数据库连接池原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 判断List和Map是否相等并合并List中相同的Map

    判断List和Map是否相等并合并List中相同的Map

    今天小编就为大家分享一篇关于判断List和Map是否相等并合并List中相同的Map,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 分析ThreadLocal内存泄漏问题

    分析ThreadLocal内存泄漏问题

    ThreadLocal的作用是提供线程内的局部变量,这种变量在线程生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量传递的复杂度,但是如果滥用ThreadLocal可能会导致内存泄漏,所以本文将为大家分析ThreadLocal内存泄漏问题
    2023-07-07
  • java中Vector类的常用方法详解

    java中Vector类的常用方法详解

    这篇文章主要为大家详细介绍了java中Vector类的常用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Java8中方便又实用的Map函数总结

    Java8中方便又实用的Map函数总结

    java8之后,常用的Map接口中添加了一些非常实用的函数,可以大大简化一些特定场景的代码编写,提升代码可读性,快跟随小编一起来看看吧
    2022-11-11
  • 一起学JAVA基础之运算符

    一起学JAVA基础之运算符

    计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量,下面这篇文章主要给大家介绍了关于JAVA基础之运算符的相关资料,需要的朋友可以参考下
    2022-01-01
  • Maven配置中repositories、distributionManagement、pluginRepositories用法及将已有jar包部署到私服

    Maven配置中repositories、distributionManagement、pluginRepositori

    这篇文章主要介绍了Maven配置中repositories、distributionManagement、pluginRepositories用法及将已有jar包部署到私服,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Java基础之SpringBoot整合knife4j

    Java基础之SpringBoot整合knife4j

    Swagger现在已经成了最流行的接口文档生成与管理工具,但是你是否在用的时候也在吐槽,它是真的不好看,接口测试的json数据没法格式化,测试地址如果更改了还要去改配置,接口测试时增加token验证是真的麻烦…针对Swagger的种种缺点,Knife4j就呼之欲出了.需要的朋友可以参考下
    2021-05-05

最新评论