Class WeakCache<K,V>

java.lang.Object
dev.engine.core.resource.WeakCache<K,V>
Type Parameters:
K - the key type (e.g., MeshData, TextureData)
V - the value type (e.g., MeshHandle, Handle<TextureResource>)

public class WeakCache<K,V> extends Object
Identity-based cache with weak reference keys and automatic cleanup.

Maps CPU-side data objects to GPU-side resources using identity semantics. When the CPU-side data is garbage collected, the associated GPU resource is queued for cleanup on the next pollStale(Consumer) call.

Lookups are O(1) via identity-hashed weak references.

Thread safety: intended for single-thread access (the render thread). The GC may enqueue references from any thread, but pollStale(Consumer) is called from the render thread only.

  • Constructor Details

    • WeakCache

      public WeakCache()
  • Method Details

    • getOrCreate

      public V getOrCreate(K key, Function<K,V> factory)
      Gets or creates a cached value for the given key. Uses identity (==) comparison, not equals(). O(1) lookup via identity hash.
    • pollStale

      public void pollStale(Consumer<V> cleanup)
      Polls for stale entries (keys that were garbage collected). Calls the cleanup consumer for each orphaned value. Should be called once per frame.
    • size

      public int size()
      Returns the number of live entries in the cache.
    • values

      public Iterable<V> values()
      Returns all live values in the cache.
    • clear

      public void clear(Consumer<V> cleanup)
      Drains all entries, calling cleanup on each value.