本文實(shí)例講述了Spring實(shí)戰(zhàn)之緩存使用condition操作。分享給大家供大家參考,具體如下:

為來安等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及來安網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、來安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
一 配置文件
<?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" />
<!-- 配置默認(rèn)的緩存區(qū) -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<!-- 配置名為users的緩存區(qū) -->
<cache name="users"
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 接口類
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)類
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" , condition="#age<100")
public class UserServiceImpl implements UserService
{
public User getUsersByNameAndAge(String name, int age)
{
System.out.println("--正在執(zhí)行findUsersByNameAndAge()查詢方法--");
return new User(name, age);
}
public User getAnotherUser(String name, int age)
{
System.out.println("--正在執(zhí)行findAnotherUser()查詢方法--");
return new User(name, age);
}
}
五 測(cè)試類
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)用方法時(shí)age參數(shù)不小于100,因此不會(huì)緩存,
// 所以下面兩次方法調(diào)用都會(huì)真正執(zhí)行這些方法。
User u1 = us.getUsersByNameAndAge("孫悟空", 500);
User u2 = us.getAnotherUser("孫悟空", 500);
System.out.println(u1 == u2); // 輸出false
// 調(diào)用方法時(shí)age參數(shù)小于100,因此會(huì)緩存,
// 所以下面第二次方法調(diào)用時(shí)不會(huì)真正執(zhí)行該方法,而是直接使用緩存數(shù)據(jù)。
User u3 = us.getUsersByNameAndAge("孫悟空", 50);
User u4 = us.getAnotherUser("孫悟空", 50);
System.out.println(u3 == u4); // 輸出true
}
}
六 測(cè)試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
false
--正在執(zhí)行findUsersByNameAndAge()查詢方法--
true
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
當(dāng)前標(biāo)題:Spring實(shí)戰(zhàn)之緩存使用condition操作示例
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article30/iigcso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、用戶體驗(yàn)、服務(wù)器托管、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)