From e6f43c68b0899b023da7d7c98c941fbd1148663f Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 17 Nov 2018 14:48:29 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-cache-ehcache=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + spring-boot-demo-cache-ehcache/.gitignore | 25 ++ spring-boot-demo-cache-ehcache/README.md | 301 ++++++++++++++++++ spring-boot-demo-cache-ehcache/pom.xml | 69 ++++ ...SpringBootDemoCacheEhcacheApplication.java | 27 ++ .../xkcoding/cache/ehcache/entity/User.java | 35 ++ .../cache/ehcache/service/UserService.java | 41 +++ .../ehcache/service/impl/UserServiceImpl.java | 83 +++++ .../src/main/resources/application.yml | 8 + .../src/main/resources/ehcache.xml | 37 +++ ...gBootDemoCacheEhcacheApplicationTests.java | 16 + .../ehcache/service/UserServiceTest.java | 65 ++++ 12 files changed, 708 insertions(+) create mode 100644 spring-boot-demo-cache-ehcache/.gitignore create mode 100644 spring-boot-demo-cache-ehcache/README.md create mode 100644 spring-boot-demo-cache-ehcache/pom.xml create mode 100644 spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java create mode 100644 spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java create mode 100644 spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java create mode 100644 spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java create mode 100644 spring-boot-demo-cache-ehcache/src/main/resources/application.yml create mode 100644 spring-boot-demo-cache-ehcache/src/main/resources/ehcache.xml create mode 100644 spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java create mode 100644 spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java diff --git a/pom.xml b/pom.xml index c34ee8e..b729a7b 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,7 @@ spring-boot-demo-orm-mybatis-plus spring-boot-demo-orm-beetlsql spring-boot-demo-cache-redis + spring-boot-demo-cache-ehcache spring-boot-demo-email spring-boot-demo-upload spring-boot-demo-war diff --git a/spring-boot-demo-cache-ehcache/.gitignore b/spring-boot-demo-cache-ehcache/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/.gitignore @@ -0,0 +1,25 @@ +/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/spring-boot-demo-cache-ehcache/README.md b/spring-boot-demo-cache-ehcache/README.md new file mode 100644 index 0000000..dd071ba --- /dev/null +++ b/spring-boot-demo-cache-ehcache/README.md @@ -0,0 +1,301 @@ +# spring-boot-demo-cache-ehcache + +> 此 demo 主要演示了 Spring Boot 如何集成 ehcache 使用缓存。 + +## pom.xml + +```xml + + + 4.0.0 + + 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 + + + + + +``` + +## SpringBootDemoCacheEhcacheApplication.java + +```java +/** + *

+ * 启动类 + *

+ * + * @package: com.xkcoding.cache.ehcache + * @description: 启动类 + * @author: yangkai.shen + * @date: Created in 2018/11/16 17:02 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@SpringBootApplication +@EnableCaching +public class SpringBootDemoCacheEhcacheApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoCacheEhcacheApplication.class, args); + } +} +``` + +## application.yml + +```yaml +spring: + cache: + type: ehcache + ehcache: + config: classpath:ehcache.xml +logging: + level: + com.xkcoding: debug +``` + +## ehcache.xml + +```xml + + + + + + + + + + + +``` + +## UserServiceImpl.java + +```java +/** + *

+ * UserService + *

+ * + * @package: com.xkcoding.cache.ehcache.service.impl + * @description: UserService + * @author: yangkai.shen + * @date: Created in 2018/11/16 16:54 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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); + } +} +``` + +## UserServiceTest.java + +```java +/** + *

+ * ehcache缓存测试 + *

+ * + * @package: com.xkcoding.cache.ehcache.service + * @description: ehcache缓存测试 + * @author: yangkai.shen + * @date: Created in 2018/11/16 16:58 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class UserServiceTest extends SpringBootDemoCacheEhcacheApplicationTests { + + @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); + // 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效 + } + + /** + * 测试删除,查看redis是否存在缓存数据 + */ + @Test + public void deleteUser() { + // 查询一次,使ehcache中存在缓存数据 + userService.get(1L); + // 删除,查看ehcache是否存在缓存数据 + userService.delete(1L); + } +} +``` + +## 参考 + +- 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 \ No newline at end of file diff --git a/spring-boot-demo-cache-ehcache/pom.xml b/spring-boot-demo-cache-ehcache/pom.xml new file mode 100644 index 0000000..62f7e9b --- /dev/null +++ b/spring-boot-demo-cache-ehcache/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + 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 + + + + + diff --git a/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java new file mode 100644 index 0000000..4b4f082 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplication.java @@ -0,0 +1,27 @@ +package com.xkcoding.cache.ehcache; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +/** + *

+ * 启动类 + *

+ * + * @package: com.xkcoding.cache.ehcache + * @description: 启动类 + * @author: yangkai.shen + * @date: Created in 2018/11/16 17:02 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@SpringBootApplication +@EnableCaching +public class SpringBootDemoCacheEhcacheApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoCacheEhcacheApplication.class, args); + } +} diff --git a/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java new file mode 100644 index 0000000..20c8187 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/entity/User.java @@ -0,0 +1,35 @@ +package com.xkcoding.cache.ehcache.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + *

+ * 用户实体 + *

+ * + * @package: com.xkcoding.cache.ehcache.entity + * @description: 用户实体 + * @author: yangkai.shen + * @date: Created in 2018/11/16 16:53 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User implements Serializable { + private static final long serialVersionUID = 2892248514883451461L; + /** + * 主键id + */ + private Long id; + /** + * 姓名 + */ + private String name; +} diff --git a/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java new file mode 100644 index 0000000..f607d02 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/UserService.java @@ -0,0 +1,41 @@ +package com.xkcoding.cache.ehcache.service; + +import com.xkcoding.cache.ehcache.entity.User; + +/** + *

+ * UserService + *

+ * + * @package: com.xkcoding.cache.ehcache.service + * @description: UserService + * @author: yangkai.shen + * @date: Created in 2018/11/16 16:53 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +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/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..dca9949 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/main/java/com/xkcoding/cache/ehcache/service/impl/UserServiceImpl.java @@ -0,0 +1,83 @@ +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 + *

+ * + * @package: com.xkcoding.cache.ehcache.service.impl + * @description: UserService + * @author: yangkai.shen + * @date: Created in 2018/11/16 16:54 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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/spring-boot-demo-cache-ehcache/src/main/resources/application.yml b/spring-boot-demo-cache-ehcache/src/main/resources/application.yml new file mode 100644 index 0000000..7f543c7 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/main/resources/application.yml @@ -0,0 +1,8 @@ +spring: + cache: + type: ehcache + ehcache: + config: classpath:ehcache.xml +logging: + level: + com.xkcoding: debug \ No newline at end of file diff --git a/spring-boot-demo-cache-ehcache/src/main/resources/ehcache.xml b/spring-boot-demo-cache-ehcache/src/main/resources/ehcache.xml new file mode 100644 index 0000000..c4e1cd5 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/main/resources/ehcache.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java b/spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java new file mode 100644 index 0000000..3fa6624 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/SpringBootDemoCacheEhcacheApplicationTests.java @@ -0,0 +1,16 @@ +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/spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java b/spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java new file mode 100644 index 0000000..0720b54 --- /dev/null +++ b/spring-boot-demo-cache-ehcache/src/test/java/com/xkcoding/cache/ehcache/service/UserServiceTest.java @@ -0,0 +1,65 @@ +package com.xkcoding.cache.ehcache.service; + +import com.xkcoding.cache.ehcache.SpringBootDemoCacheEhcacheApplicationTests; +import com.xkcoding.cache.ehcache.entity.User; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +/** + *

+ * ehcache缓存测试 + *

+ * + * @package: com.xkcoding.cache.ehcache.service + * @description: ehcache缓存测试 + * @author: yangkai.shen + * @date: Created in 2018/11/16 16:58 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class UserServiceTest extends SpringBootDemoCacheEhcacheApplicationTests { + + @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); + // 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效 + } + + /** + * 测试删除,查看redis是否存在缓存数据 + */ + @Test + public void deleteUser() { + // 查询一次,使ehcache中存在缓存数据 + userService.get(1L); + // 删除,查看ehcache是否存在缓存数据 + userService.delete(1L); + } +} \ No newline at end of file