Spring實戰(zhàn)之類級別緩存實現(xiàn)與使用方法
本文實例講述了Spring實戰(zhàn)之類級別緩存實現(xià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" />
<!-- 配置默認的緩存區(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 實現(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")
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);
}
}
五 測試類
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對象的方法時會執(zhí)行該方法,并緩存方法的結(jié)果
User u1 = us.getUsersByNameAndAge("孫悟空", 500);
// 第二次調(diào)用us對象的方法時直接利用緩存的數(shù)據(jù),并不真正執(zhí)行該方法
User u2 = us.getAnotherUser("孫悟空", 500);
System.out.println(u1 == u2); // 輸出true
}
}
六 測試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法--
true
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java?ObjectMapper的使用和使用過程中遇到的問題
在Java開發(fā)中,ObjectMapper是Jackson庫的核心類,用于將Java對象序列化為JSON字符串,或者將JSON字符串反序列化為Java對象,這篇文章主要介紹了Java?ObjectMapper的使用和使用過程中遇到的問題,需要的朋友可以參考下2024-07-07
window?下?win10?jdk8安裝與環(huán)境變量的配置過程
這篇文章主要介紹了window?下?win10?jdk8安裝與環(huán)境變量的配置,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
SpringBoot導(dǎo)出PDF的四種實現(xiàn)方法詳解
在Spring?Boot應(yīng)用程序中實現(xiàn)PDF導(dǎo)出功能,可以選擇多種庫和技術(shù)棧,本文為大家整理了四種常見的方法,感興趣的小伙伴可以參考一下2025-02-02
springBoot Junit測試用例出現(xiàn)@Autowired不生效的解決
這篇文章主要介紹了springBoot Junit測試用例出現(xiàn)@Autowired不生效的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java 中jasperReport實現(xiàn)動態(tài)列打印的實現(xiàn)代碼
這篇文章主要介紹了Java 中jasperReport實現(xiàn)動態(tài)列打印的實現(xiàn)代碼的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Java中如何將?int[]?數(shù)組轉(zhuǎn)換為?ArrayList(list)
這篇文章主要介紹了Java中將?int[]?數(shù)組?轉(zhuǎn)換為?List(ArrayList),本文通過示例代碼給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12

