Class ThreadLocalCache<K,​V>

  • All Implemented Interfaces:
    Cache<K,​V>

    public final class ThreadLocalCache<K,​V>
    extends Object
    implements Cache<K,​V>
    Cache implementation based on ThreadLocal, that is, allowing each thread to have separate cache bindings. It is especially useful for caching non thread-safe objects. Since it is possible, that the application will need to use different ThreadLocalCache instances, for example one cache for script engines and another cache for database connections, we do not enforce this class to be singleton. However, it is mandatory to have only one instance of ThreadLocalCache per cache usage context (e.g. single instance for caching script engines and another, single instance for caching database connections) - usually, it is a job of IoC container.
    Since:
    2.1.0-SNAPSHOT
    Version:
    %I%, %G%
    Author:
    Piotr Dyraga
    See Also:
    Cache
    • Constructor Detail

      • ThreadLocalCache

        public ThreadLocalCache()
    • Method Detail

      • contains

        public boolean contains​(K key)
        Returns {@link true} if Cache contains object identified by the given key.
        Specified by:
        contains in interface Cache<K,​V>
        Parameters:
        key - key of the object being looked up in the Cache
        Returns:
        {@link true} if cache contains object identified by the given key; {@link false} is returned otherwise
      • put

        public void put​(K key,
                        V value)
        Associates the specified value with the specified key in Cache. If Cache previously contained a mapping for the specified key, the old value is replaced by the new one.
        Specified by:
        put in interface Cache<K,​V>
        Parameters:
        key - key under which value will be stored in cache
        value - value to be stored in cache
      • lookup

        public V lookup​(K key)
        Returns value associated with the specified key. If for the given key, no value is currently cached, then {@link null} is returned.
        Specified by:
        lookup in interface Cache<K,​V>
        Parameters:
        key - key of the object being looked up in the Cache
        Returns:
        object associated with the specified key, or {@link null} if there is no such object
      • invalidate

        public void invalidate​(K key)
        Discard value cached under the specified key.
        Specified by:
        invalidate in interface Cache<K,​V>
        Parameters:
        key - key of the cache binding which is going to be invalidated.