package com.guwan.backend.config; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.stats.CacheStats; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @Slf4j @RestController @RequestMapping("/api/cache") public class CacheMonitor { private final Cache userCache; private final Cache productCache; public CacheMonitor(Cache userCache, Cache productCache) { this.userCache = userCache; this.productCache = productCache; } @GetMapping("/stats") public Map getStats() { Map stats = new HashMap<>(); // 用户缓存统计 Map userStats = new HashMap<>(); userStats.put("stats", userCache.stats()); userStats.put("estimatedSize", userCache.estimatedSize()); userStats.put("asMap", userCache.asMap()); stats.put("userCache", userStats); // 产品缓存统计 Map productStats = new HashMap<>(); productStats.put("stats", productCache.stats()); productStats.put("estimatedSize", productCache.estimatedSize()); productStats.put("asMap", productCache.asMap()); stats.put("productCache", productStats); return stats; } @GetMapping("/details") public String getDetails() { StringBuilder details = new StringBuilder(); details.append("=== Cache Details ===\n"); // 用户缓存详情 CacheStats userStats = userCache.stats(); details.append("User Cache:\n"); details.append(" Hit count: ").append(userStats.hitCount()).append("\n"); details.append(" Miss count: ").append(userStats.missCount()).append("\n"); details.append(" Load success count: ").append(userStats.loadSuccessCount()).append("\n"); details.append(" Load failure count: ").append(userStats.loadFailureCount()).append("\n"); details.append(" Total load time: ").append(userStats.totalLoadTime()).append("\n"); details.append(" Eviction count: ").append(userStats.evictionCount()).append("\n"); details.append(" Estimated size: ").append(userCache.estimatedSize()).append("\n"); // 产品缓存详情 CacheStats productStats = productCache.stats(); details.append("\nProduct Cache:\n"); details.append(" Hit count: ").append(productStats.hitCount()).append("\n"); details.append(" Miss count: ").append(productStats.missCount()).append("\n"); details.append(" Load success count: ").append(productStats.loadSuccessCount()).append("\n"); details.append(" Load failure count: ").append(productStats.loadFailureCount()).append("\n"); details.append(" Total load time: ").append(productStats.totalLoadTime()).append("\n"); details.append(" Eviction count: ").append(productStats.evictionCount()).append("\n"); details.append(" Estimated size: ").append(productCache.estimatedSize()).append("\n"); return details.toString(); } // 每5分钟打印一次缓存统计信息 @Scheduled(fixedRate = 300000) public void logCacheStats() { log.info("=== Cache Stats ==="); log.info("User Cache: {}", userCache.stats()); log.info("Product Cache: {}", productCache.stats()); } }