Springboot使用ehcache缓存

1.创建ehcache.xml文件
src/main/resources下

<?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">

	<!-- 使用时,在DAO相关方法前加 @Cacheable(加入缓存)或@CacheEvict(从缓存中清除) -->

	<!-- 磁盘存储:将缓存中暂时不使用的对象转移到硬盘,类似于Windows系统的虚拟内存
		path	指定在硬盘上存储对象的路径。可以配置的目录有:
			user.home				用户的Home目录
			user.dir				用户当前的工作目录
			java.io.tmpdir			默认的临时目录
			ehcache.disk.store.dir	ehcache的配置目录
			绝对路径					如:d:\\ehcache
		查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
	-->
	 <diskStore path="java.io.tmpdir"/>
  
  <!-- defaultCache:					echcache的默认缓存策略。如果不加特殊说明,则所有对象按照此配置项处理
  		name							指定当前CacheManager的名称。
		maxElementsInMemory				在内存中缓存最大数量(最多存储多少个对象),0=不限
		eternal							对象是否永不过期 (指定true则下面两项配置需为0无限期)
		timeToIdleSeconds				最大的等待时间(秒),0=不限。对象自上次访问后的存活时间。默认值为0
		timeToLiveSeconds				最大的存活时间(秒),0=不限。对象自加入到缓存后的存活时间。默认值为0
		maxElementsOnDisk="10000000"	在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
		overflowToDisk					如果内存中数据超过内存限制,是否要缓存到磁盘上。
		diskExpiryThreadIntervalSeconds 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
		memoryStoreEvictionPolicy		如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
			1、FIFO ,first in first out (先进先出).
	 		2、LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
	 		3、LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
		diskPersistent  				是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
		diskSpoolBufferSizeMB			DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
		
		<persistence strategy="localTempSwap"/> 表示Cache的持久化,它只有一个属性strategy,表示当前Cache对应的持久化策略。其可选值如下:
			l  localTempSwap:当堆内存或者非堆内存里面的元素已经满了的时候,将其中的元素临时的存放在磁盘上,一旦重启就会消失。
	
			l  localRestartable:该策略只对企业版Ehcache有用。它可以在重启的时候将堆内存或者非堆内存里面的元素持久化到硬盘上,重启之后再从硬盘上恢复元素到内存中。
	
			l  none:不持久化缓存的元素
	
			l  distributed:该策略不适用于单机,是用于分布式的。
	-->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="0"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <!-- 指定的缓存配置 -->
    <cache name="configs"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="0"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>

	
</ehcache>

2.查询放入指定缓存模块

@Cacheable(value="configs", key="#configId")
	public SysConfigPO getById(Integer configId)
	{
		return baseMapper.selectById(configId);
	}

3.修改删除缓存


	@CacheEvict(value="configs", key="#configId")
	public int updValueById(Integer configId, String value)
	{
		return baseMapper.updateById( SysConfigPO.of(configId, value) );
	}

4.配置文件

spring:
  cache:
    type: ehcache
    ehcache:
      config: classpath:/ehcache.xml

版权声明:本文为weixin_37460672原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。