Object unless it is constrained by using a method like #removalListener
Object unless it is constrained by using a method like #removalListener
Cache, LoadingCache, AsyncCache, and
AsyncLoadingCache instances having a combination of the following features:
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(Duration.ofMinutes(10))
.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, expireAfter, expireAfterWrite, expireAfterAccess, weakKeys, weakValues, or softValues are requested.
If maximumSize or maximumWeight is requested, entries may be evicted on each cache modification.
If expireAfter,
expireAfterWrite, or
expireAfterAccess is requested, then entries may be evicted on
each cache modification, on occasional cache accesses, or on calls to Cache#cleanUp. A
scheduler(Scheduler) may be specified to provide prompt removal of expired entries
rather than waiting until activity triggers the periodic maintenance. 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 that
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,
expireAfter, 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.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static enum |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv static final double | |
| pack-priv static final int | |
| pack-priv static final Supplier | |
| pack-priv @Nullable RemovalListener | |
| pack-priv @Nullable Executor | |
| pack-priv long | |
| pack-priv long | |
| pack-priv @Nullable Expiry | |
| pack-priv int | |
| pack-priv boolean | |
| pack-priv @Nullable Caffeine. | |
| pack-priv static final System. | |
| pack-priv long | |
| pack-priv long | |
| pack-priv long | |
| pack-priv @Nullable RemovalListener | |
| pack-priv @Nullable Scheduler | |
| pack-priv @Nullable Supplier | |
| pack-priv boolean | |
| pack-priv @Nullable Ticker | |
| pack-priv static final int | |
| pack-priv @Nullable Caffeine. | |
| pack-priv @Nullable Weigher |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public < the key type of the cache K1 extends K, the value type of the cache V1 extends V> Cache | Returns: a cache having the requested featuresBuilds a cache which does not automatically load values when keys are requested unless a mapping function is provided. |
| public < the key type of the loader K1 extends K, the value type of the loader V1 extends V> LoadingCache | Returns: a cache having the requested featuresthe cache loader used to obtain new values loader)Builds a cache, which either returns an already-loaded value for a given key or atomically
computes or retrieves it using the supplied |
| public < the key type of the cache K1 extends K, the value type of the cache V1 extends V> AsyncCache | Returns: a cache having the requested featuresBuilds a cache which does not automatically load values when keys are requested unless a mapping function is provided. |
| public < the key type of the loader K1 extends K, the value type of the loader V1 extends V> AsyncLoadingCache | Returns: a cache having the requested featuresthe cache loader used to obtain new values loader)Builds a cache, which either returns a |
| public < the key type of the loader K1 extends K, the value type of the loader V1 extends V> AsyncLoadingCache | Returns: a cache having the requested featuresthe cache loader used to obtain new values loader)Builds a cache, which either returns a |
| pack-priv static int | Returns: initial capacity for HashMap based classesthe expected number of mappings numMappings)Calculate initial capacity for |
| pack-priv static int | Returns: initial capacity for HashMap based classesthe expected number of mappings iterable)Calculate initial capacity for |
| pack-priv static int | |
| pack-priv static long | |
| public < the key type of the listener K1 extends K, the value type of the listener V1 extends V> Caffeine | Returns: the cache builder reference that should be used instead ofthis for any
remaining configuration and cache buildinga listener instance that caches should notify each time an entry is
being automatically removed due to eviction evictionListener)Specifies a listener instance that caches should notify each time an entry is evicted. |
| pack-priv boolean | |
| public Caffeine | |
| public < key type of the expiry K1 extends K, value type of the expiry V1 extends V> Caffeine | Returns: thisCaffeine instance (for chaining)the expiry to use in calculating the expiration time of cache entries expiry)Specifies that each entry should be automatically removed from the cache once a duration has elapsed after the entry's creation, the most recent replacement of its value, or its last read. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the length of time after an entry is last accessed that it should be
automatically removed duration)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 access. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the length of time after an entry is last accessed that it should be
automatically removed duration, TimeUnit the unit that unit)duration is expressed inSpecifies 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. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the length of time after an entry is created that it should be automatically
removed duration)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. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the length of time after an entry is created that it should be automatically
removed duration, TimeUnit the unit that unit)duration is expressed inSpecifies 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. |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| public static Caffeine | Returns: a new instance with the specification's settingsthe specification to build from spec)Constructs a new |
| public static Caffeine | Returns: a new instance with the specification's settingsa String in the format specified by spec)CaffeineSpecConstructs a new |
| pack-priv <K1 extends K, V1 extends V> @Nullable RemovalListener | |
| pack-priv Executor | |
| pack-priv long | |
| pack-priv long | |
| pack-priv @Nullable Expiry | |
| pack-priv int | |
| pack-priv long | |
| pack-priv long | |
| pack-priv <K1 extends K, V1 extends V> @Nullable RemovalListener | |
| pack-priv Scheduler | |
| pack-priv Supplier | |
| pack-priv Ticker | |
| pack-priv <K1 extends K, V1 extends V> Weigher | |
| pack-priv boolean | |
| public Caffeine | Returns: thisCaffeine instance (for chaining)minimum total size for the internal data structures initialCapacity)Sets the minimum total size for the internal data structures. |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the maximum size of the cache maximumSize)Specifies the maximum number of entries the cache may contain. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the maximum total weight of entries the cache may contain maximumWeight)Specifies the maximum weight of entries the cache may contain. |
| public static Caffeine | Returns: a new instance with default settingsConstructs a new |
| pack-priv static <K> BoundedLocalCache | |
| public Caffeine | Returns: thisCaffeine instance (for chaining)Enables the accumulation of |
| public Caffeine | Returns: thisCaffeine instance (for chaining)a supplier instance that returns a new statsCounterSupplier)StatsCounterEnables the accumulation of |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the length of time after an entry is created that it should be considered
stale, and thus eligible for refresh duration)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. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the length of time after an entry is created that it should be considered
stale, and thus eligible for refresh duration, TimeUnit the unit that unit)duration is expressed inSpecifies 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. |
| pack-priv boolean | |
| public < the key type of the listener K1 extends K, the value type of the listener V1 extends V> Caffeine | Returns: the cache builder reference that should be used instead ofthis for any
remaining configuration and cache buildinga listener instance that caches should notify each time an entry is
removed removalListener)Specifies a listener instance that caches should notify each time an entry is removed for any reason. |
| pack-priv static void | requireArgument(boolean expression, String template, @Nullable Object... args)
Ensures that the argument expression is true. |
| pack-priv static void | |
| pack-priv void | |
| pack-priv static void | |
| pack-priv static void | requireState(boolean expression, String template, @Nullable Object... args)
Ensures that the state expression is true. |
| pack-priv void | |
| pack-priv static long | saturatedToNanos(Duration duration)
Returns the number of nanoseconds of the given duration without throwing or overflowing. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)the scheduler that submits a task to the scheduler)executor(Executor) after a
given delaySpecifies the scheduler to use when scheduling routine maintenance based on an expiration event. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)Specifies that each value (not key) stored in the cache should be wrapped in a
|
| public Caffeine | |
| public String | toString()
Overrides java. |
| public Caffeine | Returns: thisCaffeine instance (for chaining)Specifies that each key (not value) stored in the cache should be wrapped in a
|
| public Caffeine | Returns: thisCaffeine instance (for chaining)Specifies that each value (not key) stored in the cache should be wrapped in a
|
| public < key type of the weigher K1 extends K, value type of the weigher V1 extends V> Caffeine |