Class KeyedObjectCache<KEY_TYPE,VALUE_TYPE>

java.lang.Object
io.deephaven.base.cache.KeyedObjectCache<KEY_TYPE,VALUE_TYPE>

public class KeyedObjectCache<KEY_TYPE,VALUE_TYPE> extends Object
The central idea is that we can use an open-addressed map as a bounded cache with concurrent get and synchronized put access. Rather than rely on expensive and/or concurrency-destroying bookkeeping schemes to allow "smart" cache replacement, we rely on the assumption that our hash function and probe sequence computation does a fairly good job of distributing keyed objects that have a high likelihood of being useful to cache during overlapping timeframes. We never remove anything from the cache without replacing it with a new item. A callback is accepted to allow for item resource cleanup upon eviction from the cache. The impact of collisions (for the bucket an item hashes to, or any other bucket in its associated probe sequence) is mitigated by randomized eviction of a victim item in a probe sequence of bounded length. Note that, unlike common open-addressed hashing schemes, we're unconcerned with load factor - we have an explicitly bounded capacity, and explicitly bounded probe sequence length, which must be tuned for the workload in question.
  • Constructor Details

    • KeyedObjectCache

      public KeyedObjectCache(int capacity, int probeSequenceLength, io.deephaven.hash.KeyedObjectKey<KEY_TYPE,VALUE_TYPE> keyDefinition, @Nullable @Nullable Consumer<VALUE_TYPE> postEvictionProcedure, Random random)
      Parameters:
      capacity - Lower bound on maximum capacity. Rounded up to to a prime number.
      probeSequenceLength - Lower bound on number of slots to probe (inclusive of the one a key hashes directly to). Rounded up to a power of 2.
      keyDefinition - The key definition
      postEvictionProcedure - Optional. Invoked without any extra synchronization.
      random - Pseudo-random number generator
  • Method Details

    • getCapacity

      public final int getCapacity()
    • get

      public final VALUE_TYPE get(KEY_TYPE key)
      Lookup an item by key.
      Parameters:
      key - The key
      Returns:
      The associated value, or null if none is present.
    • putIfAbsent

      public final VALUE_TYPE putIfAbsent(VALUE_TYPE value)
      Add an item to the cache if it's not already present.
      Parameters:
      value - The value, from which the key will be derived using the key definition.
      Returns:
      The equal (by key) item already present, or value if no such item was found.