Chinaunix首页 | 论坛 | 博客
  • 博客访问: 550901
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-06 18:09:31

 自定义缓存 - ehcache

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器

  1. 导包

    点击(此处)折叠或打开

    1. <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
    2. <dependency>
    3.     <groupId>org.mybatis.caches</groupId>
    4.     <artifactId>mybatis-ehcache</artifactId>
    5.     <version>1.1.0</version>
    6. </dependency>
  1. 在 Mapper.xml 中指定使用 ehcache 缓存实现

    点击(此处)折叠或打开

    1. <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
    2. <dependency>
    3.     <groupId>org.mybatis.caches</groupId>
    4.     <artifactId>mybatis-ehcache</artifactId>
    5.     <version>1.1.0</version>
    6. </dependency>
  1. 在resource中定义配置文件 ehcache.xml 

    点击(此处)折叠或打开

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <ehcache xmlns:xsi=""
    3.          xsi:noNamespaceSchemaLocation="" updateCheck="false">

    4.     <!--
    5.     diskStore: 缓存路径, ehcache分为内存和磁盘两级, 此属性定义磁盘的缓存位置
    6.     参数:
    7.     user.home - 用户主目录
    8.     user.dir - 用户当前工作目录
    9.     java.io.tmpdir - 默认临时文件路径
    10.     -->

    11.     <!-- java项目 www.fhadmin.org --> <!--当二级缓存的对象 超过内存限制时(缓存对象的个数>maxElementsInMemory),存放入的硬盘文件 -->
    12.     <diskStore path="./tempdir/Tmp_Ehcache"/>

    13.     <!--default 默认缓冲策略, 当ehcache找不到定义的缓存时, 则使用这个缓存策略, 这个只能定义一个-->
    14.     <defaultCache
    15.         eternal="false"
    16.         maxElementsInMemory="10000"
    17.         overflowToDisk="false"
    18.         diskPersistent="false"
    19.         timeToIdleSeconds="1800"
    20.         timeToLiveSeconds="259200"
    21.         memoryStoreEvictionPolicy="LRU"/>
    22.     
    23.     <cache
    24.         name="cloud_user"
    25.         eternal="false"
    26.         maxElementsInMemory="5000"
    27.         overflowToDisk="false"
    28.         diskPersistent="false"
    29.         timeToIdleSeconds="1800"
    30.         timeToLiveSeconds="1800"
    31.         memoryStoreEvictionPolicy="LRU"/>

    32.     <!--
    33.        java项目 fhadmin.cn
    34.        maxElementsInMemory:设置 在内存中缓存 对象的个数
    35.        maxElementsOnDisk:设置 在硬盘中缓存 对象的个数
    36.        eternal:设置缓存是否 永远不过期
    37.        overflowToDisk:当系统宕机的时候是否保存到磁盘上
    38.        maxElementsInMemory的时候,是否转移到硬盘中
    39.        timeToIdleSeconds:当2次访问 超过该值的时候,将缓存对象失效
    40.        timeToLiveSeconds:一个缓存对象 最多存放的时间(生命周期)
    41.        diskExpiryThreadIntervalSeconds:设置每隔多长时间,通过一个线程来清理硬盘中的缓存
    42.        clearOnFlush: 内存数量最大时是否清除
    43.        memoryStoreEvictionPolicy:当超过缓存对象的最大值时,处理的策略;LRU (最少使用),FIFO (先进先出), LFU (最少访问次数)
    44.      -->
    45. </ehcache>

阅读(882) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~