RedisCacheController.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.ruoyi.demo.controller;
  2. import com.ruoyi.common.core.domain.R;
  3. import com.ruoyi.common.utils.redis.RedisUtils;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.tags.Tag;
  6. import lombok.RequiredArgsConstructor;
  7. import org.springframework.cache.annotation.CacheEvict;
  8. import org.springframework.cache.annotation.CachePut;
  9. import org.springframework.cache.annotation.Cacheable;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import java.time.Duration;
  14. /**
  15. * spring-cache 演示案例
  16. *
  17. * @author Lion Li
  18. */
  19. // 类级别 缓存统一配置
  20. //@CacheConfig(cacheNames = "redissonCacheMap")
  21. @Tag(name ="spring-cache 演示案例", description = "spring-cache 演示案例")
  22. @RequiredArgsConstructor
  23. @RestController
  24. @RequestMapping("/demo/cache")
  25. public class RedisCacheController {
  26. /**
  27. * 测试 @Cacheable
  28. * <p>
  29. * 表示这个方法有了缓存的功能,方法的返回值会被缓存下来
  30. * 下一次调用该方法前,会去检查是否缓存中已经有值
  31. * 如果有就直接返回,不调用方法
  32. * 如果没有,就调用方法,然后把结果缓存起来
  33. * 这个注解「一般用在查询方法上」
  34. * <p>
  35. * 重点说明: 缓存注解严谨与其他筛选数据功能一起使用
  36. * 例如: 数据权限注解 会造成 缓存击穿 与 数据不一致问题
  37. * <p>
  38. * cacheNames 为配置文件内 groupId
  39. */
  40. @Cacheable(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
  41. @GetMapping("/test1")
  42. public R<String> test1(String key, String value) {
  43. return R.ok("操作成功", value);
  44. }
  45. /**
  46. * 测试 @CachePut
  47. * <p>
  48. * 加了@CachePut注解的方法,会把方法的返回值put到缓存里面缓存起来,供其它地方使用
  49. * 它「通常用在新增方法上」
  50. * <p>
  51. * cacheNames 为 配置文件内 groupId
  52. */
  53. @CachePut(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
  54. @GetMapping("/test2")
  55. public R<String> test2(String key, String value) {
  56. return R.ok("操作成功", value);
  57. }
  58. /**
  59. * 测试 @CacheEvict
  60. * <p>
  61. * 使用了CacheEvict注解的方法,会清空指定缓存
  62. * 「一般用在更新或者删除的方法上」
  63. * <p>
  64. * cacheNames 为 配置文件内 groupId
  65. */
  66. @CacheEvict(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
  67. @GetMapping("/test3")
  68. public R<String> test3(String key, String value) {
  69. return R.ok("操作成功", value);
  70. }
  71. /**
  72. * 测试设置过期时间
  73. * 手动设置过期时间10秒
  74. * 11秒后获取 判断是否相等
  75. */
  76. @GetMapping("/test6")
  77. public R<Boolean> test6(String key, String value) {
  78. RedisUtils.setCacheObject(key, value);
  79. boolean flag = RedisUtils.expire(key, Duration.ofSeconds(10));
  80. System.out.println("***********" + flag);
  81. try {
  82. Thread.sleep(11 * 1000);
  83. } catch (InterruptedException e) {
  84. e.printStackTrace();
  85. }
  86. Object obj = RedisUtils.getCacheObject(key);
  87. return R.ok(value.equals(obj));
  88. }
  89. }