spring boot整合cache使用Ehcache 进行数据缓存

本文介绍了如何在SpringBoot项目中整合Ehcache和Redis缓存技术,包括引入依赖、配置文件设置、自定义缓存策略以及在启动类中启用缓存。通过实例展示了如何在接口中使用Cacheable并观察缓存效果。
摘要由CSDN通过智能技术生成

之前的文章 spring boot整合 cache 以redis服务 处理数据缓存 便捷开发 带着大家通过spring boot整合了 cache 缓存
那么 我们就来说说 其他服务的缓存
而spring boot默认的缓存方案就是 cache 用simple模式

spring boot的强大在于它的整合能力 它将其他缓存技术整合 统一了接口
简单说 所有的接口都是一样的 代码不用动 换实现就好了

那么问题来了,都有哪些整合技术呢?
如下图
在这里插入图片描述
这是官方给出的解决方案
那么 我们这里 就在说一下
memcached
Redis
Simple
Ehcache

Redis之前说过了 Simple就是默认的
然后 本文 我们来说 Ehcache

然后 我们打开 java spring boot项目

pom.xml 导入坐标

<dependency>
	<groupId>net.sf.ehcache</groupId>
	<artifactId>ehcache</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

然后 application.yml 加上如下代码

spring:
  cache:
    type: ehcache

这里 就是告诉它 我们 cache 要用 ehcache服务

然后 找到如下图 目录 在下面创建一个 ehcache.xml
在这里插入图片描述
参考代码如下

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

    <defaultCache
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxEntriesLocalHeap="10000"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">

        <persistence strategy="localTempSwap"/>

    </defaultCache>

    <!-- 添加其他自定义缓存配置 -->

</ehcache>

地址这里 会出现报红的情况 这个是idea的问题 和我们没关系 不用管
在这里插入图片描述
然后 来说说它的配置

eternal: 缓存条目是否永久有效,这里设置为 false 表示不永久有效。
timeToIdleSeconds: 缓存条目在空闲多少秒后过期。
timeToLiveSeconds: 缓存条目在存活多少秒后过期。
maxEntriesLocalHeap: 缓存在堆内存中的最大条目数。
maxEntriesLocalDisk: 缓存在磁盘中的最大条目数。
diskExpiryThreadIntervalSeconds: 磁盘过期线程的运行间隔,用于清理过期的磁盘缓存条目。
memoryStoreEvictionPolicy: 内存存储驱逐策略,这里设置为 LRU (Least Recently Used,最近最少使用)。
后面是元素时间和策略 如果你想具体了解 可以自己去搜索

老规矩 启动类 加上 EnableCaching 启动cache
在这里插入图片描述
这里 我们属性类 就不需要 implements Serializable了
在这里插入图片描述
然后 我们接口方法还是这样写
在这里插入图片描述
我们声明Cacheable 缓存空间叫 cacheSpace
还是那句话 如果根据id查 可以把id作为key进行存储
在这里插入图片描述
但如果这个时候 你直接启动项目 然后调用接口会报错的
因为 现在还没有 cacheSpace 这个缓存

ehcache.xml加入代码

<!-- 添加其他自定义缓存配置 -->
<cache name="cacheSpace"
   eternal="false"
   timeToIdleSeconds="300"
   timeToLiveSeconds="600"
   maxEntriesLocalHeap="1000"
   maxEntriesLocalDisk="10000"
   diskExpiryThreadIntervalSeconds="120"
   memoryStoreEvictionPolicy="LRU"/>

我们加一个叫 cacheSpace 的缓存
在这里插入图片描述
重新启动项目
然后 我们调用接口
在这里插入图片描述
正常返回 看项目控制台 明显是有执行sql的
在这里插入图片描述
然后 我们清空控制台内容 然后重新调接口

很明显 我们的缓存已经形成了
在这里插入图片描述

  • 25
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Ehcache 缓存来配置 Spring Boot 的步骤如下: 1. 添加 Ehcache 依赖:在 pom.xml 文件中添加 Ehcache 的相关依赖。 ```xml <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> ``` 2. 创建 Ehcache 配置文件:在 src/main/resources 目录下创建一个 ehcache.xml 文件,并配置缓存的名称、容量、过期时间等信息。 ```xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"> <cache alias="myCache"> <expiry> <ttl unit="seconds">60</ttl> </expiry> <heap unit="entries">100</heap> </cache> </config> ``` 3. 配置 Spring Boot 应用程序:在 Spring Boot 的配置类上使用 @EnableCaching 注解开启缓存功能,并指定 Ehcache缓存管理器。 ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { @Bean public EhCacheCacheManager cacheManager(CacheManager ehCacheManager) { return new EhCacheCacheManager(ehCacheManager); } } ``` 4. 使用缓存注解:在需要缓存的方法上使用 Spring 的缓存注解,比如 @Cacheable、@CachePut、@CacheEvict 等。 ```java @Service public class MyService { @Cacheable("myCache") public String getCachedData(String key) { // 从数据库或其他数据源中获取数据 return data; } } ``` 以上是使用 Ehcache 缓存配置 Spring Boot 的基本步骤。请根据你的具体需求进行相应的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值