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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
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 演示案例
 *
 * @author Lion Li
 */
// 类级别 缓å˜ç»Ÿä¸€é…ç½®
//@CacheConfig(cacheNames = "redissonCacheMap")
@Api(value = "spring-cache 演示案例", tags = {"spring-cache 演示案例"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/demo/cache")
public class RedisCacheController {
    /**
     * 测试 @Cacheable
     * <p>
     * 表示这个方法有了缓å˜çš„功能,方法的返回值会被缓å˜ä¸‹æ¥
     * 下一次调用该方法å‰,会去检查是å¦ç¼“å˜ä¸å·²ç»æœ‰å€¼
     * 如果有就直接返回,ä¸è°ƒç”¨æ–¹æ³•
     * 如果没有,就调用方法,ç„¶åŽæŠŠç»“æžœç¼“å˜èµ·æ¥
     * 这个注解「一般用在查询方法上ã€
     * <p>
     * é‡ç‚¹è¯´æ˜Ž: ç¼“å˜æ³¨è§£ä¸¥è°¨ä¸Žå…¶ä»–ç›é€‰æ•°æ®åŠŸèƒ½ä¸€èµ·ä½¿ç”¨
     * 例如: æ•°æ®æƒé™æ³¨è§£ ä¼šé€ æˆ ç¼“å˜å‡»ç©¿ 与 æ•°æ®ä¸ä¸€è‡´é—®é¢˜
     * <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);
    }
    /**
     * 测试 @CachePut
     * <p>
     * 加了@CachePut注解的方法,会把方法的返回值put到缓å˜é‡Œé¢ç¼“å˜èµ·æ¥,供其它地方使用
     * 它「通常用在新增方法上ã€
     * <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);
    }
    /**
     * 测试 @CacheEvict
     * <p>
     * 使用了CacheEvict注解的方法,会清空指定缓å˜
     * 「一般用在更新或者删除的方法上ã€
     * <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);
    }
    /**
     * 测试设置过期时间
     * 手动设置过期时间10秒
     * 11ç§’åŽèŽ·å– åˆ¤æ–æ˜¯å¦ç›¸ç‰
     */
    @ApiOperation("测试设置过期时间")
    @GetMapping("/test6")
    public AjaxResult<Boolean> 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));
    }
}