Encache配置

本文详细阐述了ehcache.xml配置文件的各个参数含义及作用,包括缓存的内存和磁盘消耗管理、运行时错误避免等优势。解析了默认缓存策略与样本缓存配置,介绍了如何在Spring中加载和使用此配置。

缓存的配置,可以将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的启用。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值