package com.hb.proj.allconfig; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import io.micrometer.common.util.StringUtils; @Configuration public class CacheConfig { public static String TOKEN_HEADER_NAME="token"; private static Cache tokenCache=null; public CacheConfig(@Value("${cache.token.expire}") long expireVal, @Value("${token.header.name}") String tokenHeaderName) { //System.out.println(expireVal); tokenCache = Caffeine.newBuilder() .expireAfterAccess(expireVal, TimeUnit.MINUTES) .initialCapacity(20) .build(); if(StringUtils.isNotBlank(tokenHeaderName)) { TOKEN_HEADER_NAME=tokenHeaderName; } } public static void put(AccessToken token) { if(token==null||StringUtils.isBlank(token.getTokenId())) { return; } tokenCache.put(token.getTokenId(), token); } public static AccessToken get(String tokenId) { return tokenCache.getIfPresent(tokenId); } }