微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ehcache整合springboot

一、引入依赖

<dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.4</version>
</dependency>

二、配置

@Configuration
public class EhcacheConfig {

    @Primary
    @Bean(name = "ehCacheManager")
    public CacheManager cacheManager(EhCacheManagerfactorybean bean) {
        CacheManager cm = bean.getobject();
        return cm;
    }

    @Bean
    public EhCacheManagerfactorybean ehCacheManagerfactorybean() {
        EhCacheManagerfactorybean cacheManagerfactorybean = new EhCacheManagerfactorybean();
        cacheManagerfactorybean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cacheManagerfactorybean.setShared(true);
        // 设置完属性后,cacheManagerfactorybean会执行afterProertiesSet()方法,
        // 所以不能在这里直接执行cacheManagerfactorybean.getobject(),直接执行的话,因为在afterPropertiesSet()方法之前执行,所以:getobject()会得到null值
        return cacheManagerfactorybean;
    }
}

三、配置ehcache 下面是针对linux下的配置,如果是windows需要修改

<?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 = "/home/cache"/>
    <!--

    eternal="false"意味着该缓存会死亡
    maxElementsInMemory="900"缓存的最大数目
    overflowTodisk="false" 内存不足时,是否启用磁盘缓存,如果为true则表示启动磁盘来存储,如果为false则表示不启动磁盘
                diskPersistent="false"
    timetoIdleSeconds="0"  当缓存的内容闲置多少时间销毁
    timetoLiveSeconds="60" 当缓存存活多少时间销毁(单位是秒,如果我们想设置2分钟的缓存存活时间,那么这个值我们需要设置120)
    memoryStoreevictionPolicy="LRU" /> 自动销毁策略
    -->
    <!--
       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 eternal="false"
    maxElementsInMemory="900"
    overflowTodisk="false" diskPersistent="false" timetoIdleSeconds="0"
    timetoLiveSeconds="60"
    memoryStoreevictionPolicy="LRU" />
    <!-- 这里的 users 缓存空间是为了下面的 demo 做准备 -->
    <cache
            name="killgoodDetail"
            eternal="false"
            maxElementsInMemory="5000"
            overflowTodisk="true"
            diskPersistent="false"
            timetoIdleSeconds="0"
            timetoLiveSeconds="86400"
            memoryStoreevictionPolicy="LRU" />
</ehcache>
        <!--<diskStore>==========当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)  -->
        <!--<diskStore path="">==用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index  -->
        <!--name=================缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)  -->
        <!--maxElementsOndisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大  -->
        <!--maxElementsInMemory==内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况  -->
        <!--1)若overflowTodisk=true,则会将Cache中多出的元素放入磁盘文件中  -->
        <!--2)若overflowTodisk=false,则根据memoryStoreevictionPolicy策略替换Cache中原有的元素  -->
        <!--eternal==============缓存中对象是否永久有效,即是否永驻内存,true时将忽略timetoIdleSeconds和timetoLiveSeconds  -->
        <!--timetoIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,认值是0表示可闲置时间无穷大,此为可选属性  -->
        <!--即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除  -->
        <!--timetoLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,认值是0表示可存活时间无穷大  -->
        <!--即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除  -->
        <!--overflowTodisk=======内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中)  -->
        <!--会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件名称是cache的名称,后缀名是data  -->
        <!--diskPersistent=======是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件  -->
        <!--这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存  -->
        <!--要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法  -->
        <!--diskExpiryThreadIntervalSeconds==磁盘缓存的清理线程运行间隔,认是120秒  -->
        <!--diskSpoolBufferSizeMB============设置diskStore(磁盘缓存)的缓存区大小,认是30MB  -->
        <!--memoryStoreevictionPolicy========内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存  -->
        <!--共有三种策略,分别为LRU(Least Recently Used 最近最少使用)、LFU(Less Frequently Used最不常用的)、FIFO(first in first out先进先出)  -->

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐