Class CacheBuilder<V extends Serializable>

  • Type Parameters:
    V - the base value type for all caches created by this builder

    public abstract class CacheBuilder<V extends Serializable>
    extends Object
    A builder of LoadingCache and Cache instances.

    By default, this class uses a proxy class to provide the LoadingCache implementation out of the Cache implementation. Applications must implement the build(String) method to provide this latter and override the build(String, CacheLoader) method only if a behavior other than the proxy is required. It can either use a standard Guava implementation, or an application specific cache.

    Since:
    1.12.0
    See Also:
    CacheFactory
    • Constructor Detail

      • CacheBuilder

        public CacheBuilder()
    • Method Detail

      • createCleanableCache

        public static <V extends Serializable> com.google.common.cache.Cache<String,​V> createCleanableCache​(com.google.common.cache.Cache<String,​V> cache)
        Call this method during Cache creation to make a Cache whose expired entries are removed every time a new entry is put or invalidated. Expired entries are removed in a separate thread after the regular method call. Threads are created using the factory provided by Threads.getThreadFactory().
        Type Parameters:
        V - the cache entry value type.
        Parameters:
        cache - the cache to convert.
        Returns:
        a Cache whose expired entries are removed every time a new entry is put or invalidated.
        Throws:
        NullPointerException - if cache is null.
        Since:
        3.3.0
      • expireAfterWrite

        public CacheBuilder<V> expireAfterWrite​(long duration,
                                                TimeUnit unit)
        Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
        Parameters:
        duration - the length of time after an entry is created that it should be automatically removed
        unit - the unit that duration is expressed in
        Returns:
        this cache builder
      • build

        public abstract com.google.common.cache.Cache<String,​V> build​(String title)
        Builds a synchronous cache which does not automatically load values when keys are requested.

        Consider build(String, CacheLoader) instead, if it is feasible to implement a CacheLoader.

        Parameters:
        title - the cache title
        Returns:
        a cache having the requested features
      • build

        public com.google.common.cache.LoadingCache<String,​V> build​(String title,
                                                                          com.google.common.cache.CacheLoader<String,​V> loader)
        Creates a synchronous cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader.
        Parameters:
        title - the cache title
        loader - the cache loader used to obtain new values
        Returns:
        a cache having the requested features
      • getExpireAfterWriteNanos

        protected long getExpireAfterWriteNanos()
        Returns the duration in nanoseconds after an entry is created that it should be automatically removed.
        Returns:
        the duration in nanoseconds after an entry is created that it should be automatically removed