Example usage for com.google.common.cache CacheBuilder newBuilder

List of usage examples for com.google.common.cache CacheBuilder newBuilder

Introduction

In this page you can find the example usage for com.google.common.cache CacheBuilder newBuilder.

Prototype

public static CacheBuilder<Object, Object> newBuilder() 

Source Link

Document

Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.

Usage

From source file:org.sentilo.common.cache.impl.LRUCacheImpl.java

public LRUCacheImpl(final int cacheSize) {
    cache = CacheBuilder.newBuilder().maximumSize(cacheSize).build();
}

From source file:com.github.cherimojava.orchidae.util.UserUtil.java

@SuppressWarnings("unchecked")
public UserUtil(int cacheSize, int concurrencyLevel) {
    registry = CacheBuilder.newBuilder().maximumSize(cacheSize).concurrencyLevel(concurrencyLevel)
            .removalListener(new RemovalListener() {
                @Override//  ww  w  . j  av  a2 s. com
                public void onRemoval(RemovalNotification notification) {
                    //save user before exit
                    if (notification.getValue() != null) {
                        ((Entity) notification.getValue()).save();
                    }
                }
            }).build(new CacheLoader<String, User>() {
                @Override
                public User load(String key) throws Exception {
                    return factory.load(User.class, key);
                }
            });
    this.cacheSize = cacheSize;
}

From source file:com.torodb.torod.impl.memory.MemoryTorodServer.java

@Inject
public MemoryTorodServer(ThreadFactory threadFactory) {
    super(threadFactory);

    openConnections = CacheBuilder.newBuilder().weakValues().removalListener(this::onConnectionInvalidated)
            .build();/*w w  w . j av  a  2 s  .  c o m*/
}

From source file:co.cask.common.internal.io.ASMDatumWriterFactory.java

@Inject
public ASMDatumWriterFactory(FieldAccessorFactory fieldAccessorFactory) {
    this.fieldAccessorFactory = fieldAccessorFactory;
    this.datumWriterClasses = CacheBuilder.newBuilder().build(new ASMCacheLoader());
}

From source file:com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository.java

private ConcurrentMap<Long, ActiveTrace> createCache(int maxActiveTraceSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);/*from  w  w  w . ja v  a2  s.com*/
    cacheBuilder.initialCapacity(maxActiveTraceSize);
    cacheBuilder.maximumSize(maxActiveTraceSize);
    // OOM defense
    cacheBuilder.weakValues();

    final Cache<Long, ActiveTrace> localCache = cacheBuilder.build();
    return localCache.asMap();
}

From source file:com.facebook.buck.util.i18n.NumberFormatter.java

/**
 * Given a function which creates {@link NumberFormat} objects for a {@link Locale}, returns a
 * wrapper providing thread-safe access to the formatter for that locale.
 *//*from  w w  w . j ava  2s. c  o m*/
public NumberFormatter(Function<Locale, NumberFormat> numberFormatCreator) {
    numberFormatCache = CacheBuilder.newBuilder().maximumSize(1000)
            .build(new CacheLoader<NumberFormatterCacheKey, NumberFormat>() {
                @Override
                public NumberFormat load(NumberFormatterCacheKey key) {
                    return numberFormatCreator.apply(key.getLocale());
                }
            });
}

From source file:com.facebook.buck.apple.xcode.xcodeproj.XCVersionGroup.java

public XCVersionGroup(String name, String path, SourceTree sourceTree) {
    super(name, path, sourceTree);
    children = Lists.newArrayList();//from   w w  w . j a va  2  s.c  om

    fileReferencesBySourceTreePath = CacheBuilder.newBuilder()
            .build(new CacheLoader<SourceTreePath, PBXFileReference>() {
                @Override
                public PBXFileReference load(SourceTreePath key) throws Exception {
                    PBXFileReference ref = new PBXFileReference(key.getPath().getFileName().toString(),
                            key.getPath().toString(), key.getSourceTree());
                    children.add(ref);
                    return ref;
                }
            });

    currentVersion = Optional.absent();
}

From source file:remotedrive.client.googledrive.CachedGoogleDriveClient.java

/**
 * Initialize a new instance Cached google drive client.
 *//*from   w  ww .  j a va2  s.c  o  m*/
public CachedGoogleDriveClient() {
    this.childrenCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, TimeUnit.MINUTES)
            .build();
    this.fileCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, TimeUnit.MINUTES).build();
    this.contentCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES)
            .build();
}

From source file:io.pravega.shared.metrics.DynamicLoggerImpl.java

public DynamicLoggerImpl(MetricsConfig metricsConfig, MetricRegistry metrics, StatsLogger statsLogger) {
    Preconditions.checkNotNull(metricsConfig, "metricsConfig");
    Preconditions.checkNotNull(metrics, "metrics");
    Preconditions.checkNotNull(statsLogger, "statsLogger");
    this.metrics = metrics;
    this.underlying = statsLogger;
    this.cacheSize = metricsConfig.getDynamicCacheSize();

    countersCache = CacheBuilder.newBuilder().maximumSize(cacheSize)
            .removalListener(new RemovalListener<String, Counter>() {
                @Override/*  w w  w  .  j  av a 2 s. c  o m*/
                public void onRemoval(RemovalNotification<String, Counter> removal) {
                    Counter counter = removal.getValue();
                    if (removal.getCause() != RemovalCause.REPLACED) {
                        Exceptions.checkNotNullOrEmpty(counter.getName(), "counter");
                        metrics.remove(counter.getName());
                        log.debug("Removed Counter: {}.", counter.getName());
                    }
                }
            }).build();

    gaugesCache = CacheBuilder.newBuilder().maximumSize(cacheSize)
            .removalListener(new RemovalListener<String, Gauge>() {
                @Override
                public void onRemoval(RemovalNotification<String, Gauge> removal) {
                    Gauge gauge = removal.getValue();
                    if (removal.getCause() != RemovalCause.REPLACED) {
                        Exceptions.checkNotNullOrEmpty(gauge.getName(), "gauge");
                        metrics.remove(gauge.getName());
                        log.debug("Removed Gauge: {}.", gauge.getName());
                    }
                }
            }).build();

    metersCache = CacheBuilder.newBuilder().maximumSize(cacheSize)
            .removalListener(new RemovalListener<String, Meter>() {
                @Override
                public void onRemoval(RemovalNotification<String, Meter> removal) {
                    Meter meter = removal.getValue();
                    if (removal.getCause() != RemovalCause.REPLACED) {
                        Exceptions.checkNotNullOrEmpty(meter.getName(), "meter");
                        metrics.remove(meter.getName());
                        log.debug("Removed Meter: {}.", meter.getName());
                    }
                }
            }).build();
}

From source file:org.hypoport.springGuavaCacheAdapter.SpringGuavaCacheAdapter.java

private void createGuavaCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (maximumSize != null) {
        builder.maximumSize(maximumSize);
    }//from  w w  w  .  jav a 2  s . c  om
    if (expireAfterAccessInSeconds != null) {
        builder.expireAfterAccess(expireAfterAccessInSeconds, SECONDS);
    }
    if (expireAfterWriteInSeconds != null) {
        builder.expireAfterWrite(expireAfterWriteInSeconds, SECONDS);
    }
    cache = builder.build();
}