All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eworkcloud.guava.GuavaConfiguration Maven / Gradle / Ivy

There is a newer version: 1.8.6
Show newest version
package com.eworkcloud.guava;

import com.google.common.cache.CacheBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
@EnableConfigurationProperties(GuavaProperties.class)
public class GuavaConfiguration {

    @Resource
    private GuavaProperties guavaProperties;

    @Bean
    public GuavaCacheManager guavaCacheManager() {
        CacheBuilder cacheBuilder = CacheBuilder.newBuilder();

        if (guavaProperties.getMaximumSize() > 0) {
            cacheBuilder.maximumSize(guavaProperties.getMaximumSize());
        }
        if (guavaProperties.getExpireAfterAccess() > 0) {
            cacheBuilder.expireAfterAccess(guavaProperties.getExpireAfterAccess(), TimeUnit.SECONDS);
        }
        if (guavaProperties.getExpireAfterWrite() > 0) {
            cacheBuilder.expireAfterWrite(guavaProperties.getExpireAfterWrite(), TimeUnit.SECONDS);
        }
        if (guavaProperties.getRefreshAfterWrite() > 0) {
            cacheBuilder.refreshAfterWrite(guavaProperties.getRefreshAfterWrite(), TimeUnit.SECONDS);
        }
        if (guavaProperties.getInitialCapacity() > 0) {
            cacheBuilder.initialCapacity(guavaProperties.getInitialCapacity());
        }
        if (guavaProperties.getConcurrencyLevel() > 0) {
            cacheBuilder.concurrencyLevel(guavaProperties.getConcurrencyLevel());
        }

        GuavaCacheManager guavaCacheManager = new GuavaCacheManager();
        guavaCacheManager.setCacheBuilder(cacheBuilder);
        return guavaCacheManager;
    }

    @Bean
    public GuavaCacheTemplate guavaCacheTemplate() {
        return new GuavaCacheTemplate(guavaCacheManager());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy