Package com.axelor.cache
Class TenantAwareCache<K,V>
java.lang.Object
com.axelor.cache.TenantAwareCache<K,V>
- Type Parameters:
K- the type of keys maintained by this cacheV- the type of mapped values
- All Implemented Interfaces:
AxelorCache<K,,V> Closeable,AutoCloseable,Iterable<Map.Entry<K,V>>
- Direct Known Subclasses:
TenantAwareDistributedCache
An
AxelorCache implementation that delegates to tenant-specific cache instances.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTenantAwareCache(com.github.benmanes.caffeine.cache.CacheLoader<String, AxelorCache<K, V>> cacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<String, AxelorCache<K, V>> removalListener) TenantAwareCache(Function<String, AxelorCache<K, V>> cacheFactory) -
Method Summary
Modifier and TypeMethodDescriptionasMap()Returns a view of the entries stored in this cache as a thread-safe map.voidcleanUp()Performs any pending maintenance operations needed by the cache.booleanClears the time to live.voidclose()Closes all underlying tenant-specific caches.longReturns the approximate number of entries in this cache.booleanSets a time to live.Returns the value associated with thekeyin this cache, obtaining that value from theCacheLoaderif defined and if necessary, otherwise returnsnullif there is no cached value for thekey.Returns the value associated with thekeyin this cache, obtaining that value from themappingFunctionif necessary.Returns a map of the values associated with thekeysin this cache, usingCacheLoaderif defined and if necessary.Returns key-specific lock for this cache.voidinvalidate(K key) Discards any cached value for thekey.voidDiscards all cached values.iterator()Returns an iterator over the entries in this cache.voidAssociates thevaluewith thekey.voidCopies all of the mappings from the specified map to the cache.longReturns the remaining time to live in milliseconds.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
TenantAwareCache
-
TenantAwareCache
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 Details
-
close
public void close()Closes all underlying tenant-specific caches.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceAxelorCache<K,V> - Specified by:
closein interfaceCloseable
-
get
Description copied from interface:AxelorCacheReturns the value associated with thekeyin this cache, obtaining that value from theCacheLoaderif defined and if necessary, otherwise returnsnullif there is no cached value for thekey.- Specified by:
getin interfaceAxelorCache<K,V> - Parameters:
key- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
nullif this map contains no mapping for the key
-
get
Description copied from interface:AxelorCacheReturns the value associated with thekeyin this cache, obtaining that value from themappingFunctionif necessary.Note that the
mappingFunctionis called only if there is no cached value after calling the cache loader if defined.- Specified by:
getin interfaceAxelorCache<K,V> - Parameters:
key- the key with which the specified value is to be associatedmappingFunction- 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
Description copied from interface:AxelorCacheReturns a map of the values associated with thekeysin this cache, usingCacheLoaderif defined and if necessary.- Specified by:
getAllin interfaceAxelorCache<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
Description copied from interface:AxelorCacheAssociates thevaluewith thekey.- Specified by:
putin interfaceAxelorCache<K,V> - Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key
-
putAll
Description copied from interface:AxelorCacheCopies 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 fromkeytovaluein the specified map.- Specified by:
putAllin interfaceAxelorCache<K,V> - Parameters:
map- the mappings to be stored in this cache
-
invalidate
Description copied from interface:AxelorCacheDiscards any cached value for thekey.- Specified by:
invalidatein interfaceAxelorCache<K,V> - Parameters:
key- the key whose mapping is to be removed from the cache
-
invalidateAll
public void invalidateAll()Description copied from interface:AxelorCacheDiscards all cached values.- Specified by:
invalidateAllin interfaceAxelorCache<K,V>
-
estimatedSize
public long estimatedSize()Description copied from interface:AxelorCacheReturns the approximate number of entries in this cache. The actual count may differ because of concurrent updates and pending invalidations.- Specified by:
estimatedSizein interfaceAxelorCache<K,V> - Returns:
- the estimated size of the cache
-
asMap
Description copied from interface:AxelorCacheReturns 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:
asMapin interfaceAxelorCache<K,V> - Returns:
- a thread-safe view of this cache supporting
ConcurrentMapoperations
-
iterator
Description copied from interface:AxelorCacheReturns an iterator over the entries in this cache. -
cleanUp
public void cleanUp()Description copied from interface:AxelorCachePerforms any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.- Specified by:
cleanUpin interfaceAxelorCache<K,V>
-
getLock
Description copied from interface:AxelorCacheReturns key-specific lock for this cache.- Specified by:
getLockin interfaceAxelorCache<K,V> - Parameters:
key-- Returns:
- reentrant lock
-
expire
Description copied from interface:AxelorCacheSets a time to live.- Specified by:
expirein interfaceAxelorCache<K,V> - Parameters:
ttl- time to live- Returns:
trueif the time to live was set
-
clearExpire
public boolean clearExpire()Description copied from interface:AxelorCacheClears the time to live.- Specified by:
clearExpirein interfaceAxelorCache<K,V> - Returns:
trueif the time to live was removed
-
remainTimeToLive
public long remainTimeToLive()Description copied from interface:AxelorCacheReturns the remaining time to live in milliseconds.- Specified by:
remainTimeToLivein interfaceAxelorCache<K,V> - Returns:
- remaining time to live in milliseconds, or a negative value if there is no time to live
-