Java Code Examples for com.github.benmanes.caffeine.cache.Caffeine#maximumWeight()
The following examples show how to use
com.github.benmanes.caffeine.cache.Caffeine#maximumWeight() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaffeineCache.java From lucene-solr with Apache License 2.0 | 6 votes |
@SuppressWarnings({"unchecked"}) private Cache<K, V> buildCache(Cache<K, V> prev) { @SuppressWarnings({"rawtypes"}) Caffeine builder = Caffeine.newBuilder() .initialCapacity(initialSize) .executor(executor) .removalListener(this) .recordStats(); if (maxIdleTimeSec > 0) { builder.expireAfterAccess(Duration.ofSeconds(maxIdleTimeSec)); } if (maxRamBytes != Long.MAX_VALUE) { builder.maximumWeight(maxRamBytes); builder.weigher((k, v) -> (int) (RamUsageEstimator.sizeOfObject(k) + RamUsageEstimator.sizeOfObject(v))); } else { builder.maximumSize(maxSize); } Cache<K, V> newCache = builder.build(); if (prev != null) { newCache.putAll(prev.asMap()); } return newCache; }
Example 2
Source File: CaffeinePolicy.java From caffeine with Apache License 2.0 | 6 votes |
public CaffeinePolicy(Config config, Set<Characteristic> characteristics) { policyStats = new PolicyStats("product.Caffeine"); BasicSettings settings = new BasicSettings(config); Caffeine<Long, AccessEvent> builder = Caffeine.newBuilder() .removalListener((Long key, AccessEvent value, RemovalCause cause) -> policyStats.recordEviction()) .initialCapacity(settings.maximumSize()) .executor(Runnable::run); if (characteristics.contains(WEIGHTED)) { builder.maximumWeight(settings.maximumSize()); builder.weigher((key, value) -> value.weight()); } else { builder.maximumSize(settings.maximumSize()); } cache = builder.build(); }
Example 3
Source File: CacheBuilderTest.java From caffeine with Apache License 2.0 | 5 votes |
@GwtIncompatible("maximumWeight") public void testMaximumSize_andWeight() { Caffeine<Object, Object> builder = Caffeine.newBuilder().maximumSize(16); try { builder.maximumWeight(16); fail(); } catch (IllegalStateException expected) {} }
Example 4
Source File: CacheBuilderTest.java From caffeine with Apache License 2.0 | 5 votes |
@GwtIncompatible("maximumWeight") public void testMaximumWeight_negative() { Caffeine<Object, Object> builder = Caffeine.newBuilder(); try { builder.maximumWeight(-1); fail(); } catch (IllegalArgumentException expected) {} }
Example 5
Source File: CaffeineCacheFromContext.java From caffeine with Apache License 2.0 | 4 votes |
public static <K, V> Cache<K, V> newCaffeineCache(CacheContext context) { Caffeine<Object, Object> builder = Caffeine.newBuilder(); context.caffeine = builder; if (context.initialCapacity != InitialCapacity.DEFAULT) { builder.initialCapacity(context.initialCapacity.size()); } if (context.isRecordingStats()) { builder.recordStats(); } if (context.maximumSize != Maximum.DISABLED) { if (context.weigher == CacheWeigher.DEFAULT) { builder.maximumSize(context.maximumSize.max()); } else { builder.weigher(context.weigher); builder.maximumWeight(context.maximumWeight()); } } if (context.expiryType() != CacheExpiry.DISABLED) { builder.expireAfter(context.expiry); } 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()) { SerializableTicker ticker = context.ticker()::read; builder.ticker(ticker); } if (context.keyStrength == ReferenceType.WEAK) { builder.weakKeys(); } else if (context.keyStrength == ReferenceType.SOFT) { throw new IllegalStateException(); } if (context.isWeakValues()) { builder.weakValues(); } else if (context.isSoftValues()) { builder.softValues(); } if (context.cacheExecutor != CacheExecutor.DEFAULT) { builder.executor(context.executor); } if (context.cacheScheduler != CacheScheduler.DEFAULT) { builder.scheduler(context.scheduler); } if (context.removalListenerType != Listener.DEFAULT) { builder.removalListener(context.removalListener); } if (context.isStrongKeys() && !context.isAsync()) { builder.writer(context.cacheWriter()); } if (context.isAsync()) { if (context.loader == null) { context.asyncCache = builder.buildAsync(); } else { context.asyncCache = builder.buildAsync( context.isAsyncLoading ? context.loader.async() : context.loader); } context.cache = context.asyncCache.synchronous(); } else if (context.loader == null) { context.cache = builder.build(); } else { context.cache = builder.build(context.loader); } @SuppressWarnings("unchecked") Cache<K, V> castedCache = (Cache<K, V>) context.cache; RandomSeedEnforcer.resetThreadLocalRandom(); return castedCache; }