Class ResourceCache
- java.lang.Object
-
- com.github.jasminb.jsonapi.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()andclear()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 Summary
Constructors Constructor Description ResourceCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcache(String identifier, Object resource)Adds resource to cache.voidcache(Map<String,Object> resources)Adds multiple resources to cache.voidclear()Clears current thread scope state.booleancontains(String identifier)Checks if resource with given identifier is cached.Objectget(String identifier)Returns cached resource ornullif resource was not found in cache.voidinit()Initialises cache for current thread-scope.voidlock()Locks this cache instance for thread scope that method was invoked in.voidunlock()Unlocks this cache instance for thread scope that method was invoked in.
-
-
-
Method Detail
-
init
public void init()
Initialises cache for current thread-scope.
-
clear
public void clear()
Clears current thread scope state.- Throws:
IllegalStateException- in caseinit()was not called
-
cache
public void cache(Map<String,Object> resources)
Adds multiple resources to cache.- Parameters:
resources- items to add- Throws:
IllegalStateException- in caseinit()was not called
-
cache
public void cache(String identifier, Object resource)
Adds resource to cache.- Parameters:
identifier- resource identifierresource- resource- Throws:
IllegalStateException- in caseinit()was not called
-
get
public Object get(String identifier)
Returns cached resource ornullif 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:
trueif resource is cached, elsefalse- Throws:
IllegalStateException- in caseinit()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 caseinit()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.
-
-