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程序设计有所帮助。

相关文章

  • 如何两步解决maven依赖导入失败的问题

    如何两步解决maven依赖导入失败的问题

    这篇文章主要介绍了如何两步解决maven依赖导入失败的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Spring AOP入门Demo分享

    Spring AOP入门Demo分享

    这篇文章主要介绍了Spring AOP入门Demo分享,涉及创建maven项目,编写切面类,通过bean配置关联等相关内容,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • Java中使用正则表达式获取网页中所有图片的路径

    Java中使用正则表达式获取网页中所有图片的路径

    这篇文章主要介绍了Java中使用正则表达式获取网页中所有图片的路径,本文直接给出实例代码,需要的朋友可以参考下
    2015-06-06
  • Java案例分享-集合嵌套

    Java案例分享-集合嵌套

    这篇文章主要介绍了Java案例分享-集合嵌套,通过案例创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历,实际操作内容需要的小伙伴可以参考一下
    2022-04-04
  • 教你用Java实现RSA非对称加密算法

    教你用Java实现RSA非对称加密算法

    今天带各位小伙伴学习怎么用Java实现RSA非对称加密算法,文中有非常详细的解释及代码示例,对正在学java算法的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • java文件上传技术深入剖析

    java文件上传技术深入剖析

    这篇文章主要为大家详细介绍了java文件上传技术深入剖析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Spring中常见的7种BeanDefinition详解

    Spring中常见的7种BeanDefinition详解

    在 Spring 容器中,我们广泛使用的是一个一个的 Bean,BeanDefinition 从名字上就可以看出是关于 Bean 的定义,下面就跟随小编一起深入了解一下常见的7中BeanDefinition吧
    2023-09-09
  • 关于IDEA 2020使用 mybatis-log-plugin插件的问题

    关于IDEA 2020使用 mybatis-log-plugin插件的问题

    这篇文章主要介绍了关于IDEA 2020使用 mybatis-log-plugin插件的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Netty分布式Server启动流程服务端初始化源码分析

    Netty分布式Server启动流程服务端初始化源码分析

    本章主要讲解server启动的关键步骤, 读者只需要了解server启动的大概逻辑, 知道关键的步骤在哪个类执行即可, 并不需要了解每一步的运作机制, 之后会对每个模块进行深度分析
    2022-03-03
  • SpringBoot集成RocketMQ的使用示例

    SpringBoot集成RocketMQ的使用示例

    RocketMQ是阿里巴巴开源的一款消息中间件,性能优秀,功能齐全,被广泛应用在各种业务场景,本文就来介绍一下SpringBoot集成RocketMQ的使用示例,感兴趣的可以了解一下
    2023-11-11

最新评论