com.dxy.library.cache.memory.guava.CacheGuava Maven / Gradle / Ivy
package com.dxy.library.cache.memory.guava;
import com.dxy.library.cache.memory.IMemory;
import com.dxy.library.util.common.config.ConfigUtils;
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.RemovalListener;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.util.concurrent.TimeUnit;
/**
* 基于guava的内存缓存器
* @author duanxinyuan
* 2018/8/8 19:48
*/
@Slf4j
public class CacheGuava implements IMemory {
private Cache> cache;
public CacheGuava() {
cache = CacheBuilder.newBuilder()
.initialCapacity(NumberUtils.toInt(ConfigUtils.getConfig("cache.memory.key.capacity.initial"), 1000))
.maximumSize(NumberUtils.toInt(ConfigUtils.getConfig("cache.memory.key.capacity.max"), 5_0000))
.expireAfterWrite(NumberUtils.toInt(ConfigUtils.getConfig("cache.memory.expire.seconds.after.write"), 5), TimeUnit.MINUTES)
.expireAfterAccess(NumberUtils.toInt(ConfigUtils.getConfig("cache.memory.expire.seconds.after.access"), 5), TimeUnit.MINUTES)
.refreshAfterWrite(NumberUtils.toInt(ConfigUtils.getConfig("cache.memory.refresh.seconds.after.write"), 5), TimeUnit.MINUTES)
.recordStats()//开启Guava Cache的统计功能
.removalListener((RemovalListener>) removalNotification -> {
if (log.isDebugEnabled()) {
log.debug("guava cache removal success, key: {}, value: {}", removalNotification.getKey(), removalNotification.getValue());
}
})
.build(new CacheLoader>() {
@Override
public Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy