Class Caffeine<K,​V>

  • Type Parameters:
    K - the base key type for all caches created by this builder
    V - the base value type for all caches created by this builder

    public final class Caffeine<K,​V>
    extends java.lang.Object
    A builder of AsyncLoadingCache, LoadingCache, and Cache instances having a combination of the following features:
    • automatic loading of entries into the cache, optionally asynchronously
    • size-based eviction when a maximum is exceeded based on frequency and recency
    • time-based expiration of entries, measured since last access or last write
    • asynchronously refresh when the first stale request for an entry occurs
    • keys automatically wrapped in weak references
    • values automatically wrapped in weak or soft references
    • writes propagated to an external resource
    • notification of evicted (or otherwise removed) entries
    • accumulation of cache access statistics

    These features are all optional; caches can be created using all or none of them. By default cache instances created by Caffeine will not perform any type of eviction.

    Usage example:

    
       LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
           .maximumSize(10_000)
           .expireAfterWrite(10, TimeUnit.MINUTES)
           .removalListener((Key key, Graph graph, RemovalCause cause) ->
               System.out.printf("Key %s was removed (%s)%n", key, cause))
           .build(key -> createExpensiveGraph(key));
     

    The returned cache is implemented as a hash table with similar performance characteristics to ConcurrentHashMap. The asMap view (and its collection views) have weakly consistent iterators. This means that they are safe for concurrent use, but if other threads modify the cache after the iterator is created, it is undefined which of these changes, if any, are reflected in that iterator. These iterators never throw ConcurrentModificationException.

    Note: by default, the returned cache uses equality comparisons (the equals method) to determine equality for keys or values. However, if weakKeys() was specified, the cache uses identity (==) comparisons instead for keys. Likewise, if weakValues() or softValues() was specified, the cache uses identity comparisons for values.

    Entries are automatically evicted from the cache when any of maximumSize, maximumWeight, expireAfterWrite, expireAfterAccess, weakKeys, weakValues, or softValues are requested.

    If maximumSize or maximumWeight is requested entries may be evicted on each cache modification.

    If expireAfterWrite or expireAfterAccess is requested entries may be evicted on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(). Expired entries may be counted by Cache.estimatedSize(), but will never be visible to read or write operations.

    If weakKeys, weakValues, or softValues are requested, it is possible for a key or value present in the cache to be reclaimed by the garbage collector. Entries with reclaimed keys or values may be removed from the cache on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(); such entries may be counted in Cache.estimatedSize(), but will never be visible to read or write operations.

    Certain cache configurations will result in the accrual of periodic maintenance tasks which will be performed during write operations, or during occasional read operations in the absence of writes. The Cache.cleanUp() method of the returned cache will also perform maintenance, but calling it should not be necessary with a high throughput cache. Only caches built with maximumSize, maximumWeight, expireAfterWrite, expireAfterAccess, weakKeys, weakValues, or softValues perform periodic maintenance.

    The caches produced by Caffeine are serializable, and the deserialized caches retain all the configuration properties of the original cache. Note that the serialized form does not include cache contents, but only configuration.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  Caffeine.Strength  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Caffeine()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <K1 extends K,​V1 extends V>
      Cache<K1,​V1>
      build()
      Builds a cache which does not automatically load values when keys are requested.
      <K1 extends K,​V1 extends V>
      LoadingCache<K1,​V1>
      build​(CacheLoader<? super K1,​V1> loader)
      Builds a cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader.
      <K1 extends K,​V1 extends V>
      AsyncLoadingCache<K1,​V1>
      buildAsync​(AsyncCacheLoader<? super K1,​V1> loader)
      Builds a cache, which either returns a CompletableFuture already loaded or currently computing the value for a given key, or atomically computes the value asynchronously through a supplied mapping function or the supplied AsyncCacheLoader.
      <K1 extends K,​V1 extends V>
      AsyncLoadingCache<K1,​V1>
      buildAsync​(CacheLoader<? super K1,​V1> loader)
      Builds a cache, which either returns a CompletableFuture already loaded or currently computing the value for a given key, or atomically computes the value asynchronously through a supplied mapping function or the supplied CacheLoader.
      (package private) boolean evicts()  
      Caffeine<K,​V> executor​(java.util.concurrent.Executor executor)
      Specifies the executor to use when running asynchronous tasks.
      Caffeine<K,​V> expireAfterAccess​(long duration, java.util.concurrent.TimeUnit unit)
      Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last read.
      Caffeine<K,​V> expireAfterWrite​(long duration, java.util.concurrent.TimeUnit unit)
      Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
      (package private) boolean expiresAfterAccess()  
      (package private) boolean expiresAfterWrite()  
      static Caffeine<java.lang.Object,​java.lang.Object> from​(CaffeineSpec spec)
      Constructs a new Caffeine instance with the settings specified in spec.
      static Caffeine<java.lang.Object,​java.lang.Object> from​(java.lang.String spec)
      Constructs a new Caffeine instance with the settings specified in spec.
      (package private) <K1 extends K,​V1 extends V>
      CacheWriter<K1,​V1>
      getCacheWriter()  
      (package private) java.util.concurrent.Executor getExecutor()  
      (package private) long getExpiresAfterAccessNanos()  
      (package private) long getExpiresAfterWriteNanos()  
      (package private) int getInitialCapacity()  
      (package private) long getMaximum()  
      (package private) long getRefreshAfterWriteNanos()  
      (package private) <K1 extends K,​V1 extends V>
      RemovalListener<K1,​V1>
      getRemovalListener​(boolean async)  
      (package private) java.util.function.Supplier<? extends StatsCounter> getStatsCounterSupplier()  
      (package private) Ticker getTicker()  
      (package private) <K1 extends K,​V1 extends V>
      Weigher<K1,​V1>
      getWeigher​(boolean isAsync)  
      (package private) boolean hasInitialCapacity()  
      Caffeine<K,​V> initialCapacity​(int initialCapacity)
      Sets the minimum total size for the internal data structures.
      (package private) boolean isBounded()  
      (package private) boolean isRecordingStats()  
      (package private) boolean isSoftValues()  
      (package private) boolean isStrongKeys()  
      (package private) boolean isStrongValues()  
      (package private) boolean isWeakKeys()  
      (package private) boolean isWeakValues()  
      (package private) boolean isWeighted()  
      Caffeine<K,​V> maximumSize​(long maximumSize)
      Specifies the maximum number of entries the cache may contain.
      Caffeine<K,​V> maximumWeight​(long maximumWeight)
      Specifies the maximum weight of entries the cache may contain.
      static Caffeine<java.lang.Object,​java.lang.Object> newBuilder()
      Constructs a new Caffeine instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.
      Caffeine<K,​V> recordStats()
      Enables the accumulation of CacheStats during the operation of the cache.
      Caffeine<K,​V> recordStats​(java.util.function.Supplier<? extends StatsCounter> statsCounterSupplier)
      Enables the accumulation of CacheStats during the operation of the cache.
      Caffeine<K,​V> refreshAfterWrite​(long duration, java.util.concurrent.TimeUnit unit)
      Specifies that active entries are eligible for automatic refresh once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
      (package private) boolean refreshes()  
      <K1 extends K,​V1 extends V>
      Caffeine<K1,​V1>
      removalListener​(RemovalListener<? super K1,​? super V1> removalListener)
      Specifies a listener instance that caches should notify each time an entry is removed for any reason.
      (package private) static void requireArgument​(boolean expression)
      Ensures that the argument expression is true.
      (package private) static void requireArgument​(boolean expression, java.lang.String template, java.lang.Object... args)
      Ensures that the argument expression is true.
      (package private) void requireNonLoadingCache()  
      (package private) static void requireState​(boolean expression)
      Ensures that the state expression is true.
      (package private) static void requireState​(boolean expression, java.lang.String template, java.lang.Object... args)
      Ensures that the state expression is true.
      (package private) void requireWeightWithWeigher()  
      Caffeine<K,​V> softValues()
      Specifies that each value (not key) stored in the cache should be wrapped in a SoftReference (by default, strong references are used).
      Caffeine<K,​V> ticker​(Ticker ticker)
      Specifies a nanosecond-precision time source for use in determining when entries should be expired or refreshed.
      java.lang.String toString()
      Returns a string representation for this Caffeine instance.
      Caffeine<K,​V> weakKeys()
      Specifies that each key (not value) stored in the cache should be wrapped in a WeakReference (by default, strong references are used).
      Caffeine<K,​V> weakValues()
      Specifies that each value (not key) stored in the cache should be wrapped in a WeakReference (by default, strong references are used).
      <K1 extends K,​V1 extends V>
      Caffeine<K1,​V1>
      weigher​(Weigher<? super K1,​? super V1> weigher)
      Specifies the weigher to use in determining the weight of entries.
      <K1 extends K,​V1 extends V>
      Caffeine<K1,​V1>
      writer​(CacheWriter<? super K1,​? super V1> writer)
      Specifies a writer instance that caches should notify each time an entry is explicitly created or modified, or removed for any reason.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • logger

        static final java.util.logging.Logger logger
      • ENABLED_STATS_COUNTER_SUPPLIER

        static final java.util.function.Supplier<StatsCounter> ENABLED_STATS_COUNTER_SUPPLIER
      • strictParsing

        boolean strictParsing
      • maximumSize

        long maximumSize
      • maximumWeight

        long maximumWeight
      • initialCapacity

        int initialCapacity
      • refreshNanos

        long refreshNanos
      • expireAfterWriteNanos

        long expireAfterWriteNanos
      • expireAfterAccessNanos

        long expireAfterAccessNanos
      • statsCounterSupplier

        java.util.function.Supplier<StatsCounter> statsCounterSupplier
      • weigher

        Weigher<? super K,​? super V> weigher
      • executor

        java.util.concurrent.Executor executor
    • Constructor Detail

      • Caffeine

        private Caffeine()
    • Method Detail

      • requireArgument

        static void requireArgument​(boolean expression,
                                    java.lang.String template,
                                    java.lang.Object... args)
        Ensures that the argument expression is true.
      • requireArgument

        static void requireArgument​(boolean expression)
        Ensures that the argument expression is true.
      • requireState

        static void requireState​(boolean expression)
        Ensures that the state expression is true.
      • requireState

        static void requireState​(boolean expression,
                                 java.lang.String template,
                                 java.lang.Object... args)
        Ensures that the state expression is true.
      • newBuilder

        @Nonnull
        public static Caffeine<java.lang.Object,​java.lang.Object> newBuilder()
        Constructs a new Caffeine instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.
        Returns:
        a new instance with default settings
      • from

        @Nonnull
        public static Caffeine<java.lang.Object,​java.lang.Object> from​(CaffeineSpec spec)
        Constructs a new Caffeine instance with the settings specified in spec.
        Parameters:
        spec - the specification to build from
        Returns:
        a new instance with the specification's settings
      • from

        @Nonnull
        public static Caffeine<java.lang.Object,​java.lang.Object> from​(java.lang.String spec)
        Constructs a new Caffeine instance with the settings specified in spec.
        Parameters:
        spec - a String in the format specified by CaffeineSpec
        Returns:
        a new instance with the specification's settings
      • initialCapacity

        @Nonnull
        public Caffeine<K,​V> initialCapacity​(@Nonnegative
                                                   int initialCapacity)
        Sets the minimum total size for the internal data structures. Providing a large enough estimate at construction time avoids the need for expensive resizing operations later, but setting this value unnecessarily high wastes memory.
        Parameters:
        initialCapacity - minimum total size for the internal data structures
        Returns:
        this builder instance
        Throws:
        java.lang.IllegalArgumentException - if initialCapacity is negative
        java.lang.IllegalStateException - if an initial capacity was already set
      • hasInitialCapacity

        boolean hasInitialCapacity()
      • getInitialCapacity

        int getInitialCapacity()
      • executor

        @Nonnull
        public Caffeine<K,​V> executor​(@Nonnull
                                            java.util.concurrent.Executor executor)
        Specifies the executor to use when running asynchronous tasks. The executor is delegated to when sending removal notifications, when asynchronous computations are performed by AsyncLoadingCache or LoadingCache.refresh(K) or refreshAfterWrite(long, java.util.concurrent.TimeUnit), or when performing periodic maintenance. By default, ForkJoinPool.commonPool() is used.

        The primary intent of this method is to facilitate testing of caches which have been configured with removalListener or utilize asynchronous computations. A test may instead prefer to configure the cache to execute tasks directly on the same thread.

        Beware that configuring a cache with an executor that throws RejectedExecutionException may experience non-deterministic behavior.

        Parameters:
        executor - the executor to use for asynchronous execution
        Returns:
        this builder instance
        Throws:
        java.lang.NullPointerException - if the specified executor is null
      • getExecutor

        @Nonnull
        java.util.concurrent.Executor getExecutor()
      • maximumSize

        @Nonnull
        public Caffeine<K,​V> maximumSize​(@Nonnegative
                                               long maximumSize)
        Specifies the maximum number of entries the cache may contain. Note that the cache may evict an entry before this limit is exceeded or temporarily exceed the threshold while evicting. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.

        When size is zero, elements will be evicted immediately after being loaded into the cache. This can be useful in testing, or to disable caching temporarily without a code change.

        This feature cannot be used in conjunction with maximumWeight.

        Parameters:
        maximumSize - the maximum size of the cache
        Returns:
        this builder instance
        Throws:
        java.lang.IllegalArgumentException - if size is negative
        java.lang.IllegalStateException - if a maximum size or weight was already set
      • maximumWeight

        @Nonnull
        public Caffeine<K,​V> maximumWeight​(@Nonnegative
                                                 long maximumWeight)
        Specifies the maximum weight of entries the cache may contain. Weight is determined using the Weigher specified with weigher, and use of this method requires a corresponding call to weigher prior to calling build().

        Note that the cache may evict an entry before this limit is exceeded or temporarily exceed the threshold while evicting. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.

        When maximumWeight is zero, elements will be evicted immediately after being loaded into cache. This can be useful in testing, or to disable caching temporarily without a code change.

        Note that weight is only used to determine whether the cache is over capacity; it has no effect on selecting which entry should be evicted next.

        This feature cannot be used in conjunction with maximumSize.

        Parameters:
        maximumWeight - the maximum total weight of entries the cache may contain
        Returns:
        this builder instance
        Throws:
        java.lang.IllegalArgumentException - if maximumWeight is negative
        java.lang.IllegalStateException - if a maximum weight or size was already set
      • weigher

        @Nonnull
        public <K1 extends K,​V1 extends VCaffeine<K1,​V1> weigher​(@Nonnull
                                                                               Weigher<? super K1,​? super V1> weigher)
        Specifies the weigher to use in determining the weight of entries. Entry weight is taken into consideration by maximumWeight(long) when determining which entries to evict, and use of this method requires a corresponding call to maximumWeight(long) prior to calling build(). Weights are measured and recorded when entries are inserted into the cache, and are thus effectively static during the lifetime of a cache entry.

        When the weight of an entry is zero it will not be considered for size-based eviction (though it still may be evicted by other means).

        Important note: Instead of returning this as a Caffeine instance, this method returns Caffeine<K1, V1>. From this point on, either the original reference or the returned reference may be used to complete configuration and build the cache, but only the "generic" one is type-safe. That is, it will properly prevent you from building caches whose key or value types are incompatible with the types accepted by the weigher already provided; the Caffeine type cannot do this. For best results, simply use the standard method-chaining idiom, as illustrated in the documentation at top, configuring a Caffeine and building your Cache all in a single statement.

        Warning: if you ignore the above advice, and use this Caffeine to build a cache whose key or value type is incompatible with the weigher, you will likely experience a ClassCastException at some undefined point in the future.

        Type Parameters:
        K1 - key type of the weigher
        V1 - value type of the weigher
        Parameters:
        weigher - the weigher to use in calculating the weight of cache entries
        Returns:
        the cache builder reference that should be used instead of this for any remaining configuration and cache building
        Throws:
        java.lang.IllegalArgumentException - if size is negative
        java.lang.IllegalStateException - if a maximum size was already set
      • evicts

        boolean evicts()
      • isWeighted

        boolean isWeighted()
      • getMaximum

        @Nonnegative
        long getMaximum()
      • getWeigher

        @Nonnull
        <K1 extends K,​V1 extends VWeigher<K1,​V1> getWeigher​(boolean isAsync)
      • weakKeys

        @Nonnull
        public Caffeine<K,​V> weakKeys()
        Specifies that each key (not value) stored in the cache should be wrapped in a WeakReference (by default, strong references are used).

        Warning: when this method is used, the resulting cache will use identity (==) comparison to determine equality of keys.

        Entries with keys that have been garbage collected may be counted in Cache.estimatedSize(), but will never be visible to read or write operations; such entries are cleaned up as part of the routine maintenance described in the class javadoc.

        This feature cannot be used in conjunction with writer.

        Returns:
        this builder instance
        Throws:
        java.lang.IllegalStateException - if the key strength was already set or the writer was set
      • isStrongKeys

        boolean isStrongKeys()
      • isWeakKeys

        boolean isWeakKeys()
      • weakValues

        @Nonnull
        public Caffeine<K,​V> weakValues()
        Specifies that each value (not key) stored in the cache should be wrapped in a WeakReference (by default, strong references are used).

        Weak values will be garbage collected once they are weakly reachable. This makes them a poor candidate for caching; consider softValues() instead.

        Note: when this method is used, the resulting cache will use identity (==) comparison to determine equality of values.

        Entries with values that have been garbage collected may be counted in Cache.estimatedSize(), but will never be visible to read or write operations; such entries are cleaned up as part of the routine maintenance described in the class javadoc.

        This feature cannot be used in conjunction with buildAsync(com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1>).

        Returns:
        this builder instance
        Throws:
        java.lang.IllegalStateException - if the value strength was already set
      • isStrongValues

        boolean isStrongValues()
      • isWeakValues

        boolean isWeakValues()
      • isSoftValues

        boolean isSoftValues()
      • softValues

        @Nonnull
        public Caffeine<K,​V> softValues()
        Specifies that each value (not key) stored in the cache should be wrapped in a SoftReference (by default, strong references are used). Softly-referenced objects will be garbage-collected in a globally least-recently-used manner, in response to memory demand.

        Warning: in most circumstances it is better to set a per-cache maximum size instead of using soft references. You should only use this method if you are very familiar with the practical consequences of soft references.

        Note: when this method is used, the resulting cache will use identity (==) comparison to determine equality of values.

        Entries with values that have been garbage collected may be counted in Cache.estimatedSize(), but will never be visible to read or write operations; such entries are cleaned up as part of the routine maintenance described in the class javadoc.

        This feature cannot be used in conjunction with buildAsync(com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1>).

        Returns:
        this builder instance
        Throws:
        java.lang.IllegalStateException - if the value strength was already set
      • expireAfterWrite

        @Nonnull
        public Caffeine<K,​V> expireAfterWrite​(@Nonnegative
                                                    long duration,
                                                    @Nonnull
                                                    java.util.concurrent.TimeUnit unit)
        Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.

        Expired entries may be counted in Cache.estimatedSize(), but will never be visible to read or write operations. Expired entries are cleaned up as part of the routine maintenance described in the class javadoc.

        Parameters:
        duration - the length of time after an entry is created that it should be automatically removed
        unit - the unit that duration is expressed in
        Returns:
        this builder instance
        Throws:
        java.lang.IllegalArgumentException - if duration is negative
        java.lang.IllegalStateException - if the time to live or time to idle was already set
      • getExpiresAfterWriteNanos

        @Nonnegative
        long getExpiresAfterWriteNanos()
      • expiresAfterWrite

        boolean expiresAfterWrite()
      • expireAfterAccess

        @Nonnull
        public Caffeine<K,​V> expireAfterAccess​(@Nonnegative
                                                     long duration,
                                                     @Nonnull
                                                     java.util.concurrent.TimeUnit unit)
        Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last read. Access time is reset by all cache read and write operations (including Cache.asMap().get(Object) and Cache.asMap().put(K, V)), but not by operations on the collection-views of Cache.asMap().

        Expired entries may be counted in Cache.estimatedSize(), but will never be visible to read or write operations. Expired entries are cleaned up as part of the routine maintenance described in the class javadoc.

        Parameters:
        duration - the length of time after an entry is last accessed that it should be automatically removed
        unit - the unit that duration is expressed in
        Returns:
        this builder instance
        Throws:
        java.lang.IllegalArgumentException - if duration is negative
        java.lang.IllegalStateException - if the time to idle or time to live was already set
      • getExpiresAfterAccessNanos

        @Nonnegative
        long getExpiresAfterAccessNanos()
      • expiresAfterAccess

        boolean expiresAfterAccess()
      • refreshAfterWrite

        @Nonnull
        public Caffeine<K,​V> refreshAfterWrite​(@Nonnegative
                                                     long duration,
                                                     @Nonnull
                                                     java.util.concurrent.TimeUnit unit)
        Specifies that active entries are eligible for automatic refresh once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value. The semantics of refreshes are specified in LoadingCache.refresh(K), and are performed by calling CacheLoader.reload(K, V).

        Automatic refreshes are performed when the first stale request for an entry occurs. The request triggering refresh will make an asynchronous call to CacheLoader.reload(K, V) and immediately return the old value.

        Note: all exceptions thrown during refresh will be logged and then swallowed.

        Parameters:
        duration - the length of time after an entry is created that it should be considered stale, and thus eligible for refresh
        unit - the unit that duration is expressed in
        Returns:
        this builder instance
        Throws:
        java.lang.IllegalArgumentException - if duration is negative
        java.lang.IllegalStateException - if the refresh interval was already set
      • getRefreshAfterWriteNanos

        @Nonnegative
        long getRefreshAfterWriteNanos()
      • refreshes

        boolean refreshes()
      • getTicker

        @Nonnull
        Ticker getTicker()
      • removalListener

        @Nonnull
        public <K1 extends K,​V1 extends VCaffeine<K1,​V1> removalListener​(@Nonnull
                                                                                       RemovalListener<? super K1,​? super V1> removalListener)
        Specifies a listener instance that caches should notify each time an entry is removed for any reason. Each cache created by this builder will invoke this listener as part of the routine maintenance described in the class documentation above.

        Warning: after invoking this method, do not continue to use this cache builder reference; instead use the reference this method returns. At runtime, these point to the same instance, but only the returned reference has the correct generic type information so as to ensure type safety. For best results, use the standard method-chaining idiom illustrated in the class documentation above, configuring a builder and building your cache in a single statement. Failure to heed this advice can result in a ClassCastException being thrown by a cache operation at some undefined point in the future.

        Warning: any exception thrown by listener will not be propagated to the Cache user, only logged via a Logger.

        Type Parameters:
        K1 - the key type of the listener
        V1 - the value type of the listener
        Parameters:
        removalListener - a listener instance that caches should notify each time an entry is removed
        Returns:
        the cache builder reference that should be used instead of this for any remaining configuration and cache building
        Throws:
        java.lang.IllegalStateException - if a removal listener was already set
        java.lang.NullPointerException - if the specified removal listener is null
      • getRemovalListener

        <K1 extends K,​V1 extends VRemovalListener<K1,​V1> getRemovalListener​(boolean async)
      • writer

        @Nonnull
        public <K1 extends K,​V1 extends VCaffeine<K1,​V1> writer​(@Nonnull
                                                                              CacheWriter<? super K1,​? super V1> writer)
        Specifies a writer instance that caches should notify each time an entry is explicitly created or modified, or removed for any reason. The writer is not notified when an entry is loaded or computed. Each cache created by this builder will invoke this writer as part of the atomic operation that modifies the cache.

        Warning: after invoking this method, do not continue to use this cache builder reference; instead use the reference this method returns. At runtime, these point to the same instance, but only the returned reference has the correct generic type information so as to ensure type safety. For best results, use the standard method-chaining idiom illustrated in the class documentation above, configuring a builder and building your cache in a single statement. Failure to heed this advice can result in a ClassCastException being thrown by a cache operation at some undefined point in the future.

        Warning: any exception thrown by writer will be propagated to the Cache user.

        This feature cannot be used in conjunction with weakKeys() or buildAsync(com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1>).

        Type Parameters:
        K1 - the key type of the writer
        V1 - the value type of the writer
        Parameters:
        writer - a writer instance that caches should notify each time an entry is explicitly created or modified, or removed for any reason
        Returns:
        the cache builder reference that should be used instead of this for any remaining configuration and cache building
        Throws:
        java.lang.IllegalStateException - if a writer was already set or if the key strength is weak
        java.lang.NullPointerException - if the specified writer is null
      • getCacheWriter

        <K1 extends K,​V1 extends VCacheWriter<K1,​V1> getCacheWriter()
      • recordStats

        @Nonnull
        public Caffeine<K,​V> recordStats()
        Enables the accumulation of CacheStats during the operation of the cache. Without this Cache.stats() will return zero for all statistics. Note that recording statistics requires bookkeeping to be performed with each operation, and thus imposes a performance penalty on cache operation.
        Returns:
        this builder instance
      • recordStats

        @Nonnull
        public Caffeine<K,​V> recordStats​(@Nonnull
                                               java.util.function.Supplier<? extends StatsCounter> statsCounterSupplier)
        Enables the accumulation of CacheStats during the operation of the cache. Without this Cache.stats() will return zero for all statistics. Note that recording statistics requires bookkeeping to be performed with each operation, and thus imposes a performance penalty on cache operation. Any exception thrown by the supplied StatsCounter will be suppressed and logged.
        Parameters:
        statsCounterSupplier - a supplier instance that returns a new StatsCounter
        Returns:
        this builder instance
      • isRecordingStats

        boolean isRecordingStats()
      • getStatsCounterSupplier

        @Nonnull
        java.util.function.Supplier<? extends StatsCounter> getStatsCounterSupplier()
      • isBounded

        boolean isBounded()
      • build

        @Nonnull
        public <K1 extends K,​V1 extends VCache<K1,​V1> build()
        Builds a cache which does not automatically load values when keys are requested.

        Consider build(CacheLoader) instead, if it is feasible to implement a CacheLoader.

        This method does not alter the state of this Caffeine instance, so it can be invoked again to create multiple independent caches.

        Type Parameters:
        K1 - the key type of the cache
        V1 - the value type of the cache
        Returns:
        a cache having the requested features
      • build

        @Nonnull
        public <K1 extends K,​V1 extends VLoadingCache<K1,​V1> build​(@Nonnull
                                                                                 CacheLoader<? super K1,​V1> loader)
        Builds a cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader. If another thread is currently loading the value for this key, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.

        This method does not alter the state of this Caffeine instance, so it can be invoked again to create multiple independent caches.

        Type Parameters:
        K1 - the key type of the loader
        V1 - the value type of the loader
        Parameters:
        loader - the cache loader used to obtain new values
        Returns:
        a cache having the requested features
        Throws:
        java.lang.NullPointerException - if the specified cache loader is null
      • buildAsync

        @Nonnull
        public <K1 extends K,​V1 extends VAsyncLoadingCache<K1,​V1> buildAsync​(@Nonnull
                                                                                           CacheLoader<? super K1,​V1> loader)
        Builds a cache, which either returns a CompletableFuture already loaded or currently computing the value for a given key, or atomically computes the value asynchronously through a supplied mapping function or the supplied CacheLoader. If the asynchronous computation fails or computes a null value then the entry will be automatically removed. Note that multiple threads can concurrently load values for distinct keys.

        This method does not alter the state of this Caffeine instance, so it can be invoked again to create multiple independent caches.

        Type Parameters:
        K1 - the key type of the loader
        V1 - the value type of the loader
        Parameters:
        loader - the cache loader used to obtain new values
        Returns:
        a cache having the requested features
        Throws:
        java.lang.IllegalStateException - if the value strength is weak or soft
        java.lang.NullPointerException - if the specified cache loader is null
      • buildAsync

        @Nonnull
        public <K1 extends K,​V1 extends VAsyncLoadingCache<K1,​V1> buildAsync​(@Nonnull
                                                                                           AsyncCacheLoader<? super K1,​V1> loader)
        Builds a cache, which either returns a CompletableFuture already loaded or currently computing the value for a given key, or atomically computes the value asynchronously through a supplied mapping function or the supplied AsyncCacheLoader. If the asynchronous computation fails or computes a null value then the entry will be automatically removed. Note that multiple threads can concurrently load values for distinct keys.

        This method does not alter the state of this Caffeine instance, so it can be invoked again to create multiple independent caches.

        Type Parameters:
        K1 - the key type of the loader
        V1 - the value type of the loader
        Parameters:
        loader - the cache loader used to obtain new values
        Returns:
        a cache having the requested features
        Throws:
        java.lang.IllegalStateException - if the value strength is weak or soft
        java.lang.NullPointerException - if the specified cache loader is null
      • requireNonLoadingCache

        void requireNonLoadingCache()
      • requireWeightWithWeigher

        void requireWeightWithWeigher()
      • toString

        public java.lang.String toString()
        Returns a string representation for this Caffeine instance. The exact form of the returned string is not specified.
        Overrides:
        toString in class java.lang.Object