CacheConfiguration.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package org.dbsyncer.web.config;
  2. import com.github.benmanes.caffeine.cache.Caffeine;
  3. import com.github.benmanes.caffeine.cache.Ticker;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.cache.CacheManager;
  8. import org.springframework.cache.caffeine.CaffeineCache;
  9. import org.springframework.cache.interceptor.KeyGenerator;
  10. import org.springframework.cache.support.SimpleCacheManager;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import java.lang.reflect.Method;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.StringJoiner;
  18. import java.util.concurrent.TimeUnit;
  19. import java.util.stream.Collectors;
  20. /**
  21. * @author AE86
  22. * @version 1.0.0
  23. * @date 2020/04/23 11:30
  24. */
  25. @Configuration
  26. @ConfigurationProperties(prefix = "dbsyncer.web")
  27. public class CacheConfiguration {
  28. private final Logger logger = LoggerFactory.getLogger(getClass());
  29. private Map<String, CacheConfig> cache;
  30. @Bean
  31. public KeyGenerator cacheKeyGenerator() {
  32. return (target, method, params) -> {
  33. String className = method.getDeclaringClass().getSimpleName();
  34. String methodName = method.getName();
  35. String paramHash = String.valueOf(Arrays.toString(params).hashCode());
  36. String cacheKey = new StringJoiner("_").add(className).add(methodName).add(paramHash).toString();
  37. logger.debug("generate cache key : {}", cacheKey);
  38. return cacheKey;
  39. };
  40. }
  41. @Bean
  42. public Ticker ticker() {
  43. return Ticker.systemTicker();
  44. }
  45. @Bean
  46. public CacheManager cacheManager(Ticker ticker) {
  47. SimpleCacheManager manager = new SimpleCacheManager();
  48. if (cache != null) {
  49. List<CaffeineCache> caches = cache.entrySet()
  50. .stream()
  51. .map(entry -> buildCache(entry.getKey(), entry.getValue(), ticker))
  52. .collect(Collectors.toList());
  53. manager.setCaches(caches);
  54. }
  55. return manager;
  56. }
  57. private CaffeineCache buildCache(String key, CacheConfig config, Ticker ticker) {
  58. logger.info("Cache key {} specified timeout of {} seconds, max of {}", key, config.getTimeout(), config.getMax());
  59. final Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
  60. .expireAfterWrite(config.getTimeout(), TimeUnit.SECONDS)
  61. .maximumSize(config.getMax())
  62. .ticker(ticker);
  63. return new CaffeineCache(key, caffeineBuilder.build());
  64. }
  65. static class CacheConfig {
  66. private Integer timeout;
  67. private Integer max = 200;
  68. public Integer getTimeout() {
  69. return timeout;
  70. }
  71. public void setTimeout(Integer timeout) {
  72. this.timeout = timeout;
  73. }
  74. public Integer getMax() {
  75. return max;
  76. }
  77. public void setMax(Integer max) {
  78. this.max = max;
  79. }
  80. }
  81. public void setCache(Map<String, CacheConfig> cache) {
  82. this.cache = cache;
  83. }
  84. }