摘要
本文介绍了springboot中配置ehcache、自定义get/set方式,并实际使用缓存的过程。
概念
ehcache是一种java缓存框架,支持多种缓存策略,如:
- 最近最少使用lru:当缓存达到最大容量时,会将最近最少使用的元素淘汰。
- 最少最近使用lfu:根据元素被访问的频率来淘汰元素,最少被访问的元素会被优先淘汰。
- 先进先出fifo:按元素进入缓存的时间顺序淘汰元素,先进入的元素会被优先淘汰。
内存与磁盘持久化存储:
ehcache支持将数据存储在内存中,以实现快速访问。当内存不足时,ehcache可以将数据交换到磁盘,确保缓存数据不会因为内存限制而丢失。磁盘存储路径可以通过配置灵活指定,即使在系统重启后也能恢复缓存数据。提供多种持久化策略,包括本地临时交换localtempswap和自定义持久化实现。
配置灵活性:
可通过配置ehcache.xml文件,放在启动类资源目录里,可自定义缓存策略,lru,diskexpirythreadintervalseconds。
编码示例
引入依赖:
pom.xml指定ehcache坐标并排除slf4j
<!-- ehcache 坐标 --> <dependency> <groupid>net.sf.ehcache</groupid> <artifactid>ehcache</artifactid> <exclusions> <exclusion> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>net.sf.ehcache</groupid> <artifactid>ehcache</artifactid> <version>2.10.9.2</version> <scope>compile</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-cache</artifactid> </dependency>
配置ehcache.xml文件:
<ehcache xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="http://ehcache.org/ehcache.xsd"> <diskstore path="java.io.tmpdir/ehcache"/> <!-- 定义磁盘存储路径,将缓存数据存储在临时目录下 --> <!-- 参数说明: maxelementsinmemory 内存中最多存储的元素数量 eternal 是否为永生缓存,false表示元素有生存和闲置时间 timetoidleseconds 元素闲置 120 秒后失效 timetoliveseconds 元素存活 120 秒后失效 maxelementsondisk 磁盘上最多存储的元素数量 diskexpirythreadintervalseconds 磁盘清理线程运行间隔 memorystoreevictionpolic 内存存储的淘汰策略,采用 lru(最近最少使用) --> <!-- defaultcache:echcache 的默认缓存策略--> <defaultcache maxelementsinmemory="10000" eternal="false" timetoidleseconds="120" timetoliveseconds="120" maxelementsondisk="10000000" diskexpirythreadintervalseconds="120" memorystoreevictionpolicy="lru"> <persistence strategy="localtempswap"/> </defaultcache> <!-- 自定义缓存策略 这里的name是用于缓存名指定时 需要对应缓存名添加--> <cache name="cachename" maxelementsinmemory="10000" eternal="false" timetoidleseconds="0" timetoliveseconds="0" maxelementsondisk="10000000" diskexpirythreadintervalseconds="120" memorystoreevictionpolicy="lru"> <persistence strategy="localtempswap"/> </cache> </ehcache>
配置文件:
application.yml指定spring的缓存文件路径
spring: cache: type: ehcache ehcache: config: classpath:ehcache.xml
自定义缓存get/set方式:
利用cachemanager处理缓存:
package org.coffeebeans.ehcache; import net.sf.ehcache.cache; import net.sf.ehcache.cachemanager; import net.sf.ehcache.element; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.arraylist; import java.util.list; import java.util.objects; /** * <li>classname: ehcacheservice </li> * <li>author: oakwang </li> */ @service public class ehcacheservice { @autowired private cachemanager cachemanager; /** * 根据名称获取缓存数据 * @param cachename cache名称 * @param keyname cache中对应key名称 * @return cache数据 */ public list<?> getcachedata(string cachename, string keyname){ cache cache = cachemanager.getcache(cachename); if (!objects.isnull(cache)) { element element = cache.get(keyname); if (!objects.isnull(element)) { return (list<?>) element.getobjectvalue(); } } return new arraylist<>(); } /** * 往缓存中存放数据 名字区分 * @param cachename cache名称 * @param keyname cache中对应key名称 * @param datalist 存放数据 */ public void putcachedata(string cachename, string keyname, list<?> datalist){ cache cache = cachemanager.getcache(cachename); if (objects.isnull(cache)){ cache = new cache(cachename,1000,true,false,0,0); } element newelement = new element(keyname, datalist); cache.put(newelement); } }
启动类加注解:
@enablecaching开启程序缓存
package org.coffeebeans; import lombok.extern.slf4j.slf4j; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cache.annotation.enablecaching; /** * <li>classname: ehcacheapplication </li> * <li>author: oakwang </li> */ @slf4j @springbootapplication @enablecaching//开启程序缓存 public class ehcacheapplication { public static void main(string[] args) { springapplication springapplication = new springapplication(ehcacheapplication.class); springapplication.run(args); log.info("ehcacheapplication start success!"); } }
编辑测试类:
package org.coffeebeans.ehcache; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.collections; import java.util.list; /** * <li>classname: testehcache </li> * <li>author: oakwang </li> */ @service public class testehcache { @autowired private ehcacheservice ehcacheservice; public void testehcache() { ehcacheservice.putcachedata("cachename", "keyname", collections.singletonlist("value")); list<?> cachedata = ehcacheservice.getcachedata("cachename", "keyname"); system.out.println("获取缓存数据:" + cachedata.tostring()); } }
测试结果:
总结
以上我们了解了springboot中配置ehcache、自定义get/set方式,并实际使用缓存的过程。
到此这篇关于详解springboot+ehcache使用示例的文章就介绍到这了,更多相关springboot+ehcache使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论