Class UsageTrackingCache<K,​V>

  • Type Parameters:
    K - The type of key by which cache entries can be indexed. This must implement Object.hashCode() and Object.equals(Object).
    V - The type of entry being cached.

    public class UsageTrackingCache<K,​V>
    extends Object
    A cache that keep things until they haven't been used for a given duration. Things will be kept in the cache until they have been inactive for too long. Things will not be dropped from the cache while they are active, no matter how long that is.
    • Method Detail

      • getAndIncrementUsage

        @CheckForNull
        public V getAndIncrementUsage​(@NonNull
                                      K key)
        Looks up an existing entry in the cache. If it finds an entry then it returns the entry and increments the usage count for that entry, and the caller MUST ensure that decrementUsage(Object) is later called on the result. If it doesn't find an entry in the cache then it returns null (and the caller will most likely decide to call cacheAndIncrementUsage(Object, Object) ).
        Parameters:
        key - The key used to look up the entry in the cache.
        Returns:
        An existing cache entry, or null.
      • cacheAndIncrementUsage

        public void cacheAndIncrementUsage​(@NonNull
                                           K key,
                                           @NonNull
                                           V entry)
        Puts an entry in the cache with a usage count of 1. The caller MUST ensure that decrementUsage(Object) is later called on the entry that has been cached.
        Parameters:
        key - The key used to look up the entry in the cache.
        entry - The entry to be cached.
      • decrementUsage

        public void decrementUsage​(@NonNull
                                   V entry)
        Decrements the usage count on a cache entry, potentially removing it from active usage. This method MUST be called once and only once for every time that getAndIncrementUsage(Object) returned a non-null value and for every time that cacheAndIncrementUsage(Object, Object) was called.
        Parameters:
        entry - The entry that is no longer in use.