CacheManagementController.java 3.7 KB
package com.infoloop.tianting.cache;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 缓存管理控制器
 * 提供缓存操作的REST端点
 */
@Slf4j
@RestController
@RequestMapping("/actuator/cache")
public class CacheManagementController {

    @Autowired
    @Qualifier("multiLevelCacheService")
    private CacheService cacheService;

    /**
     * 获取缓存统计信息
     */
    @GetMapping("/stats")
    public CacheStats getCacheStats() {
        return cacheService.getStats();
    }

    /**
     * 清除指定缓存
     */
    @DeleteMapping("/evict")
    public ResponseEntity<?> evictCache(@RequestParam String key) {
        try {
            cacheService.evict(key);
            log.info("缓存清除成功: {}", key);
            return ResponseEntity.ok("缓存已清除: " + key);
        } catch (Exception e) {
            log.error("缓存清除失败: key={}, error={}", key, e.getMessage(), e);
            return ResponseEntity.internalServerError().body("缓存清除失败: " + e.getMessage());
        }
    }

    /**
     * 根据前缀清除缓存
     */
    @DeleteMapping("/evict/prefix")
    public ResponseEntity<?> evictByPrefix(@RequestParam String prefix) {
        try {
            cacheService.evictByPrefix(prefix);
            log.info("前缀缓存清除成功: {}", prefix);
            return ResponseEntity.ok("前缀缓存已清除: " + prefix);
        } catch (Exception e) {
            log.error("前缀缓存清除失败: prefix={}, error={}", prefix, e.getMessage(), e);
            return ResponseEntity.internalServerError().body("前缀缓存清除失败: " + e.getMessage());
        }
    }

    /**
     * 清空所有缓存
     */
    @DeleteMapping("/clear")
    public ResponseEntity<?> clearCache() {
        try {
            cacheService.clear();
            log.info("所有缓存清空成功");
            return ResponseEntity.ok("所有缓存已清除");
        } catch (Exception e) {
            log.error("缓存清空失败: error={}", e.getMessage(), e);
            return ResponseEntity.internalServerError().body("缓存清空失败: " + e.getMessage());
        }
    }

    /**
     * 检查缓存是否存在
     */
    @GetMapping("/exists")
    public ResponseEntity<?> checkExists(@RequestParam String key) {
        try {
            boolean exists = cacheService.exists(key);
            return ResponseEntity.ok(String.format("缓存key '%s' %s", key, exists ? "存在" : "不存在"));
        } catch (Exception e) {
            log.error("缓存存在性检查失败: key={}, error={}", key, e.getMessage(), e);
            return ResponseEntity.internalServerError().body("缓存检查失败: " + e.getMessage());
        }
    }

    /**
     * 获取缓存过期时间
     */
    @GetMapping("/expire")
    public ResponseEntity<?> getExpire(@RequestParam String key) {
        try {
            long expire = cacheService.getExpire(key);
            String message;
            if (expire == -2) {
                message = "缓存key不存在";
            } else if (expire == -1) {
                message = "缓存key永久有效";
            } else {
                message = String.format("缓存key剩余时间: %d秒", expire);
            }
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            log.error("缓存过期时间获取失败: key={}, error={}", key, e.getMessage(), e);
            return ResponseEntity.internalServerError().body("缓存过期时间获取失败: " + e.getMessage());
        }
    }
}