Class CacheBuilder<K,V>

java.lang.Object
com.axelor.cache.CacheBuilder<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values
Direct Known Subclasses:
AbstractRedissonCacheBuilder, CaffeineCacheBuilder

public abstract class CacheBuilder<K,V> extends Object
A builder of AxelorCache instances
  • Constructor Details

    • CacheBuilder

      protected CacheBuilder(String cacheName)
    • CacheBuilder

      protected CacheBuilder(CacheBuilder<K,V> builder)
  • Method Details

    • getCacheType

      public static CacheType getCacheType()
    • newBuilder

      public static <K, V> CacheBuilder<K,V> newBuilder(String name)
      Constructs a new CacheBuilder instance.

      The caller class is used together with the specified name as cache name.

      The cache name is used to create a globally unique cache name, depending on the cache provider.

      Type Parameters:
      K - the key type of the cache
      V - the value type of the cache
      Parameters:
      name - the name used as suffix of the cache name
      Returns:
      a new CacheBuilder instance
    • newInMemoryBuilder

      public static <K, V> CacheBuilder<K,V> newInMemoryBuilder()
      Constructs a new CacheBuilder instance for an in-memory cache.

      This currently uses Caffeine as the in-memory cache provider.

      Type Parameters:
      K - the key type of the cache
      V - the value type of the cache
      Returns:
      a new CacheBuilder instance
    • fromCacheName

      protected static <K, V> CacheBuilder<K,V> fromCacheName(String name)
      Constructs a new CacheBuilder instance for the specified cache name.

      The cache name is used to create a globally unique cache name, depending on the cache provider.

      Type Parameters:
      K - the key type of the cache
      V - the value type of the cache
      Parameters:
      name - the name of the cache
      Returns:
      a new CacheBuilder instance
    • getCacheProviderInfo

      public static CacheProviderInfo getCacheProviderInfo()
      Returns information about the current cache provider configuration.
      Returns:
      the cache provider information
    • getCacheName

      protected String getCacheName()
    • getMaximumSize

      protected int getMaximumSize()
    • maximumSize

      public CacheBuilder<K,V> maximumSize(int maximumSize)
      Sets the maximum number of entries the cache may contain.

      Depending on the cache provider, this may not be supported and approximation techniques may be used.

      Parameters:
      maximumSize - the maximum size of the cache
      Returns:
      this CacheBuilder instance (for chaining)
    • getExpireAfterWrite

      protected Duration getExpireAfterWrite()
    • expireAfterWrite

      public CacheBuilder<K,V> expireAfterWrite(Duration expireAfterWrite)
      Sets the duration after which cache entries will expire following the last write.
      Parameters:
      expireAfterWrite - the duration after which entries will expire following the last write
      Returns:
      this CacheBuilder instance (for chaining)
    • getExpireAfterAccess

      protected Duration getExpireAfterAccess()
    • expireAfterAccess

      public CacheBuilder<K,V> expireAfterAccess(Duration expireAfterAccess)
      Sets the duration after which cache entries will expire following the last access.
      Parameters:
      expireAfterAccess - the duration after which entries will expire following the last access
      Returns:
      this CacheBuilder instance (for chaining)
    • isWeakKeys

      protected boolean isWeakKeys()
    • weakKeys

      public CacheBuilder<K,V> weakKeys()
      Specifies that the cache should use weak references for keys.

      Depending on the cache provider, this may not be supported and approximation techniques may be used.

      Returns:
      this CacheBuilder instance (for chaining)
    • isWeakValues

      protected boolean isWeakValues()
    • weakValues

      public CacheBuilder<K,V> weakValues()
      Specifies that the cache should use weak references for values.

      Depending on the cache provider, this may not be supported and approximation techniques may be used.

      Returns:
      this CacheBuilder instance (for chaining)
    • getRemovalListener

      protected <K1 extends K, V1 extends V> RemovalListener<K1,V1> getRemovalListener()
    • removalListener

      public <K1 extends K, V1 extends V> CacheBuilder<K1,V1> removalListener(RemovalListener<? super K1,? super V1> removalListener)
      Specifies a listener instance that caches should notify each time an entry is removed for any reason.
      Parameters:
      removalListener - the listener instance
      Returns:
      this CacheBuilder instance (for chaining)
    • build

      public abstract <K1 extends K, V1 extends V> AxelorCache<K1,V1> build()
      Builds an AxelorCache which does not automatically load values when keys are requested.
      Type Parameters:
      K1 - the key type of the cache
      V1 - the value type of the cache
      Returns:
      a new AxelorCache instance having the specified configuration
    • build

      public abstract <K1 extends K, V1 extends V> AxelorCache<K1,V1> build(CacheLoader<? super K1,V1> loader)
      Builds an AxelorCache which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader.
      Type Parameters:
      K1 - the key type of the cache
      V1 - the value type of the cache
      Parameters:
      loader - the CacheLoader used to obtain new values
      Returns:
      a new AxelorCache instance having the specified configuration and using the specified loader