• ssm框架整合ehcache缓存


    一、引入maven

            <!-- ehcache核心jar包 -->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-core</artifactId>
                <version>2.6.11</version>
            </dependency>
            <!-- MyBatis与ehcache整合jar包 -->
            <dependency>
                <groupId>org.mybatis.caches</groupId>
                <artifactId>mybatis-ehcache</artifactId>
                <version>1.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.4</version>
            </dependency>    

    二、classpath路径下添加配置文件 ehcache.xml

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="ehcache.xsd">
        <diskStore path="F:developehcache" />
        <defaultCache
                maxElementsInMemory="1000"
                maxElementsOnDisk="10000000"
                eternal="false"
                overflowToDisk="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
        </defaultCache>
    </ehcache>

    注意:

      xsi:noNamespaceSchemaLocation="ehcache.xsd" 这是我将网络上的约束文件(http://ehcache.org/ehcache.xsd)下载至本地路径进行读取

      因为他原先的语句在idea上报错,为省事,我直接将其缓存,圆文件如下

      

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    defaultcache属性配置详解

    diskStore:指定数据在磁盘中的存储位置。
     defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略以下属性是必须的:
     maxElementsInMemory - 在内存中缓存的element的最大数目 
     maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
     eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
     overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上以下属性是可选的:
     timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
     timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
     diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
     diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
     diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
     memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)

      注意:ehcache的存活时间问题

    其中主要记录的是timeToLiveSeconds和timeToIdleSeconds;因为此俩容易搞混淆:
        timeToLiveSeconds=x:缓存自创建日期起至失效时的间隔时间x;
         timeToIdleSeconds=y:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔y;
    
    如果仅有 timeToLiveSeconds 那么 自创建时间开始 间隔x后缓存失效; 如果没有timeToLiveSeconds 那么自最后一次访问缓存 间隔y后 缓存失效; 
    如果既有timeToLiveSeconds 也有 timeToIdleSeconds 那么取最小数算作间隔时间;min(x,y);;
    经过测试其计算原则是:若自创建缓存后一直都没有访问缓存,那么间隔x后失效,若自创建缓存后有N次访问缓存,那么计算(最后一次访问缓存时间+y )
    即:按照timeToIdleSeconds计算,但总存活时间不超过 y;
    举个例子: timeToIdleSeconds=120;
    timeToLiveSeconds=180;

    上面的表示此缓存最多可以存活3分钟,如果期间超过2分钟未访问 那么此缓存失效!

    三、开启ehcache缓存

      修改 ...mapper.xml,在cache中添加指定的 EhcacheCache

      <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

      或自定义cache

    <mapper namespace="com.ruoweiedu.api.app.querypricelist.xml.QueryPriceList">
        <cache type="org.mybatis.caches.ehcache.EhcacheCache" >
            <property name="timeToIdleSeconds" value="120"/>
            <property name="timeToLiveSeconds" value="120"/>
            <!-- 同ehcache参数maxElementsInMemory -->
            <property name="maxEntriesLocalHeap" value="1000"/>
            <!-- 同ehcache参数maxElementsOnDisk -->
            <property name="maxEntriesLocalDisk" value="10000000"/>
            <property name="memoryStoreEvictionPolicy" value="LRU"/>
        </cache>

    四、应用环境介绍

      对于访问多的查询请求且用户对查询结果实时性要求不高,此时可采用mybatis二级缓存技术降低数据库访问量,提高访问速度,业务场景比如:

    耗时较高的统计分析sql、电话账单查询sql等。 实现方法如下:通过设置刷新间隔时间,由mybatis每隔一段时间自动清空缓存,根据数据变化频率设置缓存刷新间隔

    flushInterval,比如设置为30分钟、60分钟、24小时等,根据需求而定。

      mybatis二级缓存对细粒度的数据级别的缓存实现不好,比如如下需求:对商品信息进行缓存,由于商品信息查询访问量大,但是要求用户每次都能查询最新的商品信息,

    此时如果使用mybatis的二级缓存就无法实现当一个商品变化时只刷新该商品的缓存信息而不刷新其它商品的信息,因为mybaits的二级缓存区域以mapper为单位划分,当一

    个商品信息变化会将所有商品信息的缓存数据全部清空。解决此类问题需要在业务层根据需求对数据有针对性缓存。

      

  • 相关阅读:
    DBA操作规范
    MySQL高可用之MHA
    Get MySQL这5个优化技巧,你将如虎添翼
    数据库的那些事
    Kubernetes
    nginx错误分析 `104: Connection reset by peer`
    kubernets资源预留
    kubernetes Pod亲和性
    kubernetes cpu限制参数说明
    zabbix 面板graph图上没有数据显示
  • 原文地址:https://www.cnblogs.com/fudou/p/9283675.html
Copyright © 2020-2023  润新知