Enum Class CacheMode
- All Implemented Interfaces:
Serializable,Comparable<CacheMode>,Constable
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionFull caching - usesConcurrentHashMapfor storage.No caching - all lookups invoke the supplier.Weak caching - usesWeakHashMapfor storage. -
Method Summary
-
Enum Constant Details
-
NONE
No caching - all lookups invoke the supplier.When this mode is used, the cache will not store any values. Every call to
Cache.get(Object)orCache.get(Object, java.util.function.Supplier)will invoke the supplier to compute the value.This mode is useful for:
- Testing scenarios where you want fresh computation each time
- Development environments where caching might hide issues
- Temporarily disabling caching without code changes
-
WEAK
Weak caching - usesWeakHashMapfor storage.Cache entries can be garbage collected when keys are no longer strongly referenced elsewhere. The WeakHashMap is wrapped with
Collections.synchronizedMap(java.util.Map)for thread safety.This mode is useful for:
- Caching metadata about objects that may be unloaded (e.g.,
Classobjects) - Memory-sensitive applications where cache entries should not prevent garbage collection
- Scenarios where keys have limited lifetimes
Note: Weak caching comes with performance trade-offs:
- Slightly slower access due to synchronization overhead
- Entries may be removed unpredictably by the garbage collector
- Not suitable for high-concurrency scenarios
- Caching metadata about objects that may be unloaded (e.g.,
-
FULL
Full caching - usesConcurrentHashMapfor storage.Provides the best performance with lock-free reads and writes. Cached entries will remain in memory until explicitly removed or the cache is cleared due to exceeding the maximum size.
This is the default and recommended mode for most use cases, offering:
- Excellent performance with no synchronization overhead for reads
- Thread-safe concurrent access
- Predictable memory usage (entries stay until evicted)
- Suitable for high-concurrency scenarios
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum class has no constant with the specified nameNullPointerException- if the argument is null
-
parse
Parses a string value into aCacheMode.Performs case-insensitive matching against the enum constant names.
Examples:
CacheMode.
parse ("none" );// Returns NONE CacheMode.parse ("WEAK" );// Returns WEAK CacheMode.parse ("Full" );// Returns FULL CacheMode.parse (null );// Returns FULL (default) CacheMode.parse ("invalid" );// Returns FULL (default)
-