public class ResourceCache extends Object
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).
| Constructor and Description |
|---|
ResourceCache() |
| Modifier and Type | Method and Description |
|---|---|
void |
cache(Map<String,Object> resources)
Adds multiple resources to cache.
|
void |
cache(String identifier,
Object resource)
Adds resource to cache.
|
void |
clear()
Clears current thread scope state.
|
boolean |
contains(String identifier)
Checks if resource with given identifier is cached.
|
Object |
get(String identifier)
Returns cached resource or
null if resource was not found in cache. |
void |
init()
Initialises cache for current thread-scope.
|
void |
lock()
Locks this cache instance for thread scope that method was invoked in.
|
void |
unlock()
Unlocks this cache instance for thread scope that method was invoked in.
|
public void init()
public void clear()
IllegalStateException - in case init() was not calledpublic void cache(Map<String,Object> resources)
resources - items to addIllegalStateException - in case init() was not calledpublic void cache(String identifier, Object resource)
identifier - resource identifierresource - resourceIllegalStateException - in case init() was not calledpublic Object get(String identifier)
null if resource was not found in cache.identifier - resource identifiernullpublic boolean contains(String identifier)
identifier - resource identifiertrue if resource is cached, else falseIllegalStateException - in case init() was not calledpublic void lock()
After invoking this method, all cache attempts will be no-op.
IllegalStateException - in case init() was not calledpublic void unlock()
After invoking this method. Cache calls will cache items accordingly.
Copyright © 2017. All rights reserved.