From 255c8a083ecf864f84c05677323c9ac0bb14672b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Thu, 21 Oct 2021 09:33:13 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A2=9E=E5=8A=A0=20RedisUtils=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=AE=BE=E7=BD=AE=E8=BF=87=E6=9C=9F=E6=97=B6?= =?UTF-8?q?=E9=97=B4=20=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/RedisCacheController.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java index 5ef9d49c..903b982b 100644 --- a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java +++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java @@ -1,6 +1,7 @@ package com.ruoyi.demo.controller; import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.utils.RedisUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.concurrent.TimeUnit; + /** * spring-cache 演示案例 * @@ -76,4 +79,24 @@ public class RedisCacheController { return AjaxResult.success("操作成功", value); } + /** + * 测试设置过期时间 + * 手动设置过期时间10秒 + * 11秒后获取 判断是否相等 + */ + @ApiOperation("测试设置过期时间") + @GetMapping("/test6") + public AjaxResult test6(String key, String value){ + RedisUtils.setCacheObject(key, value); + boolean flag = RedisUtils.expire(key, 10, TimeUnit.SECONDS); + System.out.println("***********" + flag); + try { + Thread.sleep(11 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Object obj = RedisUtils.getCacheObject(key); + return AjaxResult.success("操作成功", value.equals(obj)); + } + }