List of usage examples for com.google.common.cache CacheBuilder build
public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(CacheLoader<? super K1, V1> loader)
From source file:org.opennms.netmgt.eventd.AbstractEventUtil.java
public AbstractEventUtil(MetricRegistry registry) { // Build the cache, and enable statistics collection if we've been given a metric registry final long maximumCacheSize = Long .parseLong(System.getProperty("org.opennms.eventd.eventTemplateCacheSize", "1000")); final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder().maximumSize(maximumCacheSize); if (registry != null) { cacheBuilder.recordStats();// w w w. java 2 s .com } eventTemplateCache = cacheBuilder.build(new CacheLoader<String, EventTemplate>() { public EventTemplate load(String key) throws Exception { return new EventTemplate(key, AbstractEventUtil.this); } }); if (registry != null) { // Expose the cache statistics via a series of gauges registry.register(MetricRegistry.name("eventutil.cache.capacity"), new Gauge<Long>() { @Override public Long getValue() { return maximumCacheSize; } }); registry.register(MetricRegistry.name("eventutil.cache.size"), new Gauge<Long>() { @Override public Long getValue() { return eventTemplateCache.size(); } }); registry.register(MetricRegistry.name("eventutil.cache.evictioncount"), new Gauge<Long>() { @Override public Long getValue() { return eventTemplateCache.stats().evictionCount(); } }); registry.register(MetricRegistry.name("eventutil.cache.avgloadpenalty"), new Gauge<Double>() { @Override public Double getValue() { return eventTemplateCache.stats().averageLoadPenalty(); } }); } }
From source file:tv.dyndns.kishibe.qmaclone.server.database.CachedDatabase.java
private <K, V> LoadingCache<K, V> build(String name, CacheLoader<K, V> loader) { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().softValues().expireAfterAccess(1, TimeUnit.HOURS);//from www . j a v a 2 s .c o m if (ENABLE_CACHE_STATS) { builder.recordStats(); } LoadingCache<K, V> cache = builder.build(loader); caches.put(name, cache); return cache; }
From source file:com.streamsets.pipeline.stage.processor.kv.LookupProcessor.java
private LoadingCache<String, Optional<String>> buildCache() { CacheBuilder build = CacheBuilder.newBuilder(); if (!conf.cache.enabled) { return build.maximumSize(0).build(new StoreCacheLoader(store)); }//from ww w . jav a 2 s . c om // CacheBuilder doesn't support specifying type thus suffers from erasure, so // we build it with this if / else logic. if (conf.cache.maxSize == -1) { conf.cache.maxSize = Long.MAX_VALUE; } build.maximumSize(conf.cache.maxSize); if (conf.cache.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_ACCESS) { build.expireAfterAccess(conf.cache.expirationTime, conf.cache.timeUnit); } else if (conf.cache.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_WRITE) { build.expireAfterWrite(conf.cache.expirationTime, conf.cache.timeUnit); } else { throw new IllegalArgumentException( Utils.format("Unrecognized EvictionPolicyType: '{}'", conf.cache.evictionPolicyType)); } return build.build(new StoreCacheLoader(store)); }
From source file:com.streamsets.pipeline.stage.processor.kv.redis.RedisLookupProcessor.java
@SuppressWarnings("unchecked") private LoadingCache<Pair<String, DataType>, LookupValue> buildCache() { CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); if (!conf.cache.enabled) { return cacheBuilder.maximumSize(0).build(store); }//w ww. java2 s . co m if (conf.cache.maxSize == -1) { conf.cache.maxSize = Long.MAX_VALUE; } // CacheBuilder doesn't support specifying type thus suffers from erasure, so // we build it with this if / else logic. if (conf.cache.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_ACCESS) { cacheBuilder.maximumSize(conf.cache.maxSize).expireAfterAccess(conf.cache.expirationTime, conf.cache.timeUnit); } else if (conf.cache.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_WRITE) { cacheBuilder.maximumSize(conf.cache.maxSize).expireAfterWrite(conf.cache.expirationTime, conf.cache.timeUnit); } else { throw new IllegalArgumentException( Utils.format("Unrecognized EvictionPolicyType: '{}'", conf.cache.evictionPolicyType)); } return cacheBuilder.build(store); }
From source file:fr.inria.eventcloud.overlay.SemanticCanOverlay.java
/** * Constructs a new overlay with the specified datastore instances. * //from ww w . jav a 2s . co m * @param subscriptionsDatastore * the datastore instance that is used to store subscriptions. * * @param miscDatastore * the datastore instance that is used to store miscellaneous * data (publications, historical data, etc.). * * @param colanderDatastore * the datastore instance that is used to filter intermediate * results for SPARQL requests from a {@link SparqlColander}. */ public SemanticCanOverlay(final TransactionalTdbDatastore subscriptionsDatastore, TransactionalTdbDatastore miscDatastore, TransactionalTdbDatastore colanderDatastore) { super(new SemanticRequestResponseManager(colanderDatastore)); this.isBootstrappingPeer = false; this.miscDatastore = miscDatastore; this.subscriptionsDatastore = subscriptionsDatastore; this.miscDatastore.open(); this.subscriptionsDatastore.open(); CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder() .concurrencyLevel(P2PStructuredProperties.MAO_LIMIT_PEERS.getValue()).softValues() .maximumSize(EventCloudProperties.PEER_STUBS_CACHE_MAXIMUM_SIZE.getValue()); if (EventCloudProperties.RECORD_STATS_PEER_STUBS_CACHE.getValue()) { cacheBuilder.recordStats(); } this.peerStubsCache = cacheBuilder.build(new CacheLoader<String, SemanticPeer>() { @Override public SemanticPeer load(String peerUrl) throws Exception { return PAActiveObject.lookupActive(SemanticPeer.class, peerUrl); } }); cacheBuilder = CacheBuilder.newBuilder() .concurrencyLevel(P2PStructuredProperties.MAO_LIMIT_PEERS.getValue()).softValues() .maximumSize(EventCloudProperties.SUBSCRIPTIONS_CACHE_MAXIMUM_SIZE.getValue()); if (EventCloudProperties.RECORD_STATS_SUBSCRIPTIONS_CACHE.getValue()) { cacheBuilder.recordStats(); } this.subscriptionsCache = cacheBuilder.build(new CacheLoader<SubscriptionId, Subscription>() { @Override public Subscription load(SubscriptionId key) throws SubscriptionNotFoundException { Subscription subscription = Subscription.parseFrom(subscriptionsDatastore, key); if (subscription == null) { throw new SubscriptionNotFoundException(key); } return subscription; } }); this.subscriberConnectionFailures = CacheBuilder.newBuilder().softValues().build(); if (EventCloudProperties.isSbce2PubSubAlgorithmUsed() || EventCloudProperties.isSbce3PubSubAlgorithmUsed()) { this.ephemeralSubscriptionsGarbageColletor = Executors.newSingleThreadScheduledExecutor( new ThreadFactoryBuilder().setNameFormat("EphemeralSubscriptionsGC").build()); this.ephemeralSubscriptionsGarbageColletor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { SemanticCanOverlay.this.removeOutdatedEphemeralSubscriptions(); } catch (Throwable t) { t.printStackTrace(); } } }, EventCloudProperties.EPHEMERAL_SUBSCRIPTIONS_GC_TIMEOUT.getValue(), EventCloudProperties.EPHEMERAL_SUBSCRIPTIONS_GC_TIMEOUT.getValue(), TimeUnit.MILLISECONDS); } else { this.ephemeralSubscriptionsGarbageColletor = null; } this.publishSubscribeOperationsDelayer = new PublishSubscribeDelayer(this); if (EventCloudProperties.EXPOSE_JMX_STATISTICS.getValue()) { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); try { mbs.registerMBean(new fr.inria.eventcloud.jmx.SemanticPeerMBeanImpl(this), new ObjectName("fr.inria.eventcloud:type=SemanticPeer,id=" + super.id)); } catch (InstanceAlreadyExistsException e) { e.printStackTrace(); } catch (MBeanRegistrationException e) { e.printStackTrace(); } catch (NotCompliantMBeanException e) { e.printStackTrace(); } catch (MalformedObjectNameException e) { e.printStackTrace(); } } }
From source file:org.apache.kylin.dict.CachedTreeMap.java
private CachedTreeMap(int maxCount, Class<K> keyClazz, Class<V> valueClazz, String basePath, boolean immutable, int maxVersions, long versionTTL) throws IOException { super();/*from w w w. ja v a2s.c o m*/ this.keyClazz = keyClazz; this.valueClazz = valueClazz; this.immutable = immutable; this.keepAppend = true; this.maxVersions = maxVersions; this.versionTTL = versionTTL; this.conf = new Configuration(); if (basePath.endsWith("/")) { basePath = basePath.substring(0, basePath.length() - 1); } this.baseDir = new Path(basePath); this.fs = FileSystem.get(baseDir.toUri(), conf); if (!fs.exists(baseDir)) { fs.mkdirs(baseDir); } this.versionDir = getLatestVersion(conf, fs, baseDir); this.workingDir = new Path(baseDir, "working"); if (!this.immutable) { // For mutable map, copy all data into working dir and work on it, avoiding suddenly server crash made data corrupt if (fs.exists(workingDir)) { fs.delete(workingDir, true); } FileUtil.copy(fs, versionDir, fs, workingDir, false, true, conf); } CacheBuilder builder = CacheBuilder.newBuilder().removalListener(new RemovalListener<K, V>() { @Override public void onRemoval(RemovalNotification<K, V> notification) { logger.info(String.format("Evict cache key %s(%d) with value %s caused by %s, size %d/%d ", notification.getKey(), notification.getKey().hashCode(), notification.getValue(), notification.getCause(), size(), valueCache.size())); switch (notification.getCause()) { case SIZE: writeValue(notification.getKey(), notification.getValue()); break; case EXPLICIT: deleteValue(notification.getKey()); break; default: } } }); if (this.immutable) { // For immutable values, load all values as much as possible, and evict by soft reference to free memory when gc builder.softValues(); } else { builder.maximumSize(maxCount); } this.valueCache = builder.build(new CacheLoader<K, V>() { @Override public V load(K key) throws Exception { V value = readValue(key); logger.info(String.format("Load cache by key %s(%d) with value %s", key, key.hashCode(), value)); return value; } }); }
From source file:com.addthis.hydra.data.tree.ReadTree.java
public ReadTree(File root, boolean metrics) throws Exception { this.metrics = metrics; if (!root.isDirectory()) { throw new IOException("Unable to open root directory '" + root + "'"); }// w ww. j a v a 2 s .c o m this.root = root; this.advanced = TreeConfig.readFromDataDirectory(root.toPath()); source = initSource(); try { CacheBuilder<? super CacheKey, ? super ReadTreeNode> cacheBuilder = CacheBuilder.newBuilder(); if (nodeCacheWeight != 0) { // limit by weight cacheBuilder = cacheBuilder.maximumWeight((long) (nodeCacheWeight * advanced.cacheWeight)) .weigher((key, value) -> { /* A lean node goes from 24 to 24 + its string name and + cacheKey. the 24 becomes a small percentage. Dangerous, fat nodes typically have lots of serialized strings in their value payload. The inflation ratio there is actually probably less than for lean nodes since the various pointers for the string objects may not be nearly as large as the strings themselves. Therefore, holding them to the lean node's expansion standard is probably conservative enough. */ return value.getWeight(); }); } else { // Limit by the number of nodes cacheBuilder = cacheBuilder.maximumSize((long) (nodeCacheSize * advanced.cacheWeight)); } loadingNodeCache = cacheBuilder.build(new CacheLoader<CacheKey, ReadTreeNode>() { @Override public ReadTreeNode load(CacheKey key) throws Exception { ReadTreeNode node = sourceGet(key.dbkey()); if (node != null) { node.init(ReadTree.this, key.name); return node; } else { return MISSING; } } }); rootNode = getNode(1, "root"); if (rootNode == null) { throw new IllegalStateException("missing root in readonly tree"); } } catch (Exception e) { source.close(); throw e; } }
From source file:com.streamsets.pipeline.stage.processor.lookup.ForceLookupProcessor.java
@SuppressWarnings("unchecked") private Cache<String, Optional<List<Map<String, Field>>>> buildCache() { CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); if (!conf.cacheConfig.enabled) { return (conf.lookupMode == QUERY) ? cacheBuilder.maximumSize(0).build(new ForceLookupLoader(this)) : cacheBuilder.maximumSize(0).build(); }/* w w w .j a va 2 s. co m*/ if (conf.cacheConfig.maxSize == -1) { conf.cacheConfig.maxSize = Long.MAX_VALUE; } if (LOG.isDebugEnabled()) { cacheBuilder.recordStats(); } // CacheBuilder doesn't support specifying type thus suffers from erasure, so // we build it with this if / else logic. if (conf.cacheConfig.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_ACCESS) { cacheBuilder.maximumSize(conf.cacheConfig.maxSize).expireAfterAccess(conf.cacheConfig.expirationTime, conf.cacheConfig.timeUnit); } else if (conf.cacheConfig.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_WRITE) { cacheBuilder.maximumSize(conf.cacheConfig.maxSize).expireAfterWrite(conf.cacheConfig.expirationTime, conf.cacheConfig.timeUnit); } else { throw new IllegalArgumentException( Utils.format("Unrecognized EvictionPolicyType: '{}'", conf.cacheConfig.evictionPolicyType)); } return (conf.lookupMode == QUERY) ? cacheBuilder.build(new ForceLookupLoader(this)) : cacheBuilder.build(); }
From source file:com.github.benmanes.caffeine.cache.testing.GuavaLocalCache.java
/** Returns a Guava-backed cache. */ @SuppressWarnings("CheckReturnValue") public static <K, V> Cache<K, V> newGuavaCache(CacheContext context) { checkState(!context.isAsync(), "Guava caches are synchronous only"); CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (context.initialCapacity != InitialCapacity.DEFAULT) { builder.initialCapacity(context.initialCapacity.size()); }//from w ww.ja v a 2s .com if (context.isRecordingStats()) { builder.recordStats(); } if (context.maximumSize != MaximumSize.DISABLED) { if (context.weigher == CacheWeigher.DEFAULT) { builder.maximumSize(context.maximumSize.max()); } else { builder.weigher(new GuavaWeigher<Object, Object>(context.weigher)); builder.maximumWeight(context.maximumWeight()); } } if (context.afterAccess != Expire.DISABLED) { builder.expireAfterAccess(context.afterAccess.timeNanos(), TimeUnit.NANOSECONDS); } if (context.afterWrite != Expire.DISABLED) { builder.expireAfterWrite(context.afterWrite.timeNanos(), TimeUnit.NANOSECONDS); } if (context.refresh != Expire.DISABLED) { builder.refreshAfterWrite(context.refresh.timeNanos(), TimeUnit.NANOSECONDS); } if (context.expires() || context.refreshes()) { builder.ticker(context.ticker()); } if (context.keyStrength == ReferenceType.WEAK) { builder.weakKeys(); } else if (context.keyStrength == ReferenceType.SOFT) { throw new IllegalStateException(); } if (context.valueStrength == ReferenceType.WEAK) { builder.weakValues(); } else if (context.valueStrength == ReferenceType.SOFT) { builder.softValues(); } if (context.removalListenerType != Listener.DEFAULT) { boolean translateZeroExpire = (context.afterAccess == Expire.IMMEDIATELY) || (context.afterWrite == Expire.IMMEDIATELY); builder.removalListener(new GuavaRemovalListener<>(translateZeroExpire, context.removalListener)); } Ticker ticker = (context.ticker == null) ? Ticker.systemTicker() : context.ticker; if (context.loader == null) { context.cache = new GuavaCache<>(builder.<Integer, Integer>build(), ticker); } else if (context.loader().isBulk()) { context.cache = new GuavaLoadingCache<>( builder.build(new BulkLoader<Integer, Integer>(context.loader())), ticker); } else { context.cache = new GuavaLoadingCache<>( builder.build(new SingleLoader<Integer, Integer>(context.loader())), ticker); } @SuppressWarnings("unchecked") Cache<K, V> castedCache = (Cache<K, V>) context.cache; return castedCache; }
From source file:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestResourceServiceImpl.java
protected LoadingCache<String, Sardine> buildSardineCache() { CacheProperties cacheProperties = properties.getResourceService().getSardineCache(); CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (cacheProperties.getConcurrencyLevel() != null) { builder.concurrencyLevel(cacheProperties.getConcurrencyLevel()); }/*from ww w.j a v a 2 s. c o m*/ if (cacheProperties.getExpireAfterAccess() != null && cacheProperties.getExpireAfterAccessTimeUnit() != null) { builder.expireAfterAccess(cacheProperties.getExpireAfterAccess(), cacheProperties.getExpireAfterAccessTimeUnit()); } if (cacheProperties.getExpireAfterWrite() != null && cacheProperties.getExpireAfterWriteTimeUnit() != null) { builder.expireAfterWrite(cacheProperties.getExpireAfterWrite(), cacheProperties.getExpireAfterWriteTimeUnit()); } if (cacheProperties.getInitialCapacity() != null) { builder.initialCapacity(cacheProperties.getInitialCapacity()); } if (cacheProperties.getMaximumSize() != null) { builder.maximumSize(cacheProperties.getMaximumSize()); } if (cacheProperties.getMaximumWeight() != null) { builder.maximumWeight(cacheProperties.getMaximumWeight()); } if (cacheProperties.getRefreshAfterWrite() != null && cacheProperties.getRefreshAfterWriteTimeUnit() != null) { builder.refreshAfterWrite(cacheProperties.getRefreshAfterWrite(), cacheProperties.getRefreshAfterWriteTimeUnit()); } return builder.build(sardineCacheLoader); }