springboot 整合Ehcache的使用

Springboot提供了换粗的统一整合接口,方便缓存技术的开发与管理。

Generic,JCache,Ehcache,Hazelcast,Infinispan,Couchbase,Redis,Caffenine,Simple(默认缓存),Memcached。

如何整合Ecache

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://www.ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="D:\ehcache" />

<!--默认缓存策略 -->
<!-- external:是否永久存在,设置为true则不会被清除,此时与timeout冲突,通常设置为false-->
<!-- diskPersistent:是否启用磁盘持久化-->
<!-- maxElementsInMemory:最大缓存数量-->
<!-- overflowToDisk:超过最大缓存数量是否持久化到磁盘-->
<!-- timeToIdleSeconds:最大不活动间隔,设置过长缓存容易溢出,设置过短无效果,可用于记录时效性数据,例如验证码-->
<!-- timeToLiveSeconds:最大存活时间-->
<!-- memoryStoreEvictionPolicy:缓存清除策略-->
<defaultCache
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="LRU" />

<cache
name="smscode"
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
memoryStoreEvictionPolicy="LRU" />

</ehcache>

导入依赖

<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

配置application.yml

spring:
  cache:
    type: ehcache #指定缓存

配置ehcache.xml文件

启动服务器,发起测试

扩展内容
指定ecache的配置文件,如果ecache的配置文件名称不是ecache.xml,可以自己指定。

spring:
  cache:
    type: ehcache #指定缓存
    ehcache:
      config: ehcache2.xml

posted on 2022-09-28 08:58  书梦一生  阅读(331)  评论(0编辑  收藏  举报

导航