Spring中怎么實現(xiàn)方法級別緩存,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
一 配置文件
<?xml version="1.0" encoding="GBK"?><beans xmlns="/tupian/20230522/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="/tupian/20230522/" xmlns:context="/tupian/20230522/" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//spring-beans-4.0.xsd /tupian/20230522/ /tupian/20230522//spring-cache-4.0.xsd /tupian/20230522/ /tupian/20230522//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"/> <!-- 配置名為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>
三 領域模型
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")public class UserServiceImpl implements UserService{ @Cacheable(value = "users1") public User getUsersByNameAndAge(String name, int age) { System.out.println("--正在執(zhí)行findUsersByNameAndAge()查詢方法--"); return new User(name, age); } @Cacheable(value = "users2") 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); // 由于getAnotherUser()方法使用另一個緩存區(qū), // 因此無法使用getUsersByNameAndAge()方法緩存區(qū)的數(shù)據(jù)。 User u2 = us.getAnotherUser("孫悟空", 500); System.out.println(u1 == u2); // 輸出false // getAnotherUser("孫悟空", 500)已經(jīng)執(zhí)行過一次,故下面代碼使用緩存 User u3 = us.getAnotherUser("孫悟空", 500); System.out.println(u2 == u3); // 輸出true }}
六 測試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法----正在執(zhí)行findAnotherUser()查詢方法--falsetrue
看完上述內(nèi)容,你們掌握Spring中怎么實現(xiàn)方法級別緩存的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁標題:Spring中怎么實現(xiàn)方法級別緩存-創(chuàng)新互聯(lián)
當前路徑:http://chinadenli.net/article14/dessde.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、建站公司、定制開發(fā)、網(wǎng)站維護、響應式網(wǎng)站、電子商務
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)