Encache配置

緩存的配置,可以將Cache的配置從代碼中剝離出來,通過encache.xml文件的形式配置。

這樣做有一下幾個好處: 
1、在同一個地方配置所有的Cache,這樣很容易管理Cache的內存和磁盤消耗。 
2、發佈時可更改Cache配置。 
3、可再安裝階段就檢查出配置錯誤信息,而避免了運行時錯誤。 
本文將會對ehcache.xml配置文件進行詳細的闡述。

<?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">
    <diskStore path="java.io.tmpdir"/>
    <cacheManagerEventListenerFactory class="" properties=""/>


    <!--
    Default Cache configuration. These settings will be applied to caches
    created programmatically using CacheManager.add(String cacheName).
    This element is optional, and using CacheManager.add(String cacheName) when
    its not present will throw CacheException

    The defaultCache has an implicit name "default" which is a reserved cache name.
    
   	默認的緩存策略
   	maxEntriesLocalHeap:堆內存中最大緩存對象數,0沒有限制
    eternal:對象是否永久有效
  	timeToIdleSeconds:設當緩存閒置 n 秒後銷燬
  	timeToLiveSeconds:當緩存存活 n 秒後銷燬
  	diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩衝區。  
  	maxEntriesLocalDisk:磁盤中的最大對象數,默認爲0不限制
  	diskExpiryThreadIntervalSeconds:磁盤中對象檢測線程運行時間間隔。標識對象狀態的線程多長時間運行一次。 
  	memoryStoreEvictionPolicy:緩存策略.默認策略是LRU(最近最少使用)。你可以設置爲FIFO(先進先出)或是LFU(較少使用)
    -->
    <defaultCache
            maxEntriesLocalHeap="10000"
            maxEntriesLocalDisk="10000000"
            eternal="false"
            diskSpoolBufferSizeMB="30"
            timeToIdleSeconds="0"
            timeToLiveSeconds="0"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <!--
    Sample caches. Following are some example caches. Remove these before use.
    -->

    <!--
    Sample cache named sampleCache1
    This cache contains a maximum in memory of 10000 elements, and will expire
    an element if it is idle for more than 5 minutes and lives for more than
    10 minutes.

    If there are more than 10000 elements it will overflow to the
    disk cache, which in this configuration will go to wherever java.io.tmp is
    defined on your system. On a standard Linux system this will be /tmp"
    -->
    <cache name="Cache_FMP"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="10000000"
           eternal="false"
           overflowToDisk="true"
           diskSpoolBufferSizeMB="1024"
           timeToIdleSeconds="0"
           timeToLiveSeconds="0"
           memoryStoreEvictionPolicy="LRU"
           transactionalMode="off">
    </cache>

</ehcache>

name: Cache的唯一標識名稱
maxEntriesLocalHeap: 堆內存中最大緩存對象數,0沒有限制
maxEntriesLocalDisk: 磁盤中的最大對象數,默認爲0不限制
eternal:elements是否永久有效,如果爲true,timeouts將被忽略,element將永不過期

以下爲可選屬性:

maxEntriesInCache:只能用在Terracotta distributed caches.
overflowToOffHeap:只能用於企業版本中
maxBytesLocalHeap:如果設置了這個屬性,maxEntriesLocalHeap將不能被利用
maxBytesLocalDisk:像maxBytesLocalHeap屬性
timeToIdleSeconds:失效前的空閒秒數,當eternal爲false時,這個屬性纔有效,0爲不限
timeToLiveSeconds:失效前的存活秒數,創建時間到失效時間的間隔爲存活時間,當eternal爲false時,這個屬性纔有效,0爲不限制
diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩衝區。
clearOnFlush:當調用flush()是否清除緩存,默認是
memoryStoreEvictionPolicy:內存回收策略,默認回收策略:最近最少使用Least Recently Used,先進先出First In First Out,Less Frequently Used使用頻率最低

項目中加載此配置文件

package com.stream.core.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@EnableCaching
@Configuration
public class CacheConfig {

	@Bean
	public EhCacheManagerFactoryBean cacheManagerFactoryBean() {
		EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
		bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
                // whether the EHCache CacheManager should be shared (as a singleton at the VM  level) or independent (typically local within the application)
		bean.setShared(true);
		return bean;
	}

	@Bean
	public CacheManager cacheManager(EhCacheManagerFactoryBean bean) {
		return new EhCacheCacheManager(bean.getObject());
	}

}

上面代碼中第一個bean是EhCacheManagerFactoryBean,EhCacheManagerFactoryBean中有三個屬性:
CacheManager類型的cacheManager、boolean類型的shared、表示ehcache配置信息的configLocation

這個類很簡單,從類名和封裝了的屬性上也不難看出Spring用這個類(afterPropertiesSet方法)來new出一個 CacheManager實例, CacheManager是Ehcache賴以運行的後防基地。不過看afterPropertiesSet方法,它有對shared的判斷,這是幹啥的? 看源碼註釋發現了這樣的描述: whether the EHCache CacheManager should be shared (as a singleton at the VM level) or independent (typically local within the application). 也說是說通過這個來設置cache的基地是這裏的Spring獨用,還是跟別的(如Hibernate的Ehcache共享)。
接下來據shared與否的設置,Spring分別通過CacheManager.create()或new CacheManager()方式來創建一個ehcache基地。    這樣一個EhCacheManagerFactoryBean創建完成,也就代表着一個CacheManager的啓用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章