public WeightComparator(final WorkUnitWeighter weighter) { this.weighter = weighter; this.weightCache = CacheBuilder.newBuilder().softValues().build(new CacheLoader<WorkUnit, Long>() { @Override public Long load(WorkUnit key) throws Exception { return WeightComparator.this.weighter.weight(key); } }); }
/** * Constructor intended to be instantiated by Jenkins only. */ @Restricted(NoExternalUse.class) public UserDetailsCache() { if (EXPIRE_AFTER_WRITE_SEC == null || EXPIRE_AFTER_WRITE_SEC <= 0) { //just in case someone is trying to trick us EXPIRE_AFTER_WRITE_SEC = SystemProperties.getInteger(SYS_PROP_NAME, (int)TimeUnit.MINUTES.toSeconds(2)); if (EXPIRE_AFTER_WRITE_SEC <= 0) { //The property could also be set to a negative value EXPIRE_AFTER_WRITE_SEC = (int)TimeUnit.MINUTES.toSeconds(2); } } detailsCache = newBuilder().softValues().expireAfterWrite(EXPIRE_AFTER_WRITE_SEC, TimeUnit.SECONDS).build(); existenceCache = newBuilder().softValues().expireAfterWrite(EXPIRE_AFTER_WRITE_SEC, TimeUnit.SECONDS).build(); }
/** * * @param valueFallback - the fall back {@link ConfigStoreValueInspector} which used to get the raw {@link com.typesafe.config.Config} * @param useStrongRef - if true, use Strong reference in cache, else, use Weak reference in cache */ public InMemoryValueInspector (ConfigStoreValueInspector valueFallback, boolean useStrongRef){ this.valueFallback = valueFallback; if (useStrongRef) { this.ownConfigCache = CacheBuilder.newBuilder().build(); this.recursiveConfigCache = CacheBuilder.newBuilder().build(); } else{ this.ownConfigCache = CacheBuilder.newBuilder().softValues().build(); this.recursiveConfigCache = CacheBuilder.newBuilder().softValues().build(); } }
@VisibleForTesting SmallTableLocalCache(Ticker ticker) { cleanupService = Executors.newScheduledThreadPool(INTEGER_ONE, new ThreadFactoryBuilder().setNameFormat("SmallTableCache Cleanup Thread").setDaemon(true).build()); cacheL1 = CacheBuilder.newBuilder().expireAfterAccess(L1_CACHE_EXPIRE_DURATION, TimeUnit.SECONDS) .ticker(ticker).build(); cacheL2 = CacheBuilder.newBuilder().softValues().build(); cleanupService.scheduleAtFixedRate(() -> { cleanup(); }, INTEGER_ZERO, MAINTENANCE_THREAD_CLEANUP_PERIOD, TimeUnit.SECONDS); }
private DictionaryManager(KylinConfig config) { this.config = config; this.dictCache = CacheBuilder.newBuilder()// .softValues()// .removalListener(new RemovalListener<String, DictionaryInfo>() { @Override public void onRemoval(RemovalNotification<String, DictionaryInfo> notification) { DictionaryManager.logger.info("Dict with resource path " + notification.getKey() + " is removed due to " + notification.getCause()); } })// .maximumSize(config.getCachedDictMaxEntrySize())// .expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, DictionaryInfo>() { @Override public DictionaryInfo load(String key) throws Exception { DictionaryInfo dictInfo = DictionaryManager.this.load(key, true); if (dictInfo == null) { return NONE_INDICATOR; } else { return dictInfo; } } }); }
switch (valueStrength) { case SOFT: builder.softValues(); break; case WEAK:
LocalCache(int numThreads, long cacheMemSize, boolean useSoftRef) { CacheBuilder<Path, TailAndFileData> builder = CacheBuilder.newBuilder() .initialCapacity(DEFAULT_CACHE_INITIAL_CAPACITY) .concurrencyLevel(numThreads) .maximumWeight(cacheMemSize) .weigher(new Weigher<Path, TailAndFileData>() { @Override public int weigh(Path key, TailAndFileData value) { return value.getMemoryUsage(); } }); if (useSoftRef) { builder = builder.softValues(); } cache = builder.build(); }
/** * Call this method when you have an instance when you used the * default constructor or when you have a deserialized instance that you * want to reconnect with an extant bdbje environment. * @param env * @param dbName * @param valueClass * @param classCatalog * @throws DatabaseException */ public void initialize(final Environment env, String dbName, final Class valueClass, final StoredClassCatalog classCatalog) throws DatabaseException { // TODO: tune capacity for actual threads, expected size of key caches? this.memMap = CacheBuilder.newBuilder() .concurrencyLevel(64) .initialCapacity(8192) .softValues() .<String, V>build() .asMap(); this.db = openDatabase(env, dbName); this.diskMap = createDiskMap(this.db, classCatalog, valueClass); // keep a record of items that must be persisted; auto-persist if // unchanged after 5 minutes, or more than 10K would collect this.count = new AtomicLong(diskMap.size()); }
switch (valueStrength) { case SOFT: builder.softValues(); break; case WEAK:
public void init(String baseDir) throws IOException { this.baseDir = convertToAbsolutePath(baseDir); final GlobalDictStore globalDictStore = new GlobalDictHDFSStore(this.baseDir); Long[] versions = globalDictStore.listAllVersions(); if (versions.length == 0) { this.metadata = new GlobalDictMetadata(0, 0, 0, 0, null, new TreeMap<AppendDictSliceKey, String>()); return; // for the removed SegmentAppendTrieDictBuilder } final long latestVersion = versions[versions.length - 1]; final Path latestVersionPath = globalDictStore.getVersionDir(latestVersion); this.metadata = globalDictStore.getMetadata(latestVersion); this.bytesConvert = metadata.bytesConverter; this.dictCache = CacheBuilder.newBuilder().softValues() .removalListener(new RemovalListener<AppendDictSliceKey, AppendDictSlice>() { @Override public void onRemoval(RemovalNotification<AppendDictSliceKey, AppendDictSlice> notification) { logger.info("Evict slice with key {} and value {} caused by {}, size {}/{}", notification.getKey(), notification.getValue(), notification.getCause(), dictCache.size(), metadata.sliceFileMap.size()); } }).build(new CacheLoader<AppendDictSliceKey, AppendDictSlice>() { @Override public AppendDictSlice load(AppendDictSliceKey key) throws Exception { AppendDictSlice slice = globalDictStore.readSlice(latestVersionPath.toString(), metadata.sliceFileMap.get(key)); logger.trace("Load slice with key {} and value {}", key, slice); return slice; } }); }
@GwtIncompatible // weakValues public void testValueStrengthSetTwice() { CacheBuilder<Object, Object> builder1 = CacheBuilder.newBuilder().weakValues(); try { builder1.weakValues(); fail(); } catch (IllegalStateException expected) { } try { builder1.softValues(); fail(); } catch (IllegalStateException expected) { } CacheBuilder<Object, Object> builder2 = CacheBuilder.newBuilder().softValues(); try { builder2.softValues(); fail(); } catch (IllegalStateException expected) { } try { builder2.weakValues(); fail(); } catch (IllegalStateException expected) { } }
public void testSetSoftValues() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().softValues()); checkStrength(map, Strength.STRONG, Strength.SOFT); assertSame(EntryFactory.STRONG, map.entryFactory); }
/** Returns an iterable containing all combinations weakKeys and weak/softValues. */ @SuppressWarnings("unchecked") // varargs private static Iterable<CacheBuilder<Object, Object>> allKeyValueStrengthMakers() { return ImmutableList.of( createCacheBuilder(), createCacheBuilder().weakValues(), createCacheBuilder().softValues(), createCacheBuilder().weakKeys(), createCacheBuilder().weakKeys().weakValues(), createCacheBuilder().weakKeys().softValues()); }
public void testParse_whitespaceAllowed() { CacheBuilderSpec spec = parse( " initialCapacity=10,\nmaximumSize=20,\t\r" + "weakKeys \t ,softValues \n , \r expireAfterWrite \t = 15s\n\n"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.SOFT, spec.valueStrength); assertEquals(TimeUnit.SECONDS, spec.writeExpirationTimeUnit); assertEquals(15L, spec.writeExpirationDuration); assertNull(spec.accessExpirationTimeUnit); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .weakKeys() .softValues() .expireAfterWrite(15L, TimeUnit.SECONDS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
public void testParse_softValues() { CacheBuilderSpec spec = parse("softValues"); assertNull(spec.initialCapacity); assertNull(spec.maximumSize); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertNull(spec.keyStrength); assertEquals(Strength.SOFT, spec.valueStrength); assertNull(spec.writeExpirationTimeUnit); assertNull(spec.accessExpirationTimeUnit); assertCacheBuilderEquivalence(CacheBuilder.newBuilder().softValues(), CacheBuilder.from(spec)); }
public void testRemovalListener_collected() { QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder().concurrencyLevel(1).softValues().removalListener(listener)); Segment<Object, Object> segment = map.segments[0]; assertTrue(listener.isEmpty()); Object one = new Object(); Object two = new Object(); Object three = new Object(); map.put(one, two); map.put(two, three); assertTrue(listener.isEmpty()); int hash = map.hash(one); ReferenceEntry<Object, Object> entry = segment.getEntry(one, hash); map.reclaimValue(entry.getValueReference()); assertNotified(listener, one, two, RemovalCause.COLLECTED); assertTrue(listener.isEmpty()); }
CacheBuilder.newBuilder() .weakKeys() .softValues() .expireAfterAccess(123, NANOSECONDS) .maximumWeight(789)
CacheBuilder.newBuilder() .weakKeys() .softValues() .expireAfterAccess(123, SECONDS) .expireAfterWrite(456, MINUTES)
builder.weakValues(); } else if (context.valueStrength == ReferenceType.SOFT) { builder.softValues();
.createTestSuite()); suite.addTest( MapTestSuiteBuilder.using(new TestStringCacheGenerator(createCacheBuilder().softValues())) .named("LocalCache with softValues") // values are string literals and won't be GC'd .withFeatures(