Class CacheUtils

java.lang.Object
google.registry.model.CacheUtils

public class CacheUtils extends Object
Utility methods related to caching database entities.
  • Constructor Details

    • CacheUtils

      public CacheUtils()
  • Method Details

    • 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 the database and the caches need to be refreshed.)

    • tryMemoizeWithExpiration

      public static <T> com.google.common.base.Supplier<T> tryMemoizeWithExpiration(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<Object,Object> newCacheBuilder()
      Creates and returns a new Caffeine builder.
    • newCacheBuilder

      public static com.github.benmanes.caffeine.cache.Caffeine<Object,Object> newCacheBuilder(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.