SpringBoot整合Ehcache

一、创建项目并导入依赖

   

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-cacheartifactId>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-webartifactId>

dependency>

<dependency>

<groupId>net.sf.ehcachegroupId>

<artifactId>ehcacheartifactId>

<version>2.10.6version>

dependency>

   

二、相关配置和代码

   

ehcache.xml模板

   

<ehcacheupdateCheck="false"dynamicConfig="false">

<diskStorepath="java.io.tmpdir/ehcache"/>

   

   

<defaultCachename="defaultCache"

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="false"

maxElementsOnDisk="100000"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"/>

   

   

<cachename="myehcache"

maxEntriesLocalHeap="1000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="false"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="600"/>

   

ehcache>

   

   

复制上面代码到

   

SpringBoot整合Ehcache_第1张图片

   

SpringBoot整合Ehcache_第2张图片

   

   

   

SpringBoot整合Ehcache_第3张图片

   

SpringBoot整合Ehcache_第4张图片

   

这样ehcache模板就创建好了

   

   

创建好模板就需要在resource下创建一个ehcache的xml文件

   

SpringBoot整合Ehcache_第5张图片

   

这个文件可以自定义名字和使用ehcache作为文件名

如果是xxx.xml就需要在application.properites指定文件了

   

spring.cache.ehcache.config=classpath:你的文件名.xml

   

SpringBoot整合Ehcache_第6张图片

   

如果使用ehcache.xml就不要在application.properites里面写配置了

   

   

下面代码和SpringCache整合redis一模一样

https://www.cnblogs.com/fernfei/p/12174174.html

   

   

注:springcache只是制定规范,具体实现是redis或ehcache 就好比jdbc用哪个数据库直接更换数据库驱动就可以了

   

   

   

   

   

   

   

   

   

   

   

   

   

   

   

   

   

   

你可能感兴趣的:(SpringBoot整合Ehcache)