Class Cache2.Builder<K1,K2,V>
- Type Parameters:
K1- The first key type.K2- The second key type.V- The value type.
Cache2 instances.
Example:
Cache2<String,Integer,User>
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds a newCache2instance with the configured settings.Sets the caching mode for this cache.Conditionally enables logging of cache statistics when the JVM exits.Enables logging of cache statistics when the JVM exits.maxSize(int value) Specifies the maximum number of entries allowed in this cache.Specifies the default supplier function for computing values when keys are not found.Enables thread-local caching.weak()Sets the caching mode toWEAK.
-
Method Details
-
build
Builds a newCache2instance with the configured settings.- Returns:
- A new immutable
Cache2instance.
-
cacheMode
Sets the caching mode for this cache.Available modes:
NONE- No caching (always invoke supplier)WEAK- Weak caching (usesWeakHashMap)FULL- Full caching (usesConcurrentHashMap, default)
- Parameters:
value- The caching mode.- Returns:
- This object for method chaining.
-
logOnExit
Conditionally enables logging of cache statistics when the JVM exits.When enabled, the cache will register a shutdown hook that logs the cache name, total cache hits, and total cache misses (size of cache) to help analyze cache effectiveness.
- Parameters:
value- Whether to enable logging on exit.idValue- The identifier to use in the log message.- Returns:
- This object for method chaining.
-
logOnExit
Enables logging of cache statistics when the JVM exits.When enabled, the cache will register a shutdown hook that logs the cache name, total cache hits, and total cache misses (size of cache) to help analyze cache effectiveness.
Example output:
ClassMeta cache: hits=1523, misses: 47
This is useful for:
- Performance tuning and identifying caching opportunities
- Determining optimal max size values
- Monitoring cache efficiency in production
- Parameters:
value- The identifier to use in the log message.- Returns:
- This object for method chaining.
-
maxSize
Specifies the maximum number of entries allowed in this cache.When the cache size exceeds this value, the entire cache is cleared to make room for new entries. This is a simple eviction strategy that avoids the overhead of LRU/LFU tracking.
Default value: 1000 (or value of system property
juneau.cache.maxSize )Notes:
- Setting this too low may cause excessive cache clearing and reduce effectiveness
- Setting this too high may consume excessive memory
- For unbounded caching, use
Integer.MAX_VALUE(not recommended for production)
- Parameters:
value- The maximum number of cache entries. Must be positive.- Returns:
- This object for method chaining.
-
supplier
Specifies the default supplier function for computing values when keys are not found.This supplier will be used by
Cache2.get(Object, Object)when a key pair is not in the cache. Individual lookups can override this supplier usingCache2.get(Object, Object, java.util.function.Supplier).Example:
Cache2<String,Integer,User>
cache = Cache2 .of (String.class , Integer.class , User.class ) .supplier((tenant, id) -> userService.findUser(tenant, id)) .build();// Uses default supplier Useru =cache .get("tenant1" , 123);- Parameters:
value- The default supplier function. Can benull .- Returns:
- This object for method chaining.
-
threadLocal
Enables thread-local caching.When enabled, each thread gets its own separate cache instance. This is useful for thread-unsafe objects that need to be cached per thread.
This is a shortcut for wrapping a cache in a
ThreadLocal, but provides a cleaner API.- Returns:
- This object for method chaining.
- See Also:
-
weak
Sets the caching mode toWEAK.This is a shortcut for calling
cacheMode(CacheMode.WEAK) .Weak caching uses
WeakHashMapfor storage, allowing cache entries to be garbage collected when keys are no longer strongly referenced elsewhere.- Returns:
- This object for method chaining.
- See Also:
-