Spring实战之类级别缓存实现与使用方法

 更新时间:2020年01月13日 08:38:11   作者: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"/>
   <!-- 配置名为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.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);
  }
}

五 测试类

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);
    // 第二次调用us对象的方法时直接利用缓存的数据,并不真正执行该方法
    User u2 = us.getAnotherUser("孙悟空", 500);
    System.out.println(u1 == u2); // 输出true
  }
}

六 测试结果

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

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

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

相关文章

  • java开发ServiceLoader实现机制及SPI应用

    java开发ServiceLoader实现机制及SPI应用

    这篇文章主要为大家介绍了java开发ServiceLoader实现机制及SPI应用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • SpringBoot中Controller参数与返回值的用法总结

    SpringBoot中Controller参数与返回值的用法总结

    这篇文章主要介绍了SpringBoot中Controller参数与返回值的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java 对10个数进行排序的实现代码

    Java 对10个数进行排序的实现代码

    可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第二个元素与后8个进行比较,并进行交换
    2017-02-02
  • Java女装商城系统的实现流程

    Java女装商城系统的实现流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+SSM+jsp+mysql+maven实现一个女装商城系统,大家可以在过程中查缺补漏,提升水平
    2021-11-11
  • SpringBoot实现抽奖算法的示例代码

    SpringBoot实现抽奖算法的示例代码

    这篇文章主要为大家详细介绍了如何通过SpringBoot实现抽奖算法,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以了解一下
    2023-06-06
  • idea导入jar包的详细图文教程

    idea导入jar包的详细图文教程

    这篇文章主要给大家介绍了关于idea导入jar包的详细图文教程,文中通过图文将导入的步骤介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-03-03
  • Springboot消除switch-case过程解析

    Springboot消除switch-case过程解析

    这篇文章主要介绍了Springboot消除switch-case过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • SpringMVC适配器模式代码示例

    SpringMVC适配器模式代码示例

    这篇文章主要介绍了SpringMVC适配器模式代码示例,涉及模拟springmvc的Java代码等相关内容,具有一定借鉴价值,需要的朋友可以参考下。
    2017-11-11
  • Session过期后自动跳转到登录页面的实例代码

    Session过期后自动跳转到登录页面的实例代码

    这篇文章主要介绍了Session过期后自动跳转到登录页面实例代码,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Java利用AQS实现自定义锁

    Java利用AQS实现自定义锁

    本文主要介绍了Java利用AQS实现自定义锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07

最新评论