Class ConcurrentSizedTimeoutCache<K,V>

java.lang.Object
com.hypixel.hytale.server.worldgen.util.cache.ConcurrentSizedTimeoutCache<K,V>
Type Parameters:
K - Type of the cache key.
V - Type of the cache value.
All Implemented Interfaces:
Cache<K,V>

public class ConcurrentSizedTimeoutCache<K,V> extends Object implements Cache<K,V>
This sized timeout cache implementation aims to provide improved throughput under high concurrency. It achieves this by partitioning the cache into multiple buckets, each protected by its own multi-reader/single-writer lock. Entries are spread deterministically across the buckets (based on the key's hash) to reduce contention over write-locks when multiple threads need to insert values for different keys at the same time. Cache entries are timestamped upon access, and evicted if they have not been accessed within a specified timeout period. The cache attempts to maintain a maximum size by periodically trimming each bucket when it exceeds its target capacity.
  • Constructor Details

    • ConcurrentSizedTimeoutCache

      public ConcurrentSizedTimeoutCache(int capacity, int concurrencyLevel, long timeout, @Nonnull TimeUnit timeoutUnit, @Nonnull Function<K,K> computeKey, @Nonnull Function<K,V> computeValue, @Nullable BiConsumer<K,V> destroyer)
      Constructs a new ConcurrentTimeoutCache instance.
      Parameters:
      capacity - Total capacity of the cache.
      concurrencyLevel - Expected number of concurrent threads accessing the cache.
      timeout - Duration after which entries should be considered expired if not accessed.
      timeoutUnit - Unit of time for the timeout parameter.
      computeKey - Function for allocating the copy of a key to store in the cache (allowing queries to reuse a local key instance).
      computeValue - Function for allocating and computing the value to store in the cache for a given key.
      destroyer - (Optional) Callback function invoked when a key-value pair is evicted from the cache, allowing for resource cleanup.
  • Method Details

    • shutdown

      public void shutdown()
      Specified by:
      shutdown in interface Cache<K,V>
    • cleanup

      public void cleanup()
      Specified by:
      cleanup in interface Cache<K,V>
    • get

      @Nonnull public V get(K key)
      Specified by:
      get in interface Cache<K,V>