Package: grails.util

## [Java] Class CacheEntry<V>

- grails.util.CacheEntry

-     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.

  Authors:
    Lari Hotari
  Since:
    2.3.4

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `static class` | `CacheEntry.UpdateException` |

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `private static java.util.concurrent.Callable<CacheEntry>` | `DEFAULT_CACHE_ENTRY_FACTORY` |
    `private static org.slf4j.Logger` | `LOG` |
    `private long` | `createdMillis` |
    `private boolean` | `initialized` |
    `private java.util.concurrent.locks.ReadWriteLock` | `lock` |
    `private java.util.concurrent.locks.Lock` | `readLock` |
    `private java.util.concurrent.atomic.AtomicReference<V>` | `valueRef` |
    `private java.util.concurrent.locks.Lock` | `writeLock` |

  - ### Constructor Summary

    Constructors
    Constructor and description
    `CacheEntry ()`
    `CacheEntry (V value)`

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public CacheEntry` | `call()`
    `` | `public void` | `expire()`
    `` | `public long` | `getCreatedMillis()`
    `<K, V>` | `public static 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.
    `<K, V>` | `public static V` | `getValue(java.util.concurrent.ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater)`
    `<K, V>` | `public static V` | `getValue(java.util.concurrent.ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater, boolean returnExpiredWhileUpdating)`
    `` | `public V` | `getValue(long timeout, java.util.concurrent.Callable<V> updater)`
    `` | `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
    `` | `public V` | `getValue()`
    `` | `protected V` | `getValueWhileUpdating(java.lang.Object cacheRequestObject)`
    `` | `protected boolean` | `hasExpired(long timeout, java.lang.Object cacheRequestObject)`
    `` | `public boolean` | `isInitialized()`
    `` | `protected void` | `resetTimestamp(boolean updated)`
    `` | `public void` | `setInitialized(boolean initialized)`
    `` | `public 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)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class java.lang.Object` | `java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()`

- - ### Field Detail

    - #### @java.lang.SuppressWarnings("rawtypes") private static final java.util.concurrent.Callable<CacheEntry> DEFAULT_CACHE_ENTRY_FACTORY

    - #### private static final org.slf4j.Logger LOG

    - #### private long createdMillis

    - #### private boolean initialized

    - #### private final java.util.concurrent.locks.ReadWriteLock lock

    - #### private final java.util.concurrent.locks.Lock readLock

    - #### private final java.util.concurrent.atomic.AtomicReference<V> valueRef

    - #### private final java.util.concurrent.locks.Lock writeLock

  - ### Constructor Detail

    - #### public CacheEntry()

    - #### public CacheEntry(V value)

  - ### Method Detail

    - #### @java.lang.Override public CacheEntry call()

    - #### public void expire()

    - #### public long getCreatedMillis()

    - #### <K, V> @java.lang.SuppressWarnings({ "unchecked", "rawtypes" }) public static 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:

    - #### <K, V> public static V getValue(java.util.concurrent.ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater)

    - #### <K, V> public static V getValue(java.util.concurrent.ConcurrentMap<K, CacheEntry<V>> map, K key, long timeoutMillis, java.util.concurrent.Callable<V> updater, boolean returnExpiredWhileUpdating)

    - #### public V getValue(long timeout, java.util.concurrent.Callable<V> updater)

    - #### 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

    - #### public V getValue()

    - #### protected V getValueWhileUpdating(java.lang.Object cacheRequestObject)

    - #### protected boolean hasExpired(long timeout, java.lang.Object cacheRequestObject)

    - #### public boolean isInitialized()

    - #### protected void resetTimestamp(boolean updated)

    - #### public void setInitialized(boolean initialized)

    - #### public 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)