Springboot使用Ehcache做持久化缓存

最近在做的功能模块需要长久存储一项(仅一项)且即使服务重启后也可以查询到的数据,每天更新一次。

一开始不知道ehcache能做持久化缓存⊙﹏⊙‖,心想缓存不能用,那只能数据表了,但是因为一项数据建一张数据表好像有些没必要,瞬间没了思路。后来向同事请教,同事说可以尝试用ehcache持久化缓存。于是,我又get新技能了哈~~记录一下吧。

1、导入jar包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.6.11</version>
</dependency>

2、创建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: 为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置-->
    <diskStore path="/EcloudCMP/consume_huawei" />


    <!--
            name:缓存名称
            maxElementsInMemory:内存中最大缓存对象数
            maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大
            eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false
            overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
            diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。
            diskPersistent:是否缓存虚拟机重启期数据
            diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒
            timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态
            timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义
            memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
     -->

    <!--默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。-->
    <defaultCache
            maxElementsInMemory="100"
            eternal="true"
            overflowToDisk="true"/>

    <!--自定缓存策略,为自定义的缓存策略-->
    <cache name="bill"
           maxElementsInMemory="1"
           eternal="true"
           overflowToDisk="true"
           maxElementsOnDisk="0"
           diskPersistent="true">
           <!-- 比非持久化的配置多了这个 -->
           <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
           <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
    </cache>

</ehcache>

3、创建配置类,初始化Cache所需的Bean,当然也可以通过把ehcache.xml配置到application.yml中实现同等效果。

package cn.com.demo.config;

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;

/**
 * @Author: Peacock__
 * @Date: 2019/6/17 15:28
 */
@Configuration
// 标注启动了缓存
@EnableCaching
public class CacheConfiguration {
    /*
     * ehcache 主要的管理器
     */
    @Bean(name = "appEhCacheCacheManager")
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
        System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY,"true");
        return new EhCacheCacheManager (bean.getObject ());
    }

    /*
     * 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.
     */
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
        cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
        cacheManagerFactoryBean.setShared (true);
        return cacheManagerFactoryBean;
    }
}

4、存,取数据到缓存

注意:需要保证存入缓存的数据都是可序列化的,如果存的是自己的对象,就实现一下Serializable

package cn.com.demo.common.utils;

import cn.com.swancloud.huaweicloud.client.customer.bill.model.CustomerProductBillResponse;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Peacock__
 * @Date: 2019/6/14 17:30
 */
@Component
public class BillEmailSchedule {

    @Autowired
    private CacheManager cacheManager;

    private static final String cache_key_module = "bill";

    private static final String cache_key_name = "yesterday_consume_detail";

   
    /**
     * 从缓存中获取数据
     * @return
     * @throws IOException
     */
    private Map<String,BigDecimal> getCache() throws IOException {
        Map<String,BigDecimal> map = new HashMap<>();
        //若cacheManager被关闭,则重新创建(因为我存完数据后手动调用了shutdown,所以这里判断一下是否关闭)
        if(cacheManager == null || cacheManager.getStatus().intValue() != 1){
            cacheManager = new CacheManager(new ClassPathResource("ehcache.xml").getInputStream());
        }
        Cache cache = cacheManager.getCache(cache_key_module);
        if(cache != null){
            Element element = cache.get(cache_key_name);
            if(element != null){
                Object objectValue = element.getObjectValue();
                map = (Map)objectValue;
            }
        }
        return map;
    }

    /**
     * 数据存入缓存
     * @param data
     * @throws IOException
     */
    private void putCache(List<CustomerProductBillResponse.BillSumRecordInfo> data) throws IOException {
        //若cacheManager被关闭,则重新创建
        if(cacheManager == null || cacheManager.getStatus().intValue() != 1){
            cacheManager = new CacheManager(new ClassPathResource("ehcache.xml").getInputStream());
        }
        Cache cache = cacheManager.getCache(cache_key_module);
        //处理成要缓存的数据
        Map<String, BigDecimal> collect = data.stream().collect(Collectors.toMap(CustomerProductBillResponse.BillSumRecordInfo::getCloud_service_type_code, (CustomerProductBillResponse.BillSumRecordInfo::getConsume_amount)));
        //存入缓存(注意:需要保证存入缓存的数据都是可序列化的)
        cache.put(new Element(cache_key_name, collect));
        /**
         * ehcache和其它缓存类似,需要flush或shutdown后才会持久化到磁盘。
         * 会生成.data 的数据文件和 .index 的索引文件,方便重启恢复。
         * ehcache恢复数据是根据.index索引文件来进行数据恢复的。
         * 当程序再次启动的时候,ehcache的一个方法会将.data文件和.index文件的修改时间进行比较,如果不符合直接将.index文件删除。
         */
        //将所有缓存项从内存刷新到磁盘存储,并从DiskStore刷新到磁盘。
        cache.flush();
        //更新.index文件
        cacheManager.shutdown();
    }
}

原理:ehcache和其它缓存类似,需要flush或shutdown后才会持久化到磁盘。会生成.data 的数据文件和 .index 的索引文件,方便重启恢复。恢复数据是根据.index索引文件来进行数据恢复的。

其实到这就已经就已经能够实现我想要的效果了,但是我在测试的时候发现有的时候服务关闭再次启动的时候会删除.index导致启动后无法读取到数据。我该设置的都设置了呀,怎么回事?后来查了一下发现在项目启动的时候会调用net.sf.ehcache.store.disk.DiskStorageFactory中的DiskStorageFactory(Ehcache cache, RegisteredEventListeners cacheEventNotificationService)的方法其中有这么一段代码:

他会判断.data和.index文件的最后一次修改时间,如果满足他设定的删除条件则将.index文件删除,这么做可能是怕.index里的数据比.data里的数据老导致数据不准确问题吧。我试了很多配置,想让他不删除.index文件或者让.index文件的较.data后一点更新,都但是无济于事,因为我这里每次往.data里存数据后都手动调用了shutdown()方法【更新了.index文件】,所以我的.index文件里存的数据一定是最新的。

实在没办法了,后来我重写了ehcache提供的源码中的这个删除文件的方法,让他永远不删除.index(这方法有点暴力:-)),deploy到公司的maven私服来使用。

项目中移除之前引用的ehcache2.6.11的包,引入自己的jar(我自己升了个版本)

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.12</version>
</dependency>

源码修改处 

实现了我想要的最终效果!

源码下载github地址:https://github.com/ystory/ehcache-core-2.6.11

如果不想自己修改可以直接下载我修改过的jar,我已经上传至CSDN资源。

下载链接:https://download.csdn.net/download/peacock__/11256508

实现之后觉得并没那么难,但是这个过程却用了我2天时间,中间的小问题也是不断。好在都克服了↖(^ω^)↗有收获就很开心

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值