Package grails.util
Class CacheEntry<V>
- java.lang.Object
-
- grails.util.CacheEntry<V>
-
- Direct Known Subclasses:
ReloadableResourceBundleMessageSource.PropertiesHolderCacheEntry
public class CacheEntry<V> extends java.lang.ObjectWrapper 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 Modifier and Type Class Description static classCacheEntry.UpdateException
-
Constructor Summary
Constructors Constructor Description CacheEntry()CacheEntry(V value)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexpire()longgetCreatedMillis()VgetValue()VgetValue(long timeout, java.util.concurrent.Callable<V> updater)VgetValue(long timeout, java.util.concurrent.Callable<V> updater, boolean returnExpiredWhileUpdating, java.lang.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>
VgetValue(java.util.concurrent.ConcurrentMap<K,CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater)static <K,V>
VgetValue(java.util.concurrent.ConcurrentMap<K,CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater, boolean returnExpiredWhileUpdating)static <K,V>
VgetValue(java.util.concurrent.ConcurrentMap<K,CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater, java.util.concurrent.Callable<? extends CacheEntry> cacheEntryFactory, boolean returnExpiredWhileUpdating, java.lang.Object cacheRequestObject)Gets a value from cache.protected VgetValueWhileUpdating(java.lang.Object cacheRequestObject)protected booleanhasExpired(long timeout, java.lang.Object cacheRequestObject)booleanisInitialized()protected voidresetTimestamp(boolean updated)voidsetInitialized(boolean initialized)voidsetValue(V val)protected booleanshouldUpdate(long beforeLockingCreatedMillis, java.lang.Object cacheRequestObject)protected VupdateValue(V oldValue, java.util.concurrent.Callable<V> updater, java.lang.Object cacheRequestObject)
-
-
-
Constructor Detail
-
CacheEntry
public CacheEntry()
-
CacheEntry
public CacheEntry(V value)
-
-
Method Detail
-
getValue
public static <K,V> V getValue(java.util.concurrent.ConcurrentMap<K,CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater, java.util.concurrent.Callable<? extends CacheEntry> cacheEntryFactory, boolean returnExpiredWhileUpdating, java.lang.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 valuecacheEntryClass- CacheEntry implementation class to usereturnExpiredWhileUpdating- 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:
-
getValue
public static <K,V> V getValue(java.util.concurrent.ConcurrentMap<K,CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater)
-
getValue
public static <K,V> V getValue(java.util.concurrent.ConcurrentMap<K,CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater, boolean returnExpiredWhileUpdating)
-
getValue
public V getValue(long timeout, java.util.concurrent.Callable<V> updater, boolean returnExpiredWhileUpdating, java.lang.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
protected V getValueWhileUpdating(java.lang.Object cacheRequestObject)
-
updateValue
protected V updateValue(V oldValue, java.util.concurrent.Callable<V> updater, java.lang.Object cacheRequestObject) throws java.lang.Exception
- Throws:
java.lang.Exception
-
getValue
public V getValue()
-
setValue
public void setValue(V val)
-
hasExpired
protected boolean hasExpired(long timeout, java.lang.Object cacheRequestObject)
-
shouldUpdate
protected boolean shouldUpdate(long beforeLockingCreatedMillis, java.lang.Object cacheRequestObject)
-
resetTimestamp
protected void resetTimestamp(boolean updated)
-
getCreatedMillis
public long getCreatedMillis()
-
expire
public void expire()
-
isInitialized
public boolean isInitialized()
-
setInitialized
public void setInitialized(boolean initialized)
-
-