ehcache二级缓存框架使用总结

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。ehcache缓存框架具备以下特点:

1. 快速
2. 简单
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现

下面介绍如何在SSM开源框架中使用Ehcache缓存框架:(关于SSM框架如何搭建,这里不做介绍
具体搭建需要四步:
1.添加相关jar包:除SSM框架所需jar包外,还需mybatis-ehcache.jar,ehcache-core.jar.
2.在classpath目录下添加ehcache.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    <diskStore path="java.io.tmpdir" />
    <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
    <cache name="testCache" eternal="false" maxElementsInMemory="100"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" />
</ehcache>
标识说明:

name:Cache的唯一标识。

maxElementsInMemory:内存中最大缓存对象数。

maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。

eternal:Element是否永久有效,一旦设置了,timeout将不起作用。

overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache会将Element写到磁盘中。

timeToIdleSeconds:设置Element在失效前允许闲置时间。仅当element不是永久失效时使用,可选属性,默认值是0,也可以是可闲置时间无穷大。

diskPersistent:是否缓存虚拟机重启期数据。

diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

diskSpoolBufferSizeMB:这个参数设置DikStore(磁盘缓存)的缓存区大小。默认是30MB。每个Ehcache都应该有自己的一个缓冲区。

memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近很少使用)可以设置为FIFO(先进先出)或是LFU。

3.在applicationContext.xml新增以下内容:
 <!-- MyBatis使用ehcache缓存 start -->
    <bean id="ehCacheManager"
          class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/> <!-- 这里是关键!!!没有必错  -->
    </bean>

    <!-- 配置 Spring 的 EhCacheCacheManager,须要 spring-context-support 的支持 -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehCacheManager"/>
    </bean>

    <!-- end MyBatis使用ehcache缓存 -->
    <cache:annotation-driven  cache-manager="cacheManager" />
4.mapper.xml文件中使用<cache>标签配置ehcache,加入以下配置:
<mapper namespace="dicMapper">
	<cache type="org.mybatis.caches.ehcache.LoggingEhcache">
		<property name="timeToIdleSeconds" value="3600" />
		<property name="timeToLiveSeconds" value="3600" />
		<property name="maxEntriesLocalHeap" value="1000" />
		<property name="maxEntriesLocalDisk" value="10000000" />
		<property name="memoryStoreEvictionPolicy" value="LRU" />
	</cache>
</mapper>
注:
LogginEhcache这个配置会打印log,如果不想打印log信息,可以使用EhcacheCache
1. useCache属性
mapper.xml里面的操作是全局,默认为useCache=“true”都会有作用;假如某个业务是不要缓存的,可以在当前业务下加上useCache=“false”
2.flushcache属性
当为select语句时:
flushCache默认为false,表示任何时候语句被调用,都不会去清空本地缓存和二级缓存。
当为insert、update、delete语句时:
flushCache默认为true,表示任何时候语句被调用,都会导致本地缓存和二级缓存被清空。







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值