CacheConfig.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.hb.proj.allconfig;
  2. import java.util.concurrent.TimeUnit;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Configuration;
  5. import com.github.benmanes.caffeine.cache.Cache;
  6. import com.github.benmanes.caffeine.cache.Caffeine;
  7. import io.micrometer.common.util.StringUtils;
  8. @Configuration
  9. public class CacheConfig {
  10. public static String TOKEN_HEADER_NAME="token";
  11. private static Cache<String,AccessToken> tokenCache=null;
  12. public CacheConfig(@Value("${cache.token.expire}") long expireVal, @Value("${token.header.name}") String tokenHeaderName) {
  13. //System.out.println(expireVal);
  14. tokenCache = Caffeine.newBuilder()
  15. .expireAfterAccess(expireVal, TimeUnit.MINUTES)
  16. .initialCapacity(20)
  17. .build();
  18. if(StringUtils.isNotBlank(tokenHeaderName)) {
  19. TOKEN_HEADER_NAME=tokenHeaderName;
  20. }
  21. }
  22. public static void put(AccessToken token) {
  23. if(token==null||StringUtils.isBlank(token.getTokenId())) {
  24. return;
  25. }
  26. tokenCache.put(token.getTokenId(), token);
  27. }
  28. public static AccessToken get(String tokenId) {
  29. return tokenCache.getIfPresent(tokenId);
  30. }
  31. }