RedisCacheController.java 3.2 KB

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