RedisCacheController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  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. @Api(value = "spring-cache 演示案例", tags = {"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. @ApiOperation("测试 @Cacheable")
  41. @Cacheable(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
  42. @GetMapping("/test1")
  43. public R<String> test1(String key, String value) {
  44. return R.ok("操作成功", value);
  45. }
  46. /**
  47. * 测试 @CachePut
  48. * <p>
  49. * 加了@CachePut注解的方法,会把方法的返回值put到缓存里面缓存起来,供其它地方使用
  50. * 它「通常用在新增方法上」
  51. * <p>
  52. * cacheNames 为 配置文件内 groupId
  53. */
  54. @ApiOperation("测试 @CachePut")
  55. @CachePut(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
  56. @GetMapping("/test2")
  57. public R<String> test2(String key, String value) {
  58. return R.ok("操作成功", value);
  59. }
  60. /**
  61. * 测试 @CacheEvict
  62. * <p>
  63. * 使用了CacheEvict注解的方法,会清空指定缓存
  64. * 「一般用在更新或者删除的方法上」
  65. * <p>
  66. * cacheNames 为 配置文件内 groupId
  67. */
  68. @ApiOperation("测试 @CacheEvict")
  69. @CacheEvict(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
  70. @GetMapping("/test3")
  71. public R<String> test3(String key, String value) {
  72. return R.ok("操作成功", value);
  73. }
  74. /**
  75. * 测试设置过期时间
  76. * 手动设置过期时间10秒
  77. * 11秒后获取 判断是否相等
  78. */
  79. @ApiOperation("测试设置过期时间")
  80. @GetMapping("/test6")
  81. public R<Boolean> test6(String key, String value) {
  82. RedisUtils.setCacheObject(key, value);
  83. boolean flag = RedisUtils.expire(key, Duration.ofSeconds(10));
  84. System.out.println("***********" + flag);
  85. try {
  86. Thread.sleep(11 * 1000);
  87. } catch (InterruptedException e) {
  88. e.printStackTrace();
  89. }
  90. Object obj = RedisUtils.getCacheObject(key);
  91. return R.ok(value.equals(obj));
  92. }
  93. }