Class CacheUtils


  • public class CacheUtils
    extends java.lang.Object
    Utility methods related to caching Datastore entities.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CacheUtils.AppEngineEnvironmentCacheLoader<K,​V>
      A CacheLoader that automatically masquerade the background thread where the refresh action runs in to be an GAE thread.
    • Constructor Summary

      Constructors 
      Constructor Description
      CacheUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> com.google.common.base.Supplier<T> memoizeWithShortExpiration​(com.google.common.base.Supplier<T> original)
      Memoize a supplier, with a short expiration specified in the environment config.
      static com.github.benmanes.caffeine.cache.Caffeine<java.lang.Object,​java.lang.Object> newCacheBuilder()
      Creates and returns a new Caffeine builder.
      static com.github.benmanes.caffeine.cache.Caffeine<java.lang.Object,​java.lang.Object> newCacheBuilder​(java.time.Duration expireAfterWrite)
      Creates and returns a new Caffeine builder with the specified cache expiration.
      static <T> com.google.common.base.Supplier<T> tryMemoizeWithExpiration​(java.time.Duration expiration, com.google.common.base.Supplier<T> original)
      Memoize a supplier with the given expiration.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CacheUtils

        public CacheUtils()
    • Method Detail

      • memoizeWithShortExpiration

        public static <T> com.google.common.base.Supplier<T> memoizeWithShortExpiration​(com.google.common.base.Supplier<T> original)
        Memoize a supplier, with a short expiration specified in the environment config.

        Use this for things that might change while code is running. (For example, the various lists downloaded from the TMCH get updated in Datastore and the caches need to be refreshed.)

      • tryMemoizeWithExpiration

        public static <T> com.google.common.base.Supplier<T> tryMemoizeWithExpiration​(java.time.Duration expiration,
                                                                                      com.google.common.base.Supplier<T> original)
        Memoize a supplier with the given expiration. If the expiration is zero(likely happens in a unit test), it returns the original supplier.
      • newCacheBuilder

        public static com.github.benmanes.caffeine.cache.Caffeine<java.lang.Object,​java.lang.Object> newCacheBuilder()
        Creates and returns a new Caffeine builder.
      • newCacheBuilder

        public static com.github.benmanes.caffeine.cache.Caffeine<java.lang.Object,​java.lang.Object> newCacheBuilder​(java.time.Duration expireAfterWrite)
        Creates and returns a new Caffeine builder with the specified cache expiration.

        This also sets the refresh duration to half of the cache expiration. The resultant behavior is that a cache entry is eligible to be asynchronously refreshed after access once more than half of its cache duration has elapsed, and then it is synchronously refreshed (blocking the read) once its full cache duration has elapsed. So you will never get data older than the cache expiration, but for frequently accessed keys it will be refreshed more often than that and the cost of the load will never be incurred during the read.