Class TenantAwareCache<K,V>

java.lang.Object
com.axelor.cache.TenantAwareCache<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values
All Implemented Interfaces:
AxelorCache<K,V>, Closeable, AutoCloseable, Iterable<Map.Entry<K,V>>
Direct Known Subclasses:
TenantAwareDistributedCache

public class TenantAwareCache<K,V> extends Object implements AxelorCache<K,V>
An AxelorCache implementation that delegates to tenant-specific cache instances.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    TenantAwareCache(com.github.benmanes.caffeine.cache.CacheLoader<String,AxelorCache<K,V>> cacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<String,AxelorCache<K,V>> removalListener)
     
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a view of the entries stored in this cache as a thread-safe map.
    void
    Performs any pending maintenance operations needed by the cache.
    boolean
    Clears the time to live.
    void
    Closes all underlying tenant-specific caches.
    long
    Returns the approximate number of entries in this cache.
    boolean
    Sets a time to live.
    get(K key)
    Returns the value associated with the key in this cache, obtaining that value from the CacheLoader if defined and if necessary, otherwise returns null if there is no cached value for the key.
    get(K key, Function<? super K,? extends V> mappingFunction)
    Returns the value associated with the key in this cache, obtaining that value from the mappingFunction if necessary.
    getAll(Set<K> keys)
    Returns a map of the values associated with the keys in this cache, using CacheLoader if defined and if necessary.
    getLock(K key)
    Returns key-specific lock for this cache.
    void
    Discards any cached value for the key.
    void
    Discards all cached values.
    Returns an iterator over the entries in this cache.
    void
    put(K key, V value)
    Associates the value with the key.
    void
    putAll(Map<? extends K,? extends V> map)
    Copies all of the mappings from the specified map to the cache.
    long
    Returns the remaining time to live in milliseconds.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

  • Method Details

    • close

      public void close()
      Closes all underlying tenant-specific caches.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface AxelorCache<K,V>
      Specified by:
      close in interface Closeable
    • get

      public V get(K key)
      Description copied from interface: AxelorCache
      Returns the value associated with the key in this cache, obtaining that value from the CacheLoader if defined and if necessary, otherwise returns null if there is no cached value for the key.
      Specified by:
      get in interface AxelorCache<K,V>
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or null if this map contains no mapping for the key
    • get

      public V get(K key, Function<? super K,? extends V> mappingFunction)
      Description copied from interface: AxelorCache
      Returns the value associated with the key in this cache, obtaining that value from the mappingFunction if necessary.

      Note that the mappingFunction is called only if there is no cached value after calling the cache loader if defined.

      Specified by:
      get in interface AxelorCache<K,V>
      Parameters:
      key - the key with which the specified value is to be associated
      mappingFunction - the function to compute a value
      Returns:
      the current (existing or computed) value associated with the specified key, or null if the computed value is null
    • getAll

      public Map<K,V> getAll(Set<K> keys)
      Description copied from interface: AxelorCache
      Returns a map of the values associated with the keys in this cache, using CacheLoader if defined and if necessary.
      Specified by:
      getAll in interface AxelorCache<K,V>
      Parameters:
      keys - the keys whose associated values are to be returned
      Returns:
      an unmodifiable mapping of keys to values for the specified keys in this cache
    • put

      public void put(K key, V value)
      Description copied from interface: AxelorCache
      Associates the value with the key.
      Specified by:
      put in interface AxelorCache<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      Description copied from interface: AxelorCache
      Copies all of the mappings from the specified map to the cache.

      The effect of this call is equivalent to that of calling put(key, value) on this map once for each mapping from key to value in the specified map.

      Specified by:
      putAll in interface AxelorCache<K,V>
      Parameters:
      map - the mappings to be stored in this cache
    • invalidate

      public void invalidate(K key)
      Description copied from interface: AxelorCache
      Discards any cached value for the key.
      Specified by:
      invalidate in interface AxelorCache<K,V>
      Parameters:
      key - the key whose mapping is to be removed from the cache
    • invalidateAll

      public void invalidateAll()
      Description copied from interface: AxelorCache
      Discards all cached values.
      Specified by:
      invalidateAll in interface AxelorCache<K,V>
    • estimatedSize

      public long estimatedSize()
      Description copied from interface: AxelorCache
      Returns the approximate number of entries in this cache. The actual count may differ because of concurrent updates and pending invalidations.
      Specified by:
      estimatedSize in interface AxelorCache<K,V>
      Returns:
      the estimated size of the cache
    • asMap

      public ConcurrentMap<K,V> asMap()
      Description copied from interface: AxelorCache
      Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.

      Note that if the cache has a cache loader, it will be used. This differs from Caffeine's Cache#asMap() and is designed to match Redisson RMap behavior.

      In case of tenant-aware cache, the returned map view is for current tenant: you must use this on demand for correct tenant resolution, not on static initialization.

      Specified by:
      asMap in interface AxelorCache<K,V>
      Returns:
      a thread-safe view of this cache supporting ConcurrentMap operations
    • iterator

      public Iterator<Map.Entry<K,V>> iterator()
      Description copied from interface: AxelorCache
      Returns an iterator over the entries in this cache.
      Specified by:
      iterator in interface AxelorCache<K,V>
      Specified by:
      iterator in interface Iterable<K>
      Returns:
      an iterator over the entries in this cache
    • cleanUp

      public void cleanUp()
      Description copied from interface: AxelorCache
      Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.
      Specified by:
      cleanUp in interface AxelorCache<K,V>
    • getLock

      public Lock getLock(K key)
      Description copied from interface: AxelorCache
      Returns key-specific lock for this cache.
      Specified by:
      getLock in interface AxelorCache<K,V>
      Parameters:
      key -
      Returns:
      reentrant lock
    • expire

      public boolean expire(Duration ttl)
      Description copied from interface: AxelorCache
      Sets a time to live.
      Specified by:
      expire in interface AxelorCache<K,V>
      Parameters:
      ttl - time to live
      Returns:
      true if the time to live was set
    • clearExpire

      public boolean clearExpire()
      Description copied from interface: AxelorCache
      Clears the time to live.
      Specified by:
      clearExpire in interface AxelorCache<K,V>
      Returns:
      true if the time to live was removed
    • remainTimeToLive

      public long remainTimeToLive()
      Description copied from interface: AxelorCache
      Returns the remaining time to live in milliseconds.
      Specified by:
      remainTimeToLive in interface AxelorCache<K,V>
      Returns:
      remaining time to live in milliseconds, or a negative value if there is no time to live