创建spring boot【2.10】
引入条件
- spring-boot-starter-cache 必须
- ehcache【可选,其他等】
创建pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cache_simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cache_simple</name>
<description>cache相关例子</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 测试使用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 无需引入版本号,有spring boot控制 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
引入xml配置
默认查找名资源根目录ehcache.xml,所以我们只需在根目录放入ehcache.xml即可
ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect">
<!--
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
-->
<!--缓存路径-->
<diskStore path="/opt/webapps/cache/ehcache"/>
<defaultCache maxElementsInMemory="10000"
maxElementsOnDisk="1000000" eternal="false" overflowToDisk="false"
timeToIdleSeconds="7200" timeToLiveSeconds="7200"
memoryStoreEvictionPolicy="LRU" diskPersistent="false" diskExpiryThreadIntervalSeconds="3000"/>
<cache name="javaEpgCache" maxElementsInMemory="10000"
maxElementsOnDisk="1000000" eternal="false" overflowToDisk="false"
timeToIdleSeconds="7200" timeToLiveSeconds="7200"
memoryStoreEvictionPolicy="LRU" diskPersistent="false" diskExpiryThreadIntervalSeconds="3000">
<!-- <BootstrapCacheLoaderFactory class="net.sf.ehcache.store.DiskStoreBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=true" /> -->
</cache>
</ehcache>
创建一个简单控制器测试
HelloController.java
@RestController
public class HelloController {
@Resource
private CacheManager cacheManager;
@RequestMapping("/test")
public String test(){
Cache cache= cacheManager.getCache("javaEpgCache");
if(cache.get("name")==null){
cache.put("name","123");
return "456";
}else{
return cache.get("name").get().toString();
}
}
}
CacheSimpleApplication
使用Cache自动生效。
@SpringBootApplication
@EnableCaching
public class CacheSimpleApplication {
public static void main(String[] args) {
SpringApplication.run(CacheSimpleApplication.class, args);
}
}
运行,可以看到第一次输出456,第二次输出123.
下一节分析Spring Cache自动装配原理
网友评论