Spring實(shí)戰(zhàn)之清除緩存操作示例
本文實(shí)例講述了Spring實(shí)戰(zhà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.cache.annotation.CacheEvict; 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); } // 指定根據(jù)name、age參數(shù)清除緩存 @CacheEvict(value = "users") public void evictUser(String name, int age) { System.out.println("--正在清空"+ name + " , " + age + "對應(yīng)的緩存--"); } // 指定清除user緩存區(qū)所有緩存數(shù)據(jù) @CacheEvict(value = "users" , allEntries=true) public void evictAll() { System.out.println("--正在清空整個(gè)緩存--"); } }
五 測試類
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對象的2個(gè)帶緩存的方法,系統(tǒng)會(huì)緩存兩個(gè)方法返回的數(shù)據(jù) User u1 = us.getUsersByNameAndAge("孫悟空", 500); User u2 = us.getAnotherUser("豬八戒", 400); //調(diào)用evictUser()方法清除緩存中指定的數(shù)據(jù) us.evictUser("豬八戒", 400); // 由于前面根據(jù)"豬八戒", 400緩存的數(shù)據(jù)已經(jīng)被清除了, // 因此下面代碼會(huì)重新執(zhí)行,方法返回的數(shù)據(jù)將被再次緩存。 User u3 = us.getAnotherUser("豬八戒", 400); // ① System.out.println(u2 == u3); // 輸出false // 由于前面前面已經(jīng)緩存了參數(shù)為"孫悟空", 500的數(shù)據(jù), // 因此下面代碼不會(huì)重新執(zhí)行,直接利用緩存中的數(shù)據(jù)。 User u4 = us.getAnotherUser("孫悟空", 500); // ② System.out.println(u1 == u4); // 輸出true // 清空整個(gè)緩存。 us.evictAll(); // 由于整個(gè)緩存都已經(jīng)被清空,因此下面兩行代碼都會(huì)重新執(zhí)行 User u5 = us.getAnotherUser("孫悟空", 500); User u6 = us.getAnotherUser("豬八戒", 400); System.out.println(u1 == u5); // 輸出false System.out.println(u3 == u6); // 輸出false } }
六 測試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
--正在清空豬八戒 , 400對應(yīng)的緩存--
--正在執(zhí)行findAnotherUser()查詢方法--
false
true
--正在清空整個(gè)緩存--
--正在執(zhí)行findAnotherUser()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
false
false
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot2.x 集成騰訊云短信的詳細(xì)流程
本文主要對SpringBoot2.x集成騰訊云短信進(jìn)行簡單總結(jié),其中SpringBoot使用的2.4.5版本,本文通過業(yè)務(wù)流程圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-06-06基于RxPaparazzo實(shí)現(xiàn)圖片裁剪、圖片旋轉(zhuǎn)、比例放大縮小功能
這篇文章主要為大家詳細(xì)介紹了基于RxPaparazzo實(shí)現(xiàn)圖片裁剪、圖片旋轉(zhuǎn)、比例放大縮小功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法
這篇文章主要介紹了Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法,需要的朋友可以參考下2015-11-11SpringCloud OpenFeign超詳細(xì)講解模板化遠(yuǎn)程通信的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloudSpringboot集成OpenFeign實(shí)現(xiàn)模板化遠(yuǎn)程通信,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2022-07-07Java?設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解簡單工廠模式
簡單工廠模式是屬于創(chuàng)建型模式,又叫做靜態(tài)工廠方法(Static Factory Method)模式,但不屬于23種GOF設(shè)計(jì)模式之一。簡單工廠模式是由一個(gè)工廠對象決定創(chuàng)建出哪一種產(chǎn)品類的實(shí)例。簡單工廠模式是工廠模式家族中最簡單實(shí)用的模式,可以理解為是不同工廠模式的一個(gè)特殊實(shí)現(xiàn)2022-03-03Spring Cloud Alibaba Nacos 入門詳解
這篇文章主要介紹了Spring Cloud Alibaba Nacos入門詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03Java try catch finally異常處理組合詳解
這篇文章主要介紹了Java try catch finally異常處理組合詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Mybatis空值關(guān)聯(lián)的具體實(shí)現(xiàn)
在復(fù)雜的數(shù)據(jù)庫查詢中,處理空值關(guān)聯(lián)是一項(xiàng)常見的需求,本文就來介紹一下Mybatis空值關(guān)聯(lián)的具體實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07