疯狂的狮子li
2022-07-07 d9e54388e7fde2462e8b7e0165c5e7da3f202593
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java
@@ -1,9 +1,9 @@
package com.ruoyi.demo.controller;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
@@ -12,7 +12,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
import java.time.Duration;
/**
 * spring-cache 演示案例
@@ -21,7 +21,7 @@
 */
// 类级别 缓存统一配置
//@CacheConfig(cacheNames = "redissonCacheMap")
@Api(value = "spring-cache 演示案例", tags = {"spring-cache 演示案例"})
@Tag(name ="spring-cache 演示案例", description = "spring-cache 演示案例")
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/cache")
@@ -41,11 +41,10 @@
     * <p>
     * cacheNames 为配置文件内 groupId
     */
    @ApiOperation("测试 @Cacheable")
    @Cacheable(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
    @GetMapping("/test1")
    public AjaxResult<String> test1(String key, String value) {
        return AjaxResult.success("操作成功", value);
    public R<String> test1(String key, String value) {
        return R.ok("操作成功", value);
    }
    /**
@@ -56,11 +55,10 @@
     * <p>
     * cacheNames 为 配置文件内 groupId
     */
    @ApiOperation("测试 @CachePut")
    @CachePut(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
    @GetMapping("/test2")
    public AjaxResult<String> test2(String key, String value) {
        return AjaxResult.success("操作成功", value);
    public R<String> test2(String key, String value) {
        return R.ok("操作成功", value);
    }
    /**
@@ -71,11 +69,10 @@
     * <p>
     * cacheNames 为 配置文件内 groupId
     */
    @ApiOperation("测试 @CacheEvict")
    @CacheEvict(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null")
    @GetMapping("/test3")
    public AjaxResult<String> test3(String key, String value) {
        return AjaxResult.success("操作成功", value);
    public R<String> test3(String key, String value) {
        return R.ok("操作成功", value);
    }
    /**
@@ -83,11 +80,10 @@
     * 手动设置过期时间10秒
     * 11秒后获取 判断是否相等
     */
    @ApiOperation("测试设置过期时间")
    @GetMapping("/test6")
    public AjaxResult<Boolean> test6(String key, String value) {
    public R<Boolean> test6(String key, String value) {
        RedisUtils.setCacheObject(key, value);
        boolean flag = RedisUtils.expire(key, 10, TimeUnit.SECONDS);
        boolean flag = RedisUtils.expire(key, Duration.ofSeconds(10));
        System.out.println("***********" + flag);
        try {
            Thread.sleep(11 * 1000);
@@ -95,7 +91,7 @@
            e.printStackTrace();
        }
        Object obj = RedisUtils.getCacheObject(key);
        return AjaxResult.success("操作成功", value.equals(obj));
        return R.ok(value.equals(obj));
    }
}