RedisCacheController.java 3.3 KB

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