Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法。分享給大家供大家參考,具體如下:
一 配置文件
<?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 通過(guò)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" />
<!-- 配置默認(rèn)的緩存區(qū) -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<!-- 配置名為users1的緩存區(qū) -->
<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>
三 領(lǐng)域模型
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 接口類(lèi)
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 實(shí)現(xiàn)類(lèi)
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("--正在執(zhí)行findUsersByNameAndAge()查詢(xún)方法--");
return new User(name, age);
}
@Cacheable(value = "users2")
public User getAnotherUser(String name, int age)
{
System.out.println("--正在執(zhí)行findAnotherUser()查詢(xún)方法--");
return new User(name, age);
}
}
五 測(cè)試類(lèi)
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);
// 第一次調(diào)用us對(duì)象的方法時(shí)會(huì)執(zhí)行該方法,并緩存方法的結(jié)果
User u1 = us.getUsersByNameAndAge("孫悟空", 500);
// 由于getAnotherUser()方法使用另一個(gè)緩存區(qū),
// 因此無(wú)法使用getUsersByNameAndAge()方法緩存區(qū)的數(shù)據(jù)。
User u2 = us.getAnotherUser("孫悟空", 500);
System.out.println(u1 == u2); // 輸出false
// getAnotherUser("孫悟空", 500)已經(jīng)執(zhí)行過(guò)一次,故下面代碼使用緩存
User u3 = us.getAnotherUser("孫悟空", 500);
System.out.println(u2 == u3); // 輸出true
}
}
六 測(cè)試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢(xún)方法--
--正在執(zhí)行findAnotherUser()查詢(xún)方法--
false
true
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring實(shí)戰(zhàn)之緩存使用condition操作示例
- Spring實(shí)戰(zhàn)之緩存使用key操作示例
- Spring實(shí)戰(zhàn)之類(lèi)級(jí)別緩存實(shí)現(xiàn)與使用方法
- Spring Boot集成Ehcache緩存解決方式
- spring結(jié)合redis如何實(shí)現(xiàn)數(shù)據(jù)的緩存
- Redis整合Spring結(jié)合使用緩存實(shí)例
- 詳解Spring緩存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring Boot 基于注解的 Redis 緩存使用詳解
- SpringBoot項(xiàng)目中使用redis緩存的方法步驟
- Spring Boot 中使用cache緩存的方法
- Spring實(shí)戰(zhàn)之清除緩存操作示例
相關(guān)文章
java簡(jiǎn)單網(wǎng)頁(yè)抓取的實(shí)現(xiàn)方法
這篇文章主要介紹了java簡(jiǎn)單網(wǎng)頁(yè)抓取的實(shí)現(xiàn)方法,詳細(xì)分析了與Java網(wǎng)頁(yè)抓取相關(guān)的tcp及URL相關(guān)概念,以及對(duì)應(yīng)的類(lèi)文件原理,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
JAVA Netty實(shí)現(xiàn)聊天室+私聊功能的示例代碼
Java實(shí)現(xiàn)簡(jiǎn)單棋盤(pán)存檔和讀取功能
springboot?@Validated的概念及示例實(shí)戰(zhàn)

