Package com.easydesk.cache.impl
Class MethodInvocationCache
java.lang.Object
com.easydesk.cache.impl.MethodInvocationCache
A simple per-thread cache, keyed by
MethodInvocationCacheKey.
- The thread-to-map association uses
ThreadLocalinstead of a hand-rolledWeakHashMap<Thread, Map>guarded by manual synchronization.ThreadLocalis the standard tool for this and avoids the double-checked-locking dance the old code needed.
ThreadLocal field does -- so one shared instance is safe to use
concurrently from any number of threads.- Version:
- $Id: MethodInvocationCache.java 1 2024-06-10 00:00:00Z youngho.cho $
- Author:
- Youngho Cho
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all cached values for the current thread.voidclear(Serializable instanceOrClass) Clears cached values for the current thread that were stored against the given instance (or class name, for static methods).Caches a value for the given method invocation, visible only to the current thread.get(Serializable instanceOrClass, String method, Serializable... args) Retrieves a cached value for the given method invocation, ornullif nothing is cached for it (on this thread).getMap()voidput(Object value, MethodInvocationCacheKey key) Caches a value for the given method invocation, visible only to the current thread.voidput(Object value, Serializable instanceOrClass, String method, Serializable... args) Caches a value for the given method invocation, visible only to the current thread.
-
Constructor Details
-
MethodInvocationCache
public MethodInvocationCache()
-
-
Method Details
-
getMap
- Returns:
- the mutable cache map for the current thread (never null)
-
clear
public void clear()Clears all cached values for the current thread. -
clear
Clears cached values for the current thread that were stored against the given instance (or class name, for static methods).- Parameters:
instanceOrClass- the instance/class whose cached entries should be evicted
-
get
Retrieves a cached value for the given method invocation, ornullif nothing is cached for it (on this thread).- Parameters:
instanceOrClass- the object the method is invoked on, or a class name string for static methodsmethod- the method nameargs- the arguments the method was called with- Returns:
- the cached result, or
nullif not present
-
get
Caches a value for the given method invocation, visible only to the current thread.- Parameters:
key- the key representing the method invocation
-
put
Caches a value for the given method invocation, visible only to the current thread.- Parameters:
value- the value to cacheinstanceOrClass- the object the method is invoked on, or a class name string for static methodsmethod- the method nameargs- the arguments the method was called with
-
put
Caches a value for the given method invocation, visible only to the current thread.- Parameters:
value- the value to cachekey- the key representing the method invocation
-