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:com.salesforce.pixelcaptcha.storage.impl.PixelCaptchaSolutionStore.java

public PixelCaptchaSolutionStore(int size, int timeoutInSecs) {

    if (size <= 0)
        throw new IllegalArgumentException("The cache size cannot be less than " + MIN_SIZE);
    if (timeoutInSecs < MIN_TIMEOUT || timeoutInSecs > MAX_TIMEOUT)
        throw new IllegalArgumentException("The cache entry timeout cannot be less than " + MIN_TIMEOUT
                + " or greater than " + MAX_TIMEOUT);

    solutionStore = CacheBuilder.newBuilder().maximumSize(size)
            .expireAfterAccess(timeoutInSecs, TimeUnit.SECONDS)
            .build(new CacheLoader<String, Optional<CaptchaSolution>>() {
                @Override/*  w  w w.j a  v  a  2s  .  c om*/
                public Optional<CaptchaSolution> load(String key) {
                    // Since there is no local store load data from, the function always returns Optional.absent()
                    // Returning null will cause an exception as per CacheLoader design
                    return Optional.absent();
                }
            });
}

From source file:org.opendaylight.sloth.cache.SlothPermissionCache.java

public SlothPermissionCache(DataBroker dataBroker) {
    super(dataBroker);
    registerListener(LogicalDatastoreType.CONFIGURATION, SLOTH_PERMISSION_ID);
    permissionCache = CacheBuilder.newBuilder().maximumSize(MAX_PERMISSION_CACHE).build();
    LOG.info("initialize SlothPermissionCache");
}

From source file:org.jeo.data.CachedRepository.java

public CachedRepository(DataRepository reg, final int cacheSize) {
    this.reg = reg;
    objCache = CacheBuilder.newBuilder().maximumSize(cacheSize)
            .removalListener(new RemovalListener<Pair<String, Class>, Object>() {
                @Override//from ww  w  . j a  v  a 2  s  . c  o  m
                public void onRemoval(RemovalNotification<Pair<String, Class>, Object> n) {
                    Object obj = n.getValue();
                    if (obj instanceof Disposable) {
                        ((Disposable) obj).close();
                    }
                }
            }).build(new CacheLoader<Pair<String, Class>, Object>() {
                @Override
                public Object load(Pair<String, Class> key) throws Exception {
                    Object obj = CachedRepository.this.reg.get(key.first(), key.second());
                    if (obj instanceof Workspace) {
                        return new CachedWorkspace((Workspace) obj, cacheSize);
                    }
                    return obj;
                }
            });
}

From source file:com.github.pagehelper.cache.GuavaCache.java

public GuavaCache(Properties properties, String prefix) {
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    String maximumSize = properties.getProperty(prefix + ".maximumSize");
    if (StringUtil.isNotEmpty(maximumSize)) {
        cacheBuilder.maximumSize(Long.parseLong(maximumSize));
    } else {// www . j  av  a2s . c o m
        cacheBuilder.maximumSize(1000);
    }
    String expireAfterAccess = properties.getProperty(prefix + ".expireAfterAccess");
    if (StringUtil.isNotEmpty(expireAfterAccess)) {
        cacheBuilder.expireAfterAccess(Long.parseLong(expireAfterAccess), TimeUnit.MILLISECONDS);
    }
    String expireAfterWrite = properties.getProperty(prefix + ".expireAfterWrite");
    if (StringUtil.isNotEmpty(expireAfterWrite)) {
        cacheBuilder.expireAfterWrite(Long.parseLong(expireAfterWrite), TimeUnit.MILLISECONDS);
    }
    String initialCapacity = properties.getProperty(prefix + ".initialCapacity");
    if (StringUtil.isNotEmpty(initialCapacity)) {
        cacheBuilder.initialCapacity(Integer.parseInt(initialCapacity));
    }
    CACHE = cacheBuilder.build();
}

From source file:gobblin.runtime.locks.AbstractJobLockFactoryManager.java

public AbstractJobLockFactoryManager() {
    this(CacheBuilder.newBuilder().<Config, F>build());
}

From source file:de.brands4friends.daleq.core.internal.types.CachingTableTypeRepository.java

public CachingTableTypeRepository() {
    this.resolvers = new CopyOnWriteArrayList<TableTypeResolver>(
            Lists.newArrayList(new ClassBasedTableTypeResolver()));
    this.cache = CacheBuilder.newBuilder().build();
}

From source file:io.fluo.core.impl.VisibilityCache.java

VisibilityCache() {
    visCache = CacheBuilder.newBuilder().expireAfterAccess(TxInfoCache.CACHE_TIMEOUT_MIN, TimeUnit.MINUTES)
            .maximumWeight(10000000).weigher(new VisWeigher()).concurrencyLevel(10).build();
}

From source file:co.cask.cdap.metrics.process.TimeSeriesMetricsProcessor.java

@Inject
public TimeSeriesMetricsProcessor(final MetricsTableFactory tableFactory) {
    timeSeriesTables = CacheBuilder.newBuilder().build(new CacheLoader<String, TimeSeriesTable>() {
        @Override//from w  w w.ja v  a  2s  .co m
        public TimeSeriesTable load(String key) throws Exception {
            return tableFactory.createTimeSeries(key, 1);
        }
    });
}

From source file:org.pentaho.caching.ri.impl.GuavaCacheManager.java

@Override
public <K, V, C extends Configuration<K, V>> Cache<K, V> newCache(final String cacheName,
        final C configuration) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();

    if (configuration instanceof CompleteConfiguration) {
        configureCacheBuilder((CompleteConfiguration) configuration, cacheBuilder);
    }//  ww w  .  java2s  . c  o  m

    return new WrappedCache<K, V>(cacheBuilder.<K, V>build()) {
        @Override
        public String getName() {
            return cacheName;
        }

        @Override
        public CacheManager getCacheManager() {
            return GuavaCacheManager.this;
        }

        @Override
        public void close() {
            if (!isClosed()) {
                super.close();
                destroyCache(cacheName);
            }
        }

        @Override
        public <T extends Configuration<K, V>> T getConfiguration(Class<T> clazz) {
            return Constants.unwrap(configuration, clazz);
        }
    };
}

From source file:com.isotrol.impe3.web20.impl.ResourceComponent.java

/** Default constructor. */
public ResourceComponent() {
    final CacheLoader<Long, String> computer = new CacheLoader<Long, String>() {
        public String load(Long from) {
            return findById(ResourceEntity.class, from).getResourceId();
        }//w  w w . j av  a2  s  . co  m
    };
    cache = CacheBuilder.newBuilder().softValues().build(computer);
}