詳解Spring MVC 集成EHCache緩存
廢話少說(shuō),直接上代碼:
ehcache.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache dynamicConfig="false" monitoring="off" updateCheck="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<!-- 定義緩存策略
eternal="false" // 元素是否永恒,如果是就永不過(guò)期(必須設(shè)置)
maxEntriesLocalHeap="1000" // 堆內(nèi)存中最大緩存對(duì)象數(shù),0沒(méi)有限制(必須設(shè)置)
overflowToDisk="false" // 當(dāng)緩存達(dá)到maxElementsInMemory值是,是否允許溢出到磁盤(必須設(shè)置)
diskPersistent="false" // 磁盤緩存在VM重新啟動(dòng)時(shí)是否保持(默認(rèn)為false)
timeToIdleSeconds="0" // 導(dǎo)致元素過(guò)期的訪問(wèn)間隔(秒為單位). 當(dāng)eternal為false時(shí),這個(gè)屬性才有效,0表示可以永遠(yuǎn)空閑,默認(rèn)為0
timeToLiveSeconds="600" // 元素在緩存里存在的時(shí)間(秒為單位). 0 表示永遠(yuǎn)存在不過(guò)期
memoryStoreEvictionPolicy="LFU" // 當(dāng)達(dá)到maxElementsInMemory時(shí),如何強(qiáng)制進(jìn)行驅(qū)逐默認(rèn)使用"最近使用(LRU)"策略,其它還有先入先出FIFO,最少使用LFU,較少使用LRU
-->
<!--
1)maxElementsInMemory(正整數(shù)):在內(nèi)存中緩存的最大對(duì)象數(shù)量
2)maxElementsOnDisk(正整數(shù)):在磁盤上緩存的最大對(duì)象數(shù)量,默認(rèn)值為0,表示不限制。
3)eternal:設(shè)定緩存對(duì)象保存的永久屬性,默認(rèn)為 false 。當(dāng)為 true 時(shí) timeToIdleSeconds、timeToLiveSeconds 失效。
4)timeToIdleSeconds(單位:秒): 對(duì)象空閑時(shí)間,指對(duì)象在多長(zhǎng)時(shí)間沒(méi)有被訪問(wèn)就會(huì)失效。只對(duì)eternal為false的有效。默認(rèn)值0,表示一直可以訪問(wèn)。
5)timeToLiveSeconds(單位:秒): 對(duì)象存活時(shí)間,指對(duì)象從創(chuàng)建到失效所需要的時(shí)間。只對(duì)eternal為false的有效。默認(rèn)值0,表示一直可以訪問(wèn)。
6)overflowToDisk:如果內(nèi)存中數(shù)據(jù)超過(guò)內(nèi)存限制,是否要緩存到磁盤上。
7)diskPersistent:是否在磁盤上持久化。指重啟jvm后,數(shù)據(jù)是否有效。默認(rèn)為false。
8)diskSpoolBufferSizeMB(單位:MB): DiskStore使用的磁盤大小,默認(rèn)值30MB。每個(gè)cache使用各自的DiskStore。
9)memoryStoreEvictionPolicy:如果內(nèi)存中數(shù)據(jù)超過(guò)內(nèi)存限制,向磁盤緩存時(shí)的策略。默認(rèn)值LRU,可選FIFO、LFU。
FIFO(first in first out):先進(jìn)先出
LFU(Less Frequently Used):最少被使用,緩存的元素有一個(gè)hit屬性,hit值最小的將會(huì)被清除緩存。
LRU(Least Recently Used)默認(rèn)策略:最近最少使用,緩存的元素有一個(gè)時(shí)間戳,當(dāng)緩存容量滿了,而又需要騰出地方來(lái)緩存新的元素的時(shí)候,那么現(xiàn)有緩存元素中時(shí)間戳離當(dāng)前時(shí)間最遠(yuǎn)的元素將被清除緩存。
10) maxEntriesLocalHeap 堆內(nèi)存中最大緩存對(duì)象數(shù)
-->
<diskStore path="java.io.tmpdir"></diskStore>
<defaultCache
eternal="false"
maxEntriesLocalHeap="0"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsInMemory="10000"
overflowToDisk="true"
diskPersistent="true"
/>
<cache
name="userCache"
maxEntriesLocalHeap="10000"
/>
<cache
name="studentCache"
maxEntriesLocalHeap="10000"
/>
</ehcache>
需要增加的JAR包

springmvc.xml 需要在beans增加以下
xmlns:cache="http://www.springframework.org/schema/cache" http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
增加bean
<!-- 啟用緩存注解功能(請(qǐng)將其配置在Spring主配置文件中) --> <cache:annotation-driven cache-manager="cacheManager"/> <!-- Spring提供的基于的Ehcache實(shí)現(xiàn)的緩存管理器 --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:config/ehcache.xml"/> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean>
EHCacheUtils 操作類
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
* 操作緩存類
*
* @author jiangadam
*/
public class EhcacheUtils {
private static final String path = "/config/ehcache.xml"; // EHCache 的配置文件地址
private CacheManager manager;
private static EhcacheUtils ehCache;
private EhcacheUtils(String path) {
manager = CacheManager.create(getClass().getResource(path));
}
public static EhcacheUtils getInstance() {
if (ehCache == null) {
ehCache = new EhcacheUtils(path);
}
return ehCache;
}
/**
* 緩存一個(gè)對(duì)象
*
* @param cacheName
* 緩存的名字
* @param key
* 緩存的KEY
* @param value
* 緩存的值
*/
public void put(String cacheName, String key, Object value) {
Cache cache = manager.getCache(cacheName);
Element element = new Element(key, value);
cache.put(element);
}
/**
* 獲取一個(gè)緩存的對(duì)象,沒(méi)有返回NULL
*
* @param cacheName
* @param key
* @return
*/
public Object get(String cacheName, String key) {
Cache cache = manager.getCache(cacheName);
Element element = cache.get(key);
return element == null ? null : element.getObjectValue();
}
public Cache get(String cacheName) {
return manager.getCache(cacheName);
}
public void remove(String cacheName, String key) {
Cache cache = manager.getCache(cacheName);
cache.remove(key);
}
}
PUT 寫入緩存

GET 獲取緩存的數(shù)據(jù)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在springboot中使用注解將值注入?yún)?shù)的操作
這篇文章主要介紹了在springboot中使用注解將值注入?yún)?shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
SpringIOC框架的簡(jiǎn)單實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringIOC框架簡(jiǎn)單實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用Spring,感興趣的朋友可以了解下2021-05-05
深入理解Java虛擬機(jī) JVM 內(nèi)存結(jié)構(gòu)
本節(jié)將會(huì)介紹一下JVM的內(nèi)存結(jié)構(gòu),JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)的各個(gè)組成部分:堆,方法區(qū),程序計(jì)數(shù)器,Java虛擬機(jī)棧,本地方法棧,還會(huì)對(duì)Java堆的分代劃分做個(gè)簡(jiǎn)單的介紹2021-09-09
Spring Boot JPA中java 8 的應(yīng)用實(shí)例
這篇文章主要介紹了Spring Boot JPA中java 8 的應(yīng)用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
異常try?catch的常見(jiàn)四類方式(案例代碼)
這篇文章主要介紹了異常try?catch的常見(jiàn)四類方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

