Class SingleItemCache<T>

java.lang.Object
com.peterphi.std.guice.common.cached.util.SingleItemCache<T>
Type Parameters:
T - the type of value cached

public class SingleItemCache<T> extends Object
Wrapper around a single item guava cache as a workaround for google not implementing the request for a single item cache made in 2012.

This is a light wrapper around Cache

  • Constructor Summary

    Constructors
    Constructor
    Description
    SingleItemCache(com.google.common.cache.CacheBuilder builder, Callable<T> supplier)
    Create a new cache, caching the result of supplier using a cache built by builder.
    SingleItemCache(com.google.common.cache.CacheBuilder builder, Callable<T> supplier, boolean useCacheManager)
    Create a new cache, caching the result of supplier using a cache built by builder.
    SingleItemCache(Callable<T> supplier, Timeout validity)
    Create a new cache, caching the result of supplier for a maximum of validity.
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
    Return the cached value (or regenerate it).
    Get the value if it's sitting in the cache, otherwise return null
     
    void
    Invalidate any cached result, such that subsequent calls to get() will re-generate the value
    void
     
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SingleItemCache

      public SingleItemCache(Callable<T> supplier, Timeout validity)
      Create a new cache, caching the result of supplier for a maximum of validity. A new Cache will be constructed using CacheBuilder
      Parameters:
      supplier - a function that generates some cache-worthy result; see get() for information on how/when this method is called. The supplier MUST NOT return null
      validity - the time after write that the cached result from supplier should expire
    • SingleItemCache

      public SingleItemCache(com.google.common.cache.CacheBuilder builder, Callable<T> supplier)
      Create a new cache, caching the result of supplier using a cache built by builder.
      Parameters:
      builder - the cache builder; will be largely untouched, although the maximum size will be set to 1 (since the cache will only store a single item)
      supplier - a function that generates some cache-worthy result; see get() for information on how/when this method is called. The supplier MUST NOT return null
    • SingleItemCache

      public SingleItemCache(com.google.common.cache.CacheBuilder builder, Callable<T> supplier, boolean useCacheManager)
      Create a new cache, caching the result of supplier using a cache built by builder.
      Parameters:
      builder - the cache builder; will be largely untouched, although the maximum size will be set to 1 (since the cache will only store a single item)
      supplier - a function that generates some cache-worthy result; see get() for information on how/when this method is called. The supplier MUST NOT return null
      useCacheManager - if true, will not be registered with the CacheManager to allow cache to be cleared manually
  • Method Details

    • getIfPresent

      public T getIfPresent()
      Get the value if it's sitting in the cache, otherwise return null
      Returns:
      the value from the cache, or null if no value was cached
      See Also:
      • Cache.getIfPresent(Object)
    • get

      public T get()
      Return the cached value (or regenerate it).
      Returns:
      the cached result, or a newly-generated value if the cache had expired (or had been actively invalidated)
      Throws:
      RuntimeException - if the supplier function passed to the constructor throws
      See Also:
      • Cache.get(Object, Callable)
    • invalidate

      public void invalidate()
      Invalidate any cached result, such that subsequent calls to get() will re-generate the value
      See Also:
      • Cache.invalidateAll()
    • setName

      public void setName(String name)
    • getName

      public String getName()
    • toString

      public String toString()
      Overrides:
      toString in class Object