You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # spring-boot-demo-cache-ehcache
  2. > 此 demo 主要演示了 Spring Boot 如何集成 ehcache3 使用缓存。
  3. ## 1.开发步骤
  4. ### 1.1.添加依赖
  5. > 注意:Spring Boot 3 依赖 jakarta 的包,但是默认的 ehcache 依赖的是 javax 的,所以需要再引入依赖的时候设置 `<classifier>jakarta</classifier>`
  6. ```xml
  7. <dependencies>
  8. <dependency>
  9. <groupId>com.xkcoding</groupId>
  10. <artifactId>demo-cache-api</artifactId>
  11. <version>1.0.0-SNAPSHOT</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-cache</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-test</artifactId>
  24. <scope>test</scope>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.cache</groupId>
  28. <artifactId>cache-api</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.ehcache</groupId>
  32. <artifactId>ehcache</artifactId>
  33. <version>${ehcache.version}</version>
  34. <classifier>jakarta</classifier>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.projectlombok</groupId>
  38. <artifactId>lombok</artifactId>
  39. <optional>true</optional>
  40. </dependency>
  41. </dependencies>
  42. ```
  43. ### 1.2.修改配置文件
  44. ```yaml
  45. spring:
  46. cache:
  47. type: jcache
  48. jcache:
  49. config: classpath:ehcache3.xml
  50. logging:
  51. level:
  52. com.xkcoding: debug
  53. ```
  54. ### 1.3.配置 ehcache
  55. ```xml
  56. <?xml version="1.0" encoding="UTF-8" ?>
  57. <eh:config
  58. xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  59. xmlns:eh='http://www.ehcache.org/v3'
  60. xsi:schemaLocation="http://www.ehcache.org/v3 https://www.ehcache.org/schema/ehcache-core-3.3.xsd">
  61. <!--指定缓存目录-->
  62. <eh:persistence directory="${java.io.tmpdir}/cache-data"/>
  63. <!--缓存模板-->
  64. <eh:cache-template name="default">
  65. <eh:expiry>
  66. <eh:ttl unit="seconds">600</eh:ttl>
  67. </eh:expiry>
  68. <eh:resources>
  69. <!--堆内内存可以放2000个条目,超出部分堆外100MB-->
  70. <eh:heap unit="entries">2000</eh:heap>
  71. <eh:offheap unit="MB">100</eh:offheap>
  72. </eh:resources>
  73. </eh:cache-template>
  74. <!--实际的缓存区间,继承了default缓存模板,user完全使用模板默认-->
  75. <eh:cache alias="user" uses-template="default"/>
  76. </eh:config>
  77. ```
  78. ### 1.4.开启缓存自动装配
  79. ```java
  80. @EnableCaching
  81. @Configuration(proxyBeanMethods = false)
  82. public class EhcacheCacheAutoConfiguration {
  83. }
  84. ```
  85. ### 1.5.模拟数据服务
  86. > 为了减少重复代码,该部分我将其抽取实现在 demo-cache-api 模块中
  87. ## 2.测试
  88. ```java
  89. @Slf4j
  90. @SpringBootTest
  91. public class UserServiceTest {
  92. @Autowired
  93. private UserService userService;
  94. /**
  95. * 获取两次,查看日志验证缓存
  96. */
  97. @Test
  98. public void getTwice() {
  99. // 模拟查询id为1的用户
  100. User user1 = userService.get(1L);
  101. log.debug("【user1】= {}", user1);
  102. // 再次查询
  103. User user2 = userService.get(1L);
  104. log.debug("【user2】= {}", user2);
  105. // 查看日志,只打印一次日志,证明缓存生效
  106. }
  107. /**
  108. * 先存,再查询,查看日志验证缓存
  109. */
  110. @Test
  111. public void getAfterSave() {
  112. userService.saveOrUpdate(new User(4L, "user4"));
  113. User user = userService.get(4L);
  114. log.debug("【user】= {}", user);
  115. // 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效
  116. }
  117. /**
  118. * 测试删除,查看redis是否存在缓存数据
  119. */
  120. @Test
  121. public void deleteUser() {
  122. // 查询一次,使ehcache中存在缓存数据
  123. userService.get(1L);
  124. // 删除,查看ehcache是否存在缓存数据
  125. userService.delete(1L);
  126. }
  127. }
  128. ```
  129. ## 3.参考
  130. - [Ehcache 官网](http://www.ehcache.org/documentation/)
  131. - [Spring Boot 官方文档之 JCache 集成](https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#io.caching.provider.jcache)