搭建本地缓存ehcache的demo
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ehcache.demo</groupId>
<artifactId>ehcache-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ehcache-demo</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring Boot 缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 使用 ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.application.yml
spring:
application:
name: demo
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
server:
port: 8080
3.ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!--这里给一篇博客:https://www.cnblogs.com/fashflying/p/6908028.html-->
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="local"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
4.目录结构,其它类
package com.ehcache.demo.impl;
import com.ehcache.demo.DemoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
/**
* @program: ehcache-demo
* @description: 测试实现类
* @author: sw
* @create: 2020-06-28 22:08
**/
@Service
@EnableCaching
public class DemoServiceImpl implements DemoService {
private final static String CACHE_KEY = "local";
private int count = 0;
//第一次查询操作后缓存!,count请求多次count都++了一次
@Cacheable(value = CACHE_KEY,key = "#id")
public Integer getId(int id) {
System.out.println(id);
return ++count;
}
//每次请求移,都清除旧值,也就是每请求一次返回++的值
@CacheEvict(value = CACHE_KEY,key = "#id")
public Integer removeId(int id) {
return ++count;
}
//每次请求都更新缓存中都值,count也会每次++,和上面不同都是这个是更新缓存,上面是删除缓存
@CachePut(value = CACHE_KEY,key = "#id")
public Integer updateId(int id) {
return ++count;
}
}
package com.ehcache.demo.controller;
import com.ehcache.demo.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: ehcache-demo
* @description: test
* @author: zxb
* @create: 2020-06-28 22:26
**/
@RestController
public class EhcacheController {
@Autowired
private DemoService demoService;
@GetMapping("/hello")
public String hello(){
return "ok";
}
@GetMapping("/getId")
public Integer getId(int id) {
System.out.println(id);
return demoService.getId(id);
}
@GetMapping("/removeId")
public Integer removeId(int id) {
return demoService.removeId(id);
}
@GetMapping("/updateId")
public Integer updateId(int id) {
return demoService.updateId(id);
}
}
package com.ehcache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* Hello world!
*
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class App {
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}