From c36fb10ef8976f61eb0d7748d3aebb7ded3d0438 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Wed, 14 Sep 2022 15:39:47 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=BC=93=E5=AD=98=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B9=8B=20ehcache=20=E7=BC=93=E5=AD=98=E6=A1=88?= =?UTF-8?q?=E4=BE=8B=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo-cache/demo-cache-ehcache/.gitignore | 25 -- demo-cache/demo-cache-ehcache/README.md | 301 ++++++--------------- demo-cache/demo-cache-ehcache/pom.xml | 114 ++++---- ...plication.java => EhcacheCacheApplication.java} | 6 +- .../EhcacheCacheAutoConfiguration.java | 18 ++ .../com/xkcoding/cache/ehcache/entity/User.java | 30 -- .../cache/ehcache/service/UserService.java | 36 --- .../ehcache/service/impl/UserServiceImpl.java | 78 ------ .../src/main/resources/application.yml | 8 +- .../src/main/resources/ehcache.xml | 37 --- .../src/main/resources/ehcache3.xml | 24 ++ .../ehcache/EhcacheCacheApplicationTests.java | 13 + ...SpringBootDemoCacheEhcacheApplicationTests.java | 16 -- .../cache/ehcache/service/UserServiceTest.java | 10 +- demo-cache/pom.xml | 1 + 15 files changed, 219 insertions(+), 498 deletions(-) delete mode 100644 demo-cache/demo-cache-ehcache/.gitignore rename demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/{SpringBootDemoCacheEhcacheApplication.java => EhcacheCacheApplication.java} (61%) create mode 100644 demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/configuration/EhcacheCacheAutoConfiguration.java delete mode 100644 demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java delete mode 100644 demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java delete mode 100644 demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java delete mode 100644 demo-cache/demo-cache-ehcache/src/main/resources/ehcache.xml create mode 100644 demo-cache/demo-cache-ehcache/src/main/resources/ehcache3.xml create mode 100644 demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/EhcacheCacheApplicationTests.java delete mode 100644 demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java diff --git a/demo-cache/demo-cache-ehcache/.gitignore b/demo-cache/demo-cache-ehcache/.gitignore deleted file mode 100644 index 82eca33..0000000 --- a/demo-cache/demo-cache-ehcache/.gitignore +++ /dev/null @@ -1,25 +0,0 @@ -/target/ -!.mvn/wrapper/maven-wrapper.jar - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -/nbproject/private/ -/build/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ \ No newline at end of file diff --git a/demo-cache/demo-cache-ehcache/README.md b/demo-cache/demo-cache-ehcache/README.md index 2d99cc6..91f261e 100644 --- a/demo-cache/demo-cache-ehcache/README.md +++ b/demo-cache/demo-cache-ehcache/README.md @@ -1,240 +1,119 @@ # spring-boot-demo-cache-ehcache -> 此 demo 主要演示了 Spring Boot 如何集成 ehcache 使用缓存。 +> 此 demo 主要演示了 Spring Boot 如何集成 ehcache3 使用缓存。 -## pom.xml +## 1.开发步骤 -```xml - - - 4.0.0 +### 1.1.添加依赖 - spring-boot-demo-cache-ehcache - 1.0.0-SNAPSHOT - jar - - spring-boot-demo-cache-ehcache - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-cache - - - - net.sf.ehcache - ehcache - - - - org.projectlombok - lombok - true - - - - com.google.guava - guava - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - spring-boot-demo-cache-ehcache - - - org.springframework.boot - spring-boot-maven-plugin - - - - - -``` +> 注意:Spring Boot 3 依赖 jakarta 的包,但是默认的 ehcache 依赖的是 javax 的,所以需要再引入依赖的时候设置 `jakarta` -## SpringBootDemoCacheEhcacheApplication.java - -```java -/** - *

- * 启动类 - *

- * - * @author yangkai.shen - * @date Created in 2018-11-16 17:02 - */ -@SpringBootApplication -@EnableCaching -public class SpringBootDemoCacheEhcacheApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringBootDemoCacheEhcacheApplication.class, args); - } -} +```xml + + + com.xkcoding + demo-cache-api + 1.0.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-cache + + + + org.springframework.boot + spring-boot-starter-test + test + + + + javax.cache + cache-api + + + + org.ehcache + ehcache + ${ehcache.version} + jakarta + + + + org.projectlombok + lombok + true + + ``` -## application.yml +### 1.2.修改配置文件 ```yaml spring: cache: - type: ehcache - ehcache: - config: classpath:ehcache.xml + type: jcache + jcache: + config: classpath:ehcache3.xml logging: level: com.xkcoding: debug ``` -## ehcache.xml +### 1.3.配置 ehcache ```xml - - - - - - - - - - - + + + + + + + + + 600 + + + + 2000 + 100 + + + + + + + ``` -## UserServiceImpl.java +### 1.4.开启缓存自动装配 ```java -/** - *

- * UserService - *

- * - * @author yangkai.shen - * @date Created in 2018-11-16 16:54 - */ -@Service -@Slf4j -public class UserServiceImpl implements UserService { - /** - * 模拟数据库 - */ - private static final Map DATABASES = Maps.newConcurrentMap(); - - /** - * 初始化数据 - */ - static { - DATABASES.put(1L, new User(1L, "user1")); - DATABASES.put(2L, new User(2L, "user2")); - DATABASES.put(3L, new User(3L, "user3")); - } - - /** - * 保存或修改用户 - * - * @param user 用户对象 - * @return 操作结果 - */ - @CachePut(value = "user", key = "#user.id") - @Override - public User saveOrUpdate(User user) { - DATABASES.put(user.getId(), user); - log.info("保存用户【user】= {}", user); - return user; - } - - /** - * 获取用户 - * - * @param id key值 - * @return 返回结果 - */ - @Cacheable(value = "user", key = "#id") - @Override - public User get(Long id) { - // 我们假设从数据库读取 - log.info("查询用户【id】= {}", id); - return DATABASES.get(id); - } +@EnableCaching +@Configuration(proxyBeanMethods = false) +public class EhcacheCacheAutoConfiguration { - /** - * 删除 - * - * @param id key值 - */ - @CacheEvict(value = "user", key = "#id") - @Override - public void delete(Long id) { - DATABASES.remove(id); - log.info("删除用户【id】= {}", id); - } } ``` -## UserServiceTest.java +### 1.5.模拟数据服务 + +> 为了减少重复代码,该部分我将其抽取实现在 demo-cache-api 模块中 + +## 2.测试 ```java -/** - *

- * ehcache缓存测试 - *

- * - * @author yangkai.shen - * @date Created in 2018-11-16 16:58 - */ @Slf4j -public class UserServiceTest extends SpringBootDemoCacheEhcacheApplicationTests { +@SpringBootTest +public class UserServiceTest { @Autowired private UserService userService; @@ -279,8 +158,8 @@ public class UserServiceTest extends SpringBootDemoCacheEhcacheApplicationTests } ``` -## 参考 +## 3.参考 + +- [Ehcache 官网](http://www.ehcache.org/documentation/) +- [Spring Boot 官方文档之 JCache 集成](https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#io.caching.provider.jcache) -- Ehcache 官网:http://www.ehcache.org/documentation/ -- Spring Boot 官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-ehcache2 -- 博客:https://juejin.im/post/5b308de9518825748b56ae1d diff --git a/demo-cache/demo-cache-ehcache/pom.xml b/demo-cache/demo-cache-ehcache/pom.xml index 8a44447..f211f7a 100644 --- a/demo-cache/demo-cache-ehcache/pom.xml +++ b/demo-cache/demo-cache-ehcache/pom.xml @@ -1,69 +1,77 @@ - 4.0.0 - - demo-cache-ehcache + + com.xkcoding + demo-cache 1.0.0-SNAPSHOT - jar + + + 4.0.0 + + demo-cache-ehcache + 1.0.0-SNAPSHOT + jar + + demo-cache-ehcache + Demo project for Spring Boot - demo-cache-ehcache - Demo project for Spring Boot + + 17 - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - + 3.10.1 + - - UTF-8 - UTF-8 - 1.8 - + + + com.xkcoding + demo-cache-api + 1.0.0-SNAPSHOT + - - - org.springframework.boot - spring-boot-starter - + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-cache - + + org.springframework.boot + spring-boot-starter-cache + - - net.sf.ehcache - ehcache - + + org.springframework.boot + spring-boot-starter-test + test + - - org.projectlombok - lombok - true - + + javax.cache + cache-api + - - com.google.guava - guava - + + org.ehcache + ehcache + ${ehcache.version} + jakarta + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.projectlombok + lombok + true + + - - demo-cache-ehcache - - - org.springframework.boot - spring-boot-maven-plugin - - - + + demo-cache-ehcache + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/EhcacheCacheApplication.java similarity index 61% rename from demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java rename to demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/EhcacheCacheApplication.java index 2fdf43f..245552a 100644 --- a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java +++ b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/EhcacheCacheApplication.java @@ -2,7 +2,6 @@ package com.xkcoding.cache.ehcache; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.annotation.EnableCaching; /** *

@@ -13,10 +12,9 @@ import org.springframework.cache.annotation.EnableCaching; * @date Created in 2018-11-16 17:02 */ @SpringBootApplication -@EnableCaching -public class SpringBootDemoCacheEhcacheApplication { +public class EhcacheCacheApplication { public static void main(String[] args) { - SpringApplication.run(SpringBootDemoCacheEhcacheApplication.class, args); + SpringApplication.run(EhcacheCacheApplication.class, args); } } diff --git a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/configuration/EhcacheCacheAutoConfiguration.java b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/configuration/EhcacheCacheAutoConfiguration.java new file mode 100644 index 0000000..896b916 --- /dev/null +++ b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/configuration/EhcacheCacheAutoConfiguration.java @@ -0,0 +1,18 @@ +package com.xkcoding.cache.ehcache.configuration; + +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Configuration; + +/** + *

+ * ehcache 缓存自动装配 + *

+ * + * @author yangkai.shen + * @date Created in 2022-09-07 14:03 + */ +@EnableCaching +@Configuration(proxyBeanMethods = false) +public class EhcacheCacheAutoConfiguration { + +} diff --git a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java deleted file mode 100644 index 522357b..0000000 --- a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.xkcoding.cache.ehcache.entity; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.io.Serializable; - -/** - *

- * 用户实体 - *

- * - * @author yangkai.shen - * @date Created in 2018-11-16 16:53 - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -public class User implements Serializable { - private static final long serialVersionUID = 2892248514883451461L; - /** - * 主键id - */ - private Long id; - /** - * 姓名 - */ - private String name; -} diff --git a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java deleted file mode 100644 index 79fc0f4..0000000 --- a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.xkcoding.cache.ehcache.service; - -import com.xkcoding.cache.ehcache.entity.User; - -/** - *

- * UserService - *

- * - * @author yangkai.shen - * @date Created in 2018-11-16 16:53 - */ -public interface UserService { - /** - * 保存或修改用户 - * - * @param user 用户对象 - * @return 操作结果 - */ - User saveOrUpdate(User user); - - /** - * 获取用户 - * - * @param id key值 - * @return 返回结果 - */ - User get(Long id); - - /** - * 删除 - * - * @param id key值 - */ - void delete(Long id); -} diff --git a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java b/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java deleted file mode 100644 index a013ba7..0000000 --- a/demo-cache/demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.xkcoding.cache.ehcache.service.impl; - -import com.google.common.collect.Maps; -import com.xkcoding.cache.ehcache.entity.User; -import com.xkcoding.cache.ehcache.service.UserService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.CachePut; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.stereotype.Service; - -import java.util.Map; - -/** - *

- * UserService - *

- * - * @author yangkai.shen - * @date Created in 2018-11-16 16:54 - */ -@Service -@Slf4j -public class UserServiceImpl implements UserService { - /** - * 模拟数据库 - */ - private static final Map DATABASES = Maps.newConcurrentMap(); - - /** - * 初始化数据 - */ - static { - DATABASES.put(1L, new User(1L, "user1")); - DATABASES.put(2L, new User(2L, "user2")); - DATABASES.put(3L, new User(3L, "user3")); - } - - /** - * 保存或修改用户 - * - * @param user 用户对象 - * @return 操作结果 - */ - @CachePut(value = "user", key = "#user.id") - @Override - public User saveOrUpdate(User user) { - DATABASES.put(user.getId(), user); - log.info("保存用户【user】= {}", user); - return user; - } - - /** - * 获取用户 - * - * @param id key值 - * @return 返回结果 - */ - @Cacheable(value = "user", key = "#id") - @Override - public User get(Long id) { - // 我们假设从数据库读取 - log.info("查询用户【id】= {}", id); - return DATABASES.get(id); - } - - /** - * 删除 - * - * @param id key值 - */ - @CacheEvict(value = "user", key = "#id") - @Override - public void delete(Long id) { - DATABASES.remove(id); - log.info("删除用户【id】= {}", id); - } -} diff --git a/demo-cache/demo-cache-ehcache/src/main/resources/application.yml b/demo-cache/demo-cache-ehcache/src/main/resources/application.yml index 7f543c7..35c6f4c 100644 --- a/demo-cache/demo-cache-ehcache/src/main/resources/application.yml +++ b/demo-cache/demo-cache-ehcache/src/main/resources/application.yml @@ -1,8 +1,8 @@ spring: cache: - type: ehcache - ehcache: - config: classpath:ehcache.xml + type: jcache + jcache: + config: classpath:ehcache3.xml logging: level: - com.xkcoding: debug \ No newline at end of file + com.xkcoding: debug diff --git a/demo-cache/demo-cache-ehcache/src/main/resources/ehcache.xml b/demo-cache/demo-cache-ehcache/src/main/resources/ehcache.xml deleted file mode 100644 index c4e1cd5..0000000 --- a/demo-cache/demo-cache-ehcache/src/main/resources/ehcache.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/demo-cache/demo-cache-ehcache/src/main/resources/ehcache3.xml b/demo-cache/demo-cache-ehcache/src/main/resources/ehcache3.xml new file mode 100644 index 0000000..343c79b --- /dev/null +++ b/demo-cache/demo-cache-ehcache/src/main/resources/ehcache3.xml @@ -0,0 +1,24 @@ + + + + + + + + + 600 + + + + 2000 + 100 + + + + + + + diff --git a/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/EhcacheCacheApplicationTests.java b/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/EhcacheCacheApplicationTests.java new file mode 100644 index 0000000..0a63868 --- /dev/null +++ b/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/EhcacheCacheApplicationTests.java @@ -0,0 +1,13 @@ +package com.xkcoding.cache.ehcache; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class EhcacheCacheApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java b/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java deleted file mode 100644 index 3fa6624..0000000 --- a/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xkcoding.cache.ehcache; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringBootDemoCacheEhcacheApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java b/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java index 00d3b0f..088449b 100644 --- a/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java +++ b/demo-cache/demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java @@ -1,10 +1,11 @@ package com.xkcoding.cache.ehcache.service; -import com.xkcoding.cache.ehcache.SpringBootDemoCacheEhcacheApplicationTests; -import com.xkcoding.cache.ehcache.entity.User; +import com.xkcoding.cache.api.UserService; +import com.xkcoding.cache.entity.User; import lombok.extern.slf4j.Slf4j; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; /** *

@@ -15,7 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired; * @date Created in 2018-11-16 16:58 */ @Slf4j -public class UserServiceTest extends SpringBootDemoCacheEhcacheApplicationTests { +@SpringBootTest +public class UserServiceTest { @Autowired private UserService userService; diff --git a/demo-cache/pom.xml b/demo-cache/pom.xml index 89df467..ffafc44 100644 --- a/demo-cache/pom.xml +++ b/demo-cache/pom.xml @@ -20,6 +20,7 @@ demo-cache-api + demo-cache-ehcache demo-cache-redis