SpringCache簡介
一個項目隨着時間的積累, 數據規模會越來越大, 對數據的訪問需求也越來越高, 數據庫的查詢等操作的使用量也會越來越大.但是數據庫的性能是有上限的.因此如何提高數據查詢的性能, 分擔數據庫的訪問壓力也成爲了一個重要的功能.
而緩存就是實際工作中經常會被使用的一種分擔數據庫訪問壓力,提高數據查詢性能的方法.
從3.1開始Spring引入了對Cache的支持。其使用方法和原理都類似於Spring對事務管理的支持。Spring Cache是作用在方法上的,其核心思想是:當我們在調用一個緩存方法時會把該方法參數和返回結果作爲一個鍵值存放在緩存中,等到下次利用同樣的參數調用該方法時將不再執行該方法,而是直接從緩存中獲取結果進行返回。
Spring Cache, 本質上不是一個具體的緩存實現方案, 而是一個對緩存使用的抽象, 只要通過一定的配置和既有的代碼,註解,就可以利用各種緩存方案 如EHCache,Redis等, 來暫時存儲數據.
上面這部分簡介摘抄自 SpringCache簡單梳理 這篇文章
要想使用SpringCache,原則上也是需要實現SpringCache提供的相關接口的。
//spring cache的緩存接口
org.springframework.cache.Cache
org.springframework.cache.CacheManager
但spring框架已經提供了這2個接口的很多實現了,本次我們想使用Ehcache做緩存,spring提供的EhCacheCache和EhCacheCacheManager分別實現了上面2個接口。
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
配置ehcache.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" dynamicConfig="false">
<diskStore path="D:/test_back_up/cach"/>
<!--用來緩存Doc的緩存,Doc無法序列化,所以不用寫在磁盤上-->
<cache name="DocCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="1800"
overflowToDisk="false"
maxElementsOnDisk="1"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<!--用來緩存shiro認證信息和權限信息的,因爲shiro使用了redis來緩存,所以這2個緩存作廢了-->
<!--<cache name="authorizationCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="100000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<cache name="authenticationCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="100000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>-->
<!--
timeToLiveSeconds 允許存活時間,單位是秒
解釋:從對象被放到緩存中到被刪除的最大時間,比如是100s,從對象放進緩存開始計時,只要超過了100s,就從緩存中將對象刪除。
只要這個時間到了,不管對象是否有效,都會將對象刪除。
timeToIdleSeconds 允許閒置時間,單位是秒
解釋:從最後一次訪問開始計時,超過該時間,就將對象刪除。-->
<!--
name:緩存名稱。
maxElementsInMemory:緩存最大個數。
eternal:對象是否永久有效,一但設置了,timeout將不起作用。
timeToIdleSeconds:設置對象在失效前的允許閒置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。
timeToLiveSeconds:設置對象在失效前允許存活時間(單位:秒)。最大時間介於創建時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
overflowToDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。
diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩衝區。
maxElementsOnDisk:硬盤最大緩存個數。
diskPersistent:是否緩存虛擬機重啓期數據 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置爲FIFO(先進先出)或是LFU(較少使用)。
clearOnFlush:內存數量最大時是否清除。
-->
<defaultCache name="defaultCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="100000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
配置 net.sf.ehcache.CacheManager
@Configuration
@EnableConfigurationProperties(CacheProperties.class)
public class EhcacheConfig {
/**
* 設置爲共享模式
*/
/*@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setShared(true);
return ehCacheManagerFactoryBean;
}*/
@Bean
@ConditionalOnMissingBean
CacheManager myEhCacheCacheManager(CacheProperties cacheProperties) {
Resource location = cacheProperties.resolveConfigLocation(cacheProperties.getEhcache().getConfig());
if (location != null) {
return EhCacheManagerUtils.buildCacheManager(location);
}
return EhCacheManagerUtils.buildCacheManager();
}
}
配置org.springframework.cache.ehcache.EhCacheCacheManager
@Configuration
public class SpringCacheConfig {
/**
* EhCacheCacheManager實現了spring的CacheManager接口
*/
@Bean
EhCacheCacheManager ehCacheCacheManager(net.sf.ehcache.CacheManager myEhCacheCacheManager){
return new EhCacheCacheManager(myEhCacheCacheManager);
}
}