springboot2.0 使用ehcache缓存

本文介绍如何在SpringBoot项目中使用Ehcache实现数据缓存功能。通过在启动类添加@EnableCaching注解并结合serviceImpl层方法上的@Cacheable注解,可以有效地对指定的数据进行缓存管理。同时,ehcache.xml文件提供了详细的缓存配置参数说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在springboot启动类添加

@EnableCaching

其次在serviceImpl层的方法上添加

@Cacheable(cacheNames = "zjehcache")

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <cache name="zjehcache" eternal="true" maxEntriesLocalHeap="0" ></cache>

    <!--
         磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
         path:指定在硬盘上存储对象的路径
      -->
        <!--<diskStore path="java.io.tmpdir" />-->

    <!--
        defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
        maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
        eternal:代表对象是否永不过期
        timeToIdleSeconds:最大的发呆时间
        timeToLiveSeconds:最大的存活时间
        overflowToDisk:是否允许对象被写入到磁盘
    -->
    <!--<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />-->
    <!--
23         cache:为指定名称的对象进行缓存的特殊配置
24         name:指定对象的完整名
25   -->
        <!--<cache name="com.zbaccp.entity.Person" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />-->


</ehcache>

cacheNames 的名字是自定义的与ehcache.xml中的 name一致。

点击项目model开始下载。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值