Package grails.util
Class CacheEntry<V>
java.lang.Object
grails.util.CacheEntry<V>
- Direct Known Subclasses:
ReloadableResourceBundleMessageSource.PropertiesHolderCacheEntry
Wrapper for a value inside a cache that adds timestamp information
for expiration and prevents "cache storms" with a Lock.
JMM happens-before is ensured with AtomicReference.
Objects in cache are assumed to not change after publication.
- Since:
- 2.3.4
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidexpire()longgetValue()getValue(long timeout, Callable<V> updater, boolean returnExpiredWhileUpdating, Object cacheRequestObject) gets the current value from the entry and updates it if it's older than timeout updater is a callback for creating an updated value.static <K,V> V getValue(ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, Callable<V> updater) static <K,V> V getValue(ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, Callable<V> updater, boolean returnExpiredWhileUpdating) static <K,V> V getValue(ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, Callable<V> updater, Callable<? extends CacheEntry> cacheEntryFactory, boolean returnExpiredWhileUpdating, Object cacheRequestObject) Gets a value from cache.protected VgetValueWhileUpdating(Object cacheRequestObject) protected booleanhasExpired(long timeout, Object cacheRequestObject) booleanprotected voidresetTimestamp(boolean updated) voidsetInitialized(boolean initialized) voidprotected booleanshouldUpdate(long beforeLockingCreatedMillis, Object cacheRequestObject) protected VupdateValue(V oldValue, Callable<V> updater, Object cacheRequestObject)
-
Constructor Details
-
CacheEntry
public CacheEntry() -
CacheEntry
-
-
Method Details
-
getValue
public static <K,V> V getValue(ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, Callable<V> updater, Callable<? extends CacheEntry> cacheEntryFactory, boolean returnExpiredWhileUpdating, Object cacheRequestObject) Gets a value from cache. If the key doesn't exist, it will create the value using the updater callback Prevents cache storms with a lock. The key is always added to the cache. Null return values will also be cached. You can use this together with ConcurrentLinkedHashMap to create a bounded LRU cache- Parameters:
map- the cache mapkey- the key to look uptimeoutMillis- cache entry timeoutupdater- callback to create/update valuecacheEntryFactory- callback to create cache entry, not used in default implementationreturnExpiredWhileUpdating- when true, return expired value while updating new valuecacheRequestObject- context object that gets passed to hasExpired, shouldUpdate and updateValue methods, not used in default implementation- Returns:
- the value
-
getValue
public static <K,V> V getValue(ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, Callable<V> updater) -
getValue
public static <K,V> V getValue(ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, Callable<V> updater, boolean returnExpiredWhileUpdating) -
getValue
-
getValue
public V getValue(long timeout, Callable<V> updater, boolean returnExpiredWhileUpdating, Object cacheRequestObject) gets the current value from the entry and updates it if it's older than timeout updater is a callback for creating an updated value.- Parameters:
timeout-updater-returnExpiredWhileUpdating-cacheRequestObject-- Returns:
- the current value
-
getValueWhileUpdating
-
updateValue
protected V updateValue(V oldValue, Callable<V> updater, Object cacheRequestObject) throws Exception - Throws:
Exception
-
getValue
-
setValue
-
hasExpired
-
shouldUpdate
-
resetTimestamp
protected void resetTimestamp(boolean updated) -
getCreatedMillis
public long getCreatedMillis() -
expire
public void expire() -
isInitialized
public boolean isInitialized() -
setInitialized
public void setInitialized(boolean initialized)
-