30.自定义缓存Ehcache

本文介绍了Ehcache作为Java缓存框架的特性,以及如何在MyBatis中配置和使用Ehcache进行数据缓存。通过引入mybatis-ehcache依赖并在Mapper.xml中指定缓存类型,实现数据的高效缓存。同时展示了Ehcache的配置文件,包括内存和磁盘缓存设置,以及针对特定缓存的配置示例。
摘要由CSDN通过智能技术生成

自定义缓存

ehcache:它是一个纯Java的进程内缓存框架,具有快速,精干等特点,是Hibernate(全自动框架,sql语句都不用自己写)中默认的CacheProvider

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存

要在程序中使用ehcache,先要导包!

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
    <dependency>
        <groupId>org.mybatis.caches</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.1.0</version>
    </dependency>
    </dependencies>

在Mapper.xml中指定使用ehcashe缓存

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

resource 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分为内存和磁盘两级,此属性定义为磁盘的缓存位置,参数解释如下:
        user.home -用户主目录
        user.dir -用户当前工作目录
        java.io.tmpdir -默认临时文件路径
    -->
    <diskStore path="./tmpdir/Tmp_EhCache"/>

    <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="259200"
            memoryStoreEvictionPolicy="LRU"/>

    <cache
            name="cloud_user"
            eternal="false"
            maxElementsInMemory="5000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="1800"
            memoryStoreEvictionPolicy="LRU"/>
</ehcache>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值