Class BackgroundCache<K,​V,​E extends Throwable>


  • public class BackgroundCache<K,​V,​E extends Throwable>
    extends Object

    A cache that is refreshed in the background, implementing only get, put, and size. There is no remove; background cleaning is performed on the underlying map.

    When the system is sitting idle, all cache entries are expired and there is zero overhead. Background refreshes only happen on recently used keys.

    Author:
    AO Industries, Inc.
    • Constructor Detail

      • BackgroundCache

        public BackgroundCache​(String name,
                               Class<? extends E> exceptionClass,
                               long refreshInterval,
                               long expirationAge,
                               Logger logger)
        Parameters:
        name - The name resources are based on, such as background thread names.
        exceptionClass - The class object used to verify exception types are runtime
        refreshInterval - The time between cache entry refreshes
        expirationAge - The time the a cache entry will expire if it has not been accessed, the actual expiration may happen after this time as it is only checked during refreshes.
    • Method Detail

      • stop

        public void stop()
        Stops this cache. This cache should not be used after this is called. Repeated calls to stop are allowed.
      • get

        public BackgroundCache.Result<V,​E> get​(K key)
        Gets a cached result for the given key, null if not cached. Extends the expiration of the cache entry.
      • put

        public BackgroundCache.Result<V,​E> put​(K key,
                                                     BackgroundCache.Refresher<? super K,​? extends V,​? extends E> refresher)
        Runs the refresher immediately to obtain the result, then places an entry into the cache, replacing any existing entry under this key.
        Returns:
        The result obtained from this refresher
      • put

        public void put​(K key,
                        BackgroundCache.Refresher<? super K,​? extends V,​? extends E> refresher,
                        V value)
        Places a result into the cache, replacing any existing entry under this key.
      • put

        public void put​(K key,
                        BackgroundCache.Refresher<? super K,​? extends V,​? extends E> refresher,
                        E exception)
        Places a result into the cache, replacing any existing entry under this key.
      • size

        public int size()
        Gets the current size of the cache.