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

作者:cakincqm 时间:2022-10-20 02:00:32 

本文实例讲述了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程序设计有所帮助。

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

标签:Spring,方法级别缓存
0
投稿

猜你喜欢

  • C#缩略图多路径多格式保存的实例

    2021-10-29 18:12:36
  • Java字符串编码解码性能提升的技巧分享

    2022-01-30 08:45:13
  • c#同步两个子目录文件示例分享 两个文件夹同步

    2022-01-25 05:03:03
  • Java中遍历Map的六种方法实现

    2022-03-21 13:30:58
  • Spring中AOP概念与两种动态代理模式原理详解

    2023-04-13 01:21:54
  • C#值类型、引用类型中的Equals和==的区别浅析

    2022-09-05 23:04:24
  • Android模拟器"Failed To Allocate memory 8"错误如何解决

    2021-12-09 05:04:48
  • kafka监听问题的解决和剖析

    2021-06-28 04:41:25
  • 使用newInstance()来实例化fragment并传递数据操作

    2022-07-11 15:41:57
  • Spark SQL 编程初级实践详解

    2022-06-13 18:37:56
  • 基于Spring-Security自定义登陆错误提示信息

    2021-09-20 17:33:40
  • C# Winform 调用系统接口操作 INI 配置文件的代码

    2023-03-04 11:49:54
  • Java静态代理和动态代理总结

    2022-01-02 18:49:21
  • C#中实现插入、删除Excel分页符的方法

    2022-05-27 02:51:16
  • Java实现断点续传功能的示例代码

    2021-12-27 10:07:56
  • Java png图片修改像素rgba值的操作

    2022-08-11 06:57:21
  • android 布局属性详解

    2021-07-25 13:19:49
  • Java类加载基本过程详细介绍

    2023-04-15 10:54:14
  • c# 委托的本质是什么

    2022-01-31 17:55:46
  • Java设计模式编程之解释器模式的简单讲解

    2022-01-24 16:03:32
  • asp之家 软件编程 m.aspxhome.com