RedisCacheController.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.ruoyi.demo.controller;
  2. import com.ruoyi.common.core.constant.CacheNames;
  3. import com.ruoyi.common.core.domain.R;
  4. import com.ruoyi.common.redis.utils.RedisUtils;
  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 = CacheNames.DEMO_CACHE)
  20. @RequiredArgsConstructor
  21. @RestController
  22. @RequestMapping("/demo/cache")
  23. public class RedisCacheController {
  24. /**
  25. * 测试 @Cacheable
  26. * <p>
  27. * 表示这个方法有了缓存的功能,方法的返回值会被缓存下来
  28. * 下一次调用该方法前,会去检查是否缓存中已经有值
  29. * 如果有就直接返回,不调用方法
  30. * 如果没有,就调用方法,然后把结果缓存起来
  31. * 这个注解「一般用在查询方法上」
  32. * <p>
  33. * 重点说明: 缓存注解严谨与其他筛选数据功能一起使用
  34. * 例如: 数据权限注解 会造成 缓存击穿 与 数据不一致问题
  35. * <p>
  36. * cacheNames 命名规则 查看 {@link CacheNames} 注释 支持多参数
  37. */
  38. @Cacheable(cacheNames = "demo:cache#60s#10m#20", key = "#key", condition = "#key != null")
  39. @GetMapping("/test1")
  40. public R<String> test1(String key, String value) {
  41. return R.ok("操作成功", value);
  42. }
  43. /**
  44. * 测试 @CachePut
  45. * <p>
  46. * 加了@CachePut注解的方法,会把方法的返回值put到缓存里面缓存起来,供其它地方使用
  47. * 它「通常用在新增或者实时更新方法上」
  48. * <p>
  49. * cacheNames 命名规则 查看 {@link CacheNames} 注释 支持多参数
  50. */
  51. @CachePut(cacheNames = CacheNames.DEMO_CACHE, key = "#key", condition = "#key != null")
  52. @GetMapping("/test2")
  53. public R<String> test2(String key, String value) {
  54. return R.ok("操作成功", value);
  55. }
  56. /**
  57. * 测试 @CacheEvict
  58. * <p>
  59. * 使用了CacheEvict注解的方法,会清空指定缓存
  60. * 「一般用在删除的方法上」
  61. * <p>
  62. * cacheNames 命名规则 查看 {@link CacheNames} 注释 支持多参数
  63. */
  64. @CacheEvict(cacheNames = CacheNames.DEMO_CACHE, key = "#key", condition = "#key != null")
  65. @GetMapping("/test3")
  66. public R<String> test3(String key, String value) {
  67. return R.ok("操作成功", value);
  68. }
  69. /**
  70. * 测试设置过期时间
  71. * 手动设置过期时间10秒
  72. * 11秒后获取 判断是否相等
  73. */
  74. @GetMapping("/test6")
  75. public R<Boolean> test6(String key, String value) {
  76. RedisUtils.setCacheObject(key, value);
  77. boolean flag = RedisUtils.expire(key, Duration.ofSeconds(10));
  78. System.out.println("***********" + flag);
  79. try {
  80. Thread.sleep(11 * 1000);
  81. } catch (InterruptedException e) {
  82. e.printStackTrace();
  83. }
  84. Object obj = RedisUtils.getCacheObject(key);
  85. return R.ok(value.equals(obj));
  86. }
  87. }