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

com.makitoo.internal.CacheConfigurationProvider Maven / Gradle / Ivy

The newest version!
package com.makitoo.internal;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.makitoo.User;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import static com.makitoo.internal.PropertiesUtils.getIntProp;

/**
 * Created by nicolas on 24/01/17.
 */
public class CacheConfigurationProvider implements ConfigurationProvider {
    private final LoadingCache cache;

    public CacheConfigurationProvider(Properties properties, final ConfigurationProvider delegate) {
        int maxSize = getIntProp(properties, "makitoo.cache.size", 1000);
        int maxTime = getIntProp(properties, "makitoo.cache.time", 10 * 60 * 1000 /* 10 min */);

        cache = CacheBuilder.newBuilder()
                .maximumSize(maxSize)
                .expireAfterWrite(maxTime, TimeUnit.MILLISECONDS)
                .build(
                        new CacheLoader() {
                            public UserConfiguration load(User key) throws Exception {
                                return delegate.getUserConfiguration(key);
                            }
                        });
    }

    public UserConfiguration getUserConfiguration(User user) throws Exception {
        return cache.get(user);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy