Enum Class CacheMode

java.lang.Object
java.lang.Enum<CacheMode>
org.apache.juneau.commons.collections.CacheMode
All Implemented Interfaces:
Serializable, Comparable<CacheMode>, Constable

public enum CacheMode extends Enum<CacheMode>
Cache modes for Cache and related cache classes.

Defines how cache storage is implemented and when cached entries may be evicted.

See Also:
  • Enum Constant Details

    • NONE

      public static final CacheMode 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) or Cache.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

      public static final CacheMode WEAK
      Weak caching - uses WeakHashMap for 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., Class objects)
      • 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
    • FULL

      public static final CacheMode FULL
      Full caching - uses ConcurrentHashMap for 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

      public static CacheMode[] 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

      public static CacheMode valueOf(String name)
      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 name
      NullPointerException - if the argument is null
    • parse

      public static CacheMode parse(String value)
      Parses a string value into a CacheMode.

      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)

      Parameters:
      value - The string value to parse. Can be null.
      Returns:
      The corresponding CacheMode, or FULL if the value is null or invalid.