Package grails.util

Class CacheEntry<V>

java.lang.Object
grails.util.CacheEntry<V>
Direct Known Subclasses:
ReloadableResourceBundleMessageSource.PropertiesHolderCacheEntry

public class CacheEntry<V> extends Object
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
  • Constructor Details

    • CacheEntry

      public CacheEntry()
    • CacheEntry

      public CacheEntry(V value)
  • 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 map
      key - the key to look up
      timeoutMillis - cache entry timeout
      updater - callback to create/update value
      cacheEntryFactory - callback to create cache entry, not used in default implementation
      returnExpiredWhileUpdating - when true, return expired value while updating new value
      cacheRequestObject - 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

      public V getValue(long timeout, Callable<V> updater)
    • 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

      protected V getValueWhileUpdating(Object cacheRequestObject)
    • updateValue

      protected V updateValue(V oldValue, Callable<V> updater, Object cacheRequestObject) throws Exception
      Throws:
      Exception
    • getValue

      public V getValue()
    • setValue

      public void setValue(V val)
    • hasExpired

      protected boolean hasExpired(long timeout, Object cacheRequestObject)
    • shouldUpdate

      protected boolean shouldUpdate(long beforeLockingCreatedMillis, 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)