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 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # spring-boot-demo-cache-ehcache
  2. > 此 demo 主要演示了 Spring Boot 如何集成 ehcache 使用缓存。
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-cache-ehcache</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-cache-ehcache</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-cache</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>net.sf.ehcache</groupId>
  35. <artifactId>ehcache</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.projectlombok</groupId>
  39. <artifactId>lombok</artifactId>
  40. <optional>true</optional>
  41. </dependency>
  42. <dependency>
  43. <groupId>com.google.guava</groupId>
  44. <artifactId>guava</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-test</artifactId>
  49. <scope>test</scope>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <finalName>spring-boot-demo-cache-ehcache</finalName>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>
  62. ```
  63. ## SpringBootDemoCacheEhcacheApplication.java
  64. ```java
  65. /**
  66. * <p>
  67. * 启动类
  68. * </p>
  69. *
  70. * @package: com.xkcoding.cache.ehcache
  71. * @description: 启动类
  72. * @author: yangkai.shen
  73. * @date: Created in 2018/11/16 17:02
  74. * @copyright: Copyright (c) 2018
  75. * @version: V1.0
  76. * @modified: yangkai.shen
  77. */
  78. @SpringBootApplication
  79. @EnableCaching
  80. public class SpringBootDemoCacheEhcacheApplication {
  81. public static void main(String[] args) {
  82. SpringApplication.run(SpringBootDemoCacheEhcacheApplication.class, args);
  83. }
  84. }
  85. ```
  86. ## application.yml
  87. ```yaml
  88. spring:
  89. cache:
  90. type: ehcache
  91. ehcache:
  92. config: classpath:ehcache.xml
  93. logging:
  94. level:
  95. com.xkcoding: debug
  96. ```
  97. ## ehcache.xml
  98. ```xml
  99. <!-- ehcache配置 -->
  100. <ehcache
  101. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  103. updateCheck="false">
  104. <!--缓存路径,用户目录下的base_ehcache目录-->
  105. <diskStore path="user.home/base_ehcache"/>
  106. <defaultCache
  107. maxElementsInMemory="20000"
  108. eternal="false"
  109. timeToIdleSeconds="120"
  110. timeToLiveSeconds="120"
  111. overflowToDisk="true"
  112. maxElementsOnDisk="10000000"
  113. diskPersistent="false"
  114. diskExpiryThreadIntervalSeconds="120"
  115. memoryStoreEvictionPolicy="LRU"/>
  116. <!--
  117. 缓存文件名:user,同样的可以配置多个缓存
  118. maxElementsInMemory:内存中最多存储
  119. eternal:外部存储
  120. overflowToDisk:超出缓存到磁盘
  121. diskPersistent:磁盘持久化
  122. timeToLiveSeconds:缓存时间
  123. diskExpiryThreadIntervalSeconds:磁盘过期时间
  124. -->
  125. <cache name="user"
  126. maxElementsInMemory="20000"
  127. eternal="true"
  128. overflowToDisk="true"
  129. diskPersistent="false"
  130. timeToLiveSeconds="0"
  131. diskExpiryThreadIntervalSeconds="120"/>
  132. </ehcache>
  133. ```
  134. ## UserServiceImpl.java
  135. ```java
  136. /**
  137. * <p>
  138. * UserService
  139. * </p>
  140. *
  141. * @package: com.xkcoding.cache.ehcache.service.impl
  142. * @description: UserService
  143. * @author: yangkai.shen
  144. * @date: Created in 2018/11/16 16:54
  145. * @copyright: Copyright (c) 2018
  146. * @version: V1.0
  147. * @modified: yangkai.shen
  148. */
  149. @Service
  150. @Slf4j
  151. public class UserServiceImpl implements UserService {
  152. /**
  153. * 模拟数据库
  154. */
  155. private static final Map<Long, User> DATABASES = Maps.newConcurrentMap();
  156. /**
  157. * 初始化数据
  158. */
  159. static {
  160. DATABASES.put(1L, new User(1L, "user1"));
  161. DATABASES.put(2L, new User(2L, "user2"));
  162. DATABASES.put(3L, new User(3L, "user3"));
  163. }
  164. /**
  165. * 保存或修改用户
  166. *
  167. * @param user 用户对象
  168. * @return 操作结果
  169. */
  170. @CachePut(value = "user", key = "#user.id")
  171. @Override
  172. public User saveOrUpdate(User user) {
  173. DATABASES.put(user.getId(), user);
  174. log.info("保存用户【user】= {}", user);
  175. return user;
  176. }
  177. /**
  178. * 获取用户
  179. *
  180. * @param id key值
  181. * @return 返回结果
  182. */
  183. @Cacheable(value = "user", key = "#id")
  184. @Override
  185. public User get(Long id) {
  186. // 我们假设从数据库读取
  187. log.info("查询用户【id】= {}", id);
  188. return DATABASES.get(id);
  189. }
  190. /**
  191. * 删除
  192. *
  193. * @param id key值
  194. */
  195. @CacheEvict(value = "user", key = "#id")
  196. @Override
  197. public void delete(Long id) {
  198. DATABASES.remove(id);
  199. log.info("删除用户【id】= {}", id);
  200. }
  201. }
  202. ```
  203. ## UserServiceTest.java
  204. ```java
  205. /**
  206. * <p>
  207. * ehcache缓存测试
  208. * </p>
  209. *
  210. * @package: com.xkcoding.cache.ehcache.service
  211. * @description: ehcache缓存测试
  212. * @author: yangkai.shen
  213. * @date: Created in 2018/11/16 16:58
  214. * @copyright: Copyright (c) 2018
  215. * @version: V1.0
  216. * @modified: yangkai.shen
  217. */
  218. @Slf4j
  219. public class UserServiceTest extends SpringBootDemoCacheEhcacheApplicationTests {
  220. @Autowired
  221. private UserService userService;
  222. /**
  223. * 获取两次,查看日志验证缓存
  224. */
  225. @Test
  226. public void getTwice() {
  227. // 模拟查询id为1的用户
  228. User user1 = userService.get(1L);
  229. log.debug("【user1】= {}", user1);
  230. // 再次查询
  231. User user2 = userService.get(1L);
  232. log.debug("【user2】= {}", user2);
  233. // 查看日志,只打印一次日志,证明缓存生效
  234. }
  235. /**
  236. * 先存,再查询,查看日志验证缓存
  237. */
  238. @Test
  239. public void getAfterSave() {
  240. userService.saveOrUpdate(new User(4L, "user4"));
  241. User user = userService.get(4L);
  242. log.debug("【user】= {}", user);
  243. // 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效
  244. }
  245. /**
  246. * 测试删除,查看redis是否存在缓存数据
  247. */
  248. @Test
  249. public void deleteUser() {
  250. // 查询一次,使ehcache中存在缓存数据
  251. userService.get(1L);
  252. // 删除,查看ehcache是否存在缓存数据
  253. userService.delete(1L);
  254. }
  255. }
  256. ```
  257. ## 参考
  258. - Ehcache 官网:http://www.ehcache.org/documentation/
  259. - Spring Boot 官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-ehcache2
  260. - 博客:https://juejin.im/post/5b308de9518825748b56ae1d

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。