Spring实战之方法级别缓存用法示例
作者:cakincqm 发布时间:2022-10-20 02:00:32
标签: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程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101614197


猜你喜欢
- mapper.xml使用循环语句mapper.java,传的参数是mapList<实体类> getList(Map<Str
- GridView实现桌面图标显示案例,供大家参考,具体内容如下用法与ListView类似,需要以下几步:1、定义实体类2、自定义适配器继承B
- 前言Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性。而这样的方
- 有关临时对象的生命周期有三种情况:1)一般情况:临时性对象的被摧毁,应该是对完整表达式(full-expression)求值过程中的最后一个
- 需求:用户和账户一对一关系,查询账户时实现用户的延迟加载思路:根据id查询,需要延迟加载的一方1、用户实体类package com.yl.b
- 自己闲下来时间写的一个课表控件,使用的自定义LinearLayout,里面View都是用代码实现的,最终效果如下图,写的可能有问题希望多多指
- 开篇Druid号称是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。作为日常使用较多的数据库连接组件,纯粹个人兴趣研究下
- 本文实例讲述了C#实现鼠标移动到曲线图上显示值的方法。分享给大家供大家参考。具体实现方法如下:一、问题:完成折线图报表后,产品经理要求把折线
- 1.先看源码文档/** * Indicates that an annotation type is automatically inher
- spinner组件有点类型于HTML中的下拉框<Select></select>的样子,让用户每次从下拉框中选取一个
- 1. 概述为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。2. 模式中的角色2.1 外
- Java选择的泛型类型叫做类型擦除式泛型。什么是类型擦除式泛型呢?就是Java语言中的泛型只存在于程序源码之中,在编译后的字节码文件里,则全
- 概述java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如"abc" )都可以被看作是实现
- 一、定义一个配置类,自定义RedisTemplate的序列化方式@Configurationpublic class RedisConfig
- 在Android开发中我们很多地方都用到了方法的回调,回调就是把方法的定义和功能导入实现分开的一种机制,目的是为了解耦他的本质是基于观察者设
- 在进行详解之前,我想先声明一下,本次我们进行讲解说明的是 Kafka 消息存储的信息文件内容,不是所谓的 Kafka 服务器运行产生的日志文
- 流,就是一系列的数据。当不同介质之间有数据交互的时候,JAVA就使用流来实现。数据源可以是文件,还可以是数据库、网络甚至其他的程序。比如读取
- 最近学习Spring,一直不太明白Srping的切面编程中的的argNames的含义,经过学习研究后,终于明白,分享一下需要监控的类:pac
- Jfreechart本身不能生成SVG图形,但是可以借助另外一个东西,辅助生成.好像是这个:batik ,具体代码请看下文一:Java生成s
- 说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们1.线程(T