springboot 整合Ehcache的使用
Springboot提供了换粗的统一整合接口,方便缓存技术的开发与管理。
Generic,JCache,Ehcache,Hazelcast,Infinispan,Couchbase,Redis,Caffenine,Simple(默认缓存),Memcached。
如何整合Ecache
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?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> |
导入依赖
1 2 3 4 5 | <!-- https: //mvnrepository.com/artifact/net.sf.ehcache/ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> |
配置application.yml
1 2 3 | spring: cache: type: ehcache #指定缓存 |
配置ehcache.xml文件
启动服务器,发起测试
扩展内容
指定ecache的配置文件,如果ecache的配置文件名称不是ecache.xml,可以自己指定。
1 2 3 4 5 | spring: cache: type: ehcache #指定缓存 ehcache: config: ehcache2.xml |
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具