Package grails.util

Class CacheEntry<V>

  • Direct Known Subclasses:
    ReloadableResourceBundleMessageSource.PropertiesHolderCacheEntry

    public class CacheEntry<V>
    extends java.lang.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
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void expire()  
      long getCreatedMillis()  
      V getValue()  
      V getValue​(long timeout, java.util.concurrent.Callable<V> updater)  
      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.
      static <K,​V>
      V
      getValue​(java.util.concurrent.ConcurrentMap<K,​CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater)  
      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)  
      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.
      protected V getValueWhileUpdating​(java.lang.Object cacheRequestObject)  
      protected boolean hasExpired​(long timeout, java.lang.Object cacheRequestObject)  
      boolean isInitialized()  
      protected void resetTimestamp​(boolean updated)  
      void setInitialized​(boolean initialized)  
      void setValue​(V val)  
      protected boolean shouldUpdate​(long beforeLockingCreatedMillis, java.lang.Object cacheRequestObject)  
      protected V updateValue​(V oldValue, java.util.concurrent.Callable<V> updater, java.lang.Object cacheRequestObject)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 map
        key - the key to look up
        timeoutMillis - cache entry timeout
        updater - callback to create/update value
        cacheEntryClass - CacheEntry implementation class to use
        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:
      • 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)
      • 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)