package com.qmth.exam.reserve.controller;

import com.qmth.exam.reserve.cache.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class IndexController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedisClient redisClient;

    @GetMapping(value = "/")
    public String index() {
        return String.valueOf(System.currentTimeMillis());
    }

    @GetMapping(value = "/api/cache/clear")
    public String clearCache() {
        // 清理缓存接口（临时）
        RedisTemplate<String, Object> redisTemplate = redisClient.getRedisTemplate();
        Set<String> keys = redisTemplate.keys("$cache:*");
        if (keys != null && !keys.isEmpty()) {
            keys.forEach(cacheKey -> {
                log.warn("DELETE cacheKey:{}", cacheKey);
                redisClient.delete(cacheKey);
            });
        }
        return "ok";
    }

}
