Class ConcurrentWeakCache<K,V>

java.lang.Object
org.eclipse.aether.util.concurrency.ConcurrentWeakCache<K,V>
Type Parameters:
K - the type of keys
V - the type of values

public class ConcurrentWeakCache<K,V> extends Object
A concurrent cache with weak keys and weak values, inspired by maven-impl's Cache class but stripped down and optimized for hot-path performance.

Design compared to the full Cache:

  • Zero allocation on get() — uses a ThreadLocal reusable lookup key instead of allocating a new wrapper on every read
  • O(1) cleanup per stale entry — uses identity-based removal from the ReferenceQueue instead of a full entrySet().removeIf() scan
  • Lock-free readsConcurrentHashMap.get(Object) is a volatile read with no lock acquisition
  • Lock-striped writes — ConcurrentHashMap uses fine-grained locking

Keys are held via WeakReference: when a key is no longer strongly referenced elsewhere, its entry becomes eligible for garbage collection. Values are also held via WeakReference. Stale entries are cleaned up lazily on put(K, V).

Since:
2.0.19
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new cache with default initial capacity.
    ConcurrentWeakCache(int initialCapacity)
    Creates a new cache with the given initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(K key)
    Returns the value for the given key, or null if the key is not present or the value has been garbage collected.
    void
    put(K key, V value)
    Stores a key-value pair in the cache.
    putIfAbsent(K key, V value)
    If the key is not already present (or its value has been GC'd), stores the key-value pair and returns the given value.
    int
    Returns the number of entries in the cache (including possibly stale ones).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConcurrentWeakCache

      Creates a new cache with default initial capacity.
    • ConcurrentWeakCache

      public ConcurrentWeakCache(int initialCapacity)
      Creates a new cache with the given initial capacity.
      Parameters:
      initialCapacity - the initial capacity
  • Method Details

    • get

      public V get(K key)
      Returns the value for the given key, or null if the key is not present or the value has been garbage collected.

      This method is lock-free and allocates no objects.

      Parameters:
      key - the key to look up
      Returns:
      the cached value, or null
    • put

      public void put(K key, V value)
      Stores a key-value pair in the cache. Both key and value are held via weak references.

      Also performs lazy cleanup of entries whose keys have been garbage collected.

      Parameters:
      key - the key
      value - the value
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      If the key is not already present (or its value has been GC'd), stores the key-value pair and returns the given value. If the key is already present with a live value, returns the existing value without storing. Concurrent callers for the same key are guaranteed to receive the same instance (the insert-or-keep decision is atomic per key via ConcurrentHashMap.merge(K, V, BiFunction)).

      Also performs lazy cleanup of entries whose keys have been garbage collected.

      Parameters:
      key - the key
      value - the value to store if absent
      Returns:
      the existing value if present, or the given value if newly stored
    • size

      public int size()
      Returns the number of entries in the cache (including possibly stale ones).
      Returns:
      the cache size