org.greeneyed.summer.config.CacheConfiguration Maven / Gradle / Ivy
package org.greeneyed.summer.config;
/*
* #%L
* Summer
* %%
* Copyright (C) 2018 GreenEyed (Daniel Lopez)
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Ticker;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Configuration
@ConfigurationProperties(prefix = "summer.caching")
@Data
@Slf4j
public class CacheConfiguration {
private Map specs;
@Data
public static class CacheSpec {
private Integer timeout;
private Integer max = 200;
}
@Bean
public CacheManager cacheManager(Ticker ticker) {
SimpleCacheManager manager = new SimpleCacheManager();
if (specs != null) {
//@formatter:off
List caches =
specs.entrySet().stream()
.map(entry -> buildCache(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
//@formatter:on
manager.setCaches(caches);
}
return manager;
}
private CaffeineCache buildCache(String name, CacheSpec cacheSpec) {
log.info("Cache {} specified timeout of {} min, max of {}", name, cacheSpec.getTimeout(), cacheSpec.getMax());
//@formatter:off
final Caffeine
© 2015 - 2024 Weber Informatics LLC | Privacy Policy