package com.nokia.hb.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Slf4j
@ConfigurationProperties("session")
@Configuration
@Data
public class CacheConfig {
    /**
     * session过期时间（秒）
     */
    private Integer timeout;

    @Bean
    public Cache<String, Object> sessionCache() {
        return Caffeine.newBuilder()
                .expireAfterAccess(timeout, TimeUnit.SECONDS)
                .evictionListener((k, v, c) -> log.debug("sessionCache evictionListener -> c: {}, k: {}, v: {}", c, k, v))
                .removalListener((k, v, c) -> log.debug("sessionCache removalListener -> c: {}, k: {}, v: {}", c, k, v))
                .build();
    }
}
