Class ResourceCache


  • public class ResourceCache
    extends Object
    Resource caching provider.

    Cache implementation is based on thread-local variables. In order to use cache in a thread, init() method must be called. When cache is no longer needed, clear() method should be used to perform thread local cleanup.

    Cache can be locked by invoking lock(), after cache is locked, no items will be cached, and calling any cache method will be a no-op. To unlock cache and make it accept cache calls again, unlock() method must be invoked.

    If cache is used in recursive calls, it is safe to call init() and clear() multiple times. Init call will not purge current cache state in case it is called recursively. Clear call will clear cache state only if recursion exited (initDepth == 0).

    Author:
    jbegic
    • Constructor Detail

      • ResourceCache

        public ResourceCache()
    • Method Detail

      • init

        public void init()
        Initialises cache for current thread-scope.
      • clear

        public void clear()
        Clears current thread scope state.
        Throws:
        IllegalStateException - in case init() was not called
      • cache

        public void cache​(Map<String,​Object> resources)
        Adds multiple resources to cache.
        Parameters:
        resources - items to add
        Throws:
        IllegalStateException - in case init() was not called
      • cache

        public void cache​(String identifier,
                          Object resource)
        Adds resource to cache.
        Parameters:
        identifier - resource identifier
        resource - resource
        Throws:
        IllegalStateException - in case init() was not called
      • get

        public Object get​(String identifier)
        Returns cached resource or null if resource was not found in cache.
        Parameters:
        identifier - resource identifier
        Returns:
        cached resource or null
      • contains

        public boolean contains​(String identifier)
        Checks if resource with given identifier is cached.
        Parameters:
        identifier - resource identifier
        Returns:
        true if resource is cached, else false
        Throws:
        IllegalStateException - in case init() was not called
      • lock

        public void lock()
        Locks this cache instance for thread scope that method was invoked in.

        After invoking this method, all cache attempts will be no-op.

        Throws:
        IllegalStateException - in case init() was not called
      • unlock

        public void unlock()
        Unlocks this cache instance for thread scope that method was invoked in.

        After invoking this method. Cache calls will cache items accordingly.