package com.example.demo.config;

import java.util.concurrent.TimeUnit;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.benmanes.caffeine.cache.Caffeine;

/**
 * 缓存配置类
 * 使用 Caffeine 作为本地缓存实现，提高系统性能
 */
@Configuration
@EnableCaching
public class CacheConfig {

    /**
     * 缓存名称常量
     */
    public static final String USER_CACHE = "userCache";
    public static final String USER_POINTS_CACHE = "userPointsCache";
    public static final String USER_WORK_STATS_CACHE = "userWorkStatsCache";
    public static final String SYSTEM_CONFIG_CACHE = "systemConfigCache";

    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        
        // 默认缓存配置：最大1000条，5分钟过期
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(5, TimeUnit.MINUTES)
                .recordStats()); // 记录统计信息
        
        // 注册缓存名称
        cacheManager.setCacheNames(java.util.Arrays.asList(
                USER_CACHE,
                USER_POINTS_CACHE,
                USER_WORK_STATS_CACHE,
                SYSTEM_CONFIG_CACHE
        ));
        
        return cacheManager;
    }
}
