package com.ningdatech.carapi.open.controller;

import com.ningdatech.cache.repository.CachePlusOps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/open/api/redis/")
@Api(value = "archives", tags = "对外联调接口")
@RequiredArgsConstructor
public class OpenApiRedisController {

    private final CachePlusOps cachePlusOps;

    @ApiOperation(value = "redis remove", notes = "redis")
    @GetMapping("/remove")
    public Boolean remove() {
        // 模糊匹配所有以keyword:开头的所有key值
        Set<String> keys = cachePlusOps.keys("cm:"+"*");

        log.info("cm下的key数量:{}",keys.size());

        for(String key : keys){
            cachePlusOps.del(key);
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        Set<String> keysAfter = cachePlusOps.keys("cm:"+"*");

        log.info("删除后cm下的key数量:{}",keysAfter.size());

        return Boolean.TRUE;
    }
}
