@@ -0,0 +1,130 @@ | |||||
# spring-boot-demo-cache-caffeine | |||||
> 此 demo 主要演示了 Spring Boot 如何集成 caffeine 使用缓存。 | |||||
## 1.开发步骤 | |||||
### 1.1.添加依赖 | |||||
```xml | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>demo-cache-api</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-cache</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.github.ben-manes.caffeine</groupId> | |||||
<artifactId>caffeine</artifactId> | |||||
<version>${caffeine.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | |||||
``` | |||||
### 1.2.修改配置文件 | |||||
```yaml | |||||
spring: | |||||
cache: | |||||
type: caffeine | |||||
caffeine: | |||||
spec: maximumSize=500,expireAfterAccess=600s | |||||
cache-names: | |||||
- user | |||||
logging: | |||||
level: | |||||
com.xkcoding: debug | |||||
``` | |||||
### 1.3.开启缓存自动装配 | |||||
```java | |||||
@EnableCaching | |||||
@Configuration(proxyBeanMethods = false) | |||||
public class CaffeineCacheAutoConfiguration { | |||||
} | |||||
``` | |||||
### 1.5.模拟数据服务 | |||||
> 为了减少重复代码,该部分我将其抽取实现在 demo-cache-api 模块中 | |||||
## 2.测试 | |||||
```java | |||||
@Slf4j | |||||
@SpringBootTest | |||||
public class UserServiceTest { | |||||
@Autowired | |||||
private UserService userService; | |||||
/** | |||||
* 获取两次,查看日志验证缓存 | |||||
*/ | |||||
@Test | |||||
public void getTwice() { | |||||
// 模拟查询id为1的用户 | |||||
User user1 = userService.get(1L); | |||||
log.debug("【user1】= {}", user1); | |||||
// 再次查询 | |||||
User user2 = userService.get(1L); | |||||
log.debug("【user2】= {}", user2); | |||||
// 查看日志,只打印一次日志,证明缓存生效 | |||||
} | |||||
/** | |||||
* 先存,再查询,查看日志验证缓存 | |||||
*/ | |||||
@Test | |||||
public void getAfterSave() { | |||||
userService.saveOrUpdate(new User(4L, "user4")); | |||||
User user = userService.get(4L); | |||||
log.debug("【user】= {}", user); | |||||
// 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效 | |||||
} | |||||
/** | |||||
* 测试删除,查看caffeine是否存在缓存数据 | |||||
*/ | |||||
@Test | |||||
public void deleteUser() { | |||||
// 查询一次,使caffeine中存在缓存数据 | |||||
userService.get(1L); | |||||
// 删除,查看caffeine是否存在缓存数据 | |||||
userService.delete(1L); | |||||
} | |||||
} | |||||
``` | |||||
## 3.参考 | |||||
- [Caffeine 官网](https://github.com/ben-manes/caffeine) | |||||
- [Spring Boot 官方文档之 Caffeine 集成](https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#io.caching.provider.caffeine) | |||||
@@ -0,0 +1,71 @@ | |||||
<?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"> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>demo-cache</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<artifactId>demo-cache-caffeine</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>demo-cache-caffeine</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<properties> | |||||
<java.version>17</java.version> | |||||
<caffeine.version>3.1.1</caffeine.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>demo-cache-api</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-cache</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.github.ben-manes.caffeine</groupId> | |||||
<artifactId>caffeine</artifactId> | |||||
<version>${caffeine.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>demo-cache-caffeine</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,19 @@ | |||||
package com.xkcoding.cache.caffeine; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
/** | |||||
* <p> | |||||
* 启动器 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date 2022-09-14 15:47 | |||||
*/ | |||||
@SpringBootApplication | |||||
public class CaffeineCacheApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(CaffeineCacheApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,18 @@ | |||||
package com.xkcoding.cache.caffeine.configuration; | |||||
import org.springframework.cache.annotation.EnableCaching; | |||||
import org.springframework.context.annotation.Configuration; | |||||
/** | |||||
* <p> | |||||
* caffeine 缓存自动装配 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2022-09-14 15:48 | |||||
*/ | |||||
@EnableCaching | |||||
@Configuration(proxyBeanMethods = false) | |||||
public class CaffeineCacheAutoConfiguration { | |||||
} |
@@ -0,0 +1,10 @@ | |||||
spring: | |||||
cache: | |||||
type: caffeine | |||||
caffeine: | |||||
spec: maximumSize=500,expireAfterAccess=600s | |||||
cache-names: | |||||
- user | |||||
logging: | |||||
level: | |||||
com.xkcoding: debug |
@@ -0,0 +1,13 @@ | |||||
package com.xkcoding.cache.caffeine; | |||||
import org.junit.jupiter.api.Test; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
@SpringBootTest | |||||
class CaffeineCacheApplicationTests { | |||||
@Test | |||||
void contextLoads() { | |||||
} | |||||
} |
@@ -0,0 +1,62 @@ | |||||
package com.xkcoding.cache.caffeine.service; | |||||
import com.xkcoding.cache.api.UserService; | |||||
import com.xkcoding.cache.entity.User; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.junit.jupiter.api.Test; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
/** | |||||
* <p> | |||||
* caffeine缓存测试 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-11-16 16:58 | |||||
*/ | |||||
@Slf4j | |||||
@SpringBootTest | |||||
public class UserServiceTest { | |||||
@Autowired | |||||
private UserService userService; | |||||
/** | |||||
* 获取两次,查看日志验证缓存 | |||||
*/ | |||||
@Test | |||||
public void getTwice() { | |||||
// 模拟查询id为1的用户 | |||||
User user1 = userService.get(1L); | |||||
log.debug("【user1】= {}", user1); | |||||
// 再次查询 | |||||
User user2 = userService.get(1L); | |||||
log.debug("【user2】= {}", user2); | |||||
// 查看日志,只打印一次日志,证明缓存生效 | |||||
} | |||||
/** | |||||
* 先存,再查询,查看日志验证缓存 | |||||
*/ | |||||
@Test | |||||
public void getAfterSave() { | |||||
userService.saveOrUpdate(new User(4L, "user4")); | |||||
User user = userService.get(4L); | |||||
log.debug("【user】= {}", user); | |||||
// 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效 | |||||
} | |||||
/** | |||||
* 测试删除,查看caffeine是否存在缓存数据 | |||||
*/ | |||||
@Test | |||||
public void deleteUser() { | |||||
// 查询一次,使caffeine中存在缓存数据 | |||||
userService.get(1L); | |||||
// 删除,查看caffeine是否存在缓存数据 | |||||
userService.delete(1L); | |||||
} | |||||
} |
@@ -146,7 +146,7 @@ public class UserServiceTest { | |||||
} | } | ||||
/** | /** | ||||
* 测试删除,查看redis是否存在缓存数据 | |||||
* 测试删除,查看ehcache是否存在缓存数据 | |||||
*/ | */ | ||||
@Test | @Test | ||||
public void deleteUser() { | public void deleteUser() { | ||||
@@ -50,7 +50,7 @@ public class UserServiceTest { | |||||
} | } | ||||
/** | /** | ||||
* 测试删除,查看redis是否存在缓存数据 | |||||
* 测试删除,查看ehcache是否存在缓存数据 | |||||
*/ | */ | ||||
@Test | @Test | ||||
public void deleteUser() { | public void deleteUser() { | ||||
@@ -20,6 +20,7 @@ | |||||
<modules> | <modules> | ||||
<module>demo-cache-api</module> | <module>demo-cache-api</module> | ||||
<module>demo-cache-caffeine</module> | |||||
<module>demo-cache-ehcache</module> | <module>demo-cache-ehcache</module> | ||||
<module>demo-cache-redis</module> | <module>demo-cache-redis</module> | ||||
</modules> | </modules> | ||||