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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # spring-boot-demo-cache-redis
  2. 依赖 [spring-boot-demo-parent](../spring-boot-demo-parent)、`spring-boot-starter-data-redis`
  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-redis</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-cache-redis</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo-parent</artifactId>
  17. <version>0.0.1-SNAPSHOT</version>
  18. <relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
  19. </parent>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-redis</artifactId>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <finalName>spring-boot-demo-cache-redis</finalName>
  28. </build>
  29. </project>
  30. ```
  31. ### application.yml
  32. ```yaml
  33. server:
  34. port: 8080
  35. context-path: /demo
  36. spring:
  37. redis:
  38. host: localhost
  39. port: 6379
  40. ```
  41. ### SpringBootDemoCacheRedisApplication.java (开启使用缓存注解)
  42. ```java
  43. @SpringBootApplication
  44. @EnableCaching
  45. public class SpringBootDemoCacheRedisApplication {
  46. public static void main(String[] args) {
  47. SpringApplication.run(SpringBootDemoCacheRedisApplication.class, args);
  48. }
  49. }
  50. ```
  51. ### User.java (一定要序列化!)
  52. ```java
  53. @Data
  54. public class User implements Serializable {
  55. private static final long serialVersionUID = 1442134563392432775L;
  56. private Long id;
  57. private String name;
  58. private Date birthDay;
  59. }
  60. ```
  61. ### UserController.java
  62. ```java
  63. @RestController
  64. @RequestMapping("/user")
  65. @CacheConfig(cacheNames = "users") // 整体配置缓存的名称
  66. public class UserController {
  67. @Autowired
  68. private UserService userService;
  69. @GetMapping({"", "/"})
  70. @Cacheable
  71. public List<User> index() {
  72. return userService.find();
  73. }
  74. @GetMapping("/find/{id}")
  75. @Cacheable(key = "#id", condition = "#id != null") // #id 以及 condition 里的语法是 SpEL 语法
  76. public User find(@PathVariable Long id) {
  77. return userService.find(id);
  78. }
  79. @GetMapping("/find")
  80. @Cacheable(key = "#name", condition = "#name != null")
  81. public User find(@RequestParam String name) {
  82. return userService.find(name);
  83. }
  84. @GetMapping("/save")
  85. @CacheEvict(allEntries = true, beforeInvocation = true) //@CacheEvict 清空缓存
  86. public User save() {
  87. return userService.save();
  88. }
  89. @GetMapping("/update/{id}")
  90. @CacheEvict(allEntries = true, beforeInvocation = true)
  91. public User update(@PathVariable Long id) {
  92. return userService.update(id);
  93. }
  94. @GetMapping("/delete/{id}")
  95. @CacheEvict(allEntries = true, beforeInvocation = true)
  96. public String delete(@PathVariable Long id) {
  97. User user = userService.delete(id);
  98. if (user == null) {
  99. return "用户不存在";
  100. } else {
  101. return user.getName() + "删除成功";
  102. }
  103. }
  104. }
  105. ```
  106. ### 其余 Service、Dao 代码(采用 List 存储,只为了演示启用 cache 后,是否仍会进入对应的 method)
  107. 详情请参见本demo。
  108. ### 缓存 @Annotation 详解
  109. #### @CacheConfig
  110. 用于统一配置缓存的基本信息
  111. #### @Cacheable
  112. 将返回的结果缓存,该注解里的参数如下
  113. | 参数 | 作用 | 示例 |
  114. | --------- | ---------------------------------------- | ---------------------------------------- |
  115. | value | 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 | 例如:@Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
  116. | key | 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 | 例如:@Cacheable(value=”testcache”,key=”#userName”) |
  117. | condition | 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 | 例如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”) |
  118. #### @CacheEvict
  119. 能够根据一定的条件对缓存进行清空,该注解里的参数如下
  120. | 参数 | 作用 | 示例 |
  121. | ---------------- | ---------------------------------------- | ---------------------------------------- |
  122. | value | 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 | 例如:@CachEvict(value=”mycache”) 或者 @CachEvict(value={”cache1”,”cache2”} |
  123. | key | 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 | 例如:@CachEvict(value=”testcache”,key=”#userName”) |
  124. | condition | 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才清空缓存 | 例如:@CachEvict(value=”testcache”,condition=”#userName.length()>2”) |
  125. | allEntries | 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 | 例如:@CachEvict(value=”testcache”,allEntries=true) |
  126. | beforeInvocation | 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存 | 例如:@CachEvict(value=”testcache”,beforeInvocation=true) |
  127. #### @CachePut
  128. 能够根据方法的请求参数对其结果进行缓存,和 `@Cacheable` 不同的是,它每次都会触发真实方法的调用
  129. | 参数 | 作用 | 示例 |
  130. | --------- | ---------------------------------------- | ---------------------------------------- |
  131. | value | 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 | 例如:@Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
  132. | key | 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 | 例如:@Cacheable(value=”testcache”,key=”#userName”) |
  133. | condition | 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 | 例如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”) |

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

Contributors (1)