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
-
-
- * 启动类 - *
- * - * @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 +- * UserService - *
- * - * @author yangkai.shen - * @date Created in 2018-11-16 16:54 - */ -@Service -@Slf4j -public class UserServiceImpl implements UserService { - /** - * 模拟数据库 - */ - private static final Map- * 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 @@@@ -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
@@ -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 @@