Class MethodInvocationCache

java.lang.Object
com.easydesk.cache.impl.MethodInvocationCache

@Component public class MethodInvocationCache extends Object
A simple per-thread cache, keyed by MethodInvocationCacheKey.

  • The thread-to-map association uses ThreadLocal instead of a hand-rolled WeakHashMap<Thread, Map> guarded by manual synchronization. ThreadLocal is the standard tool for this and avoids the double-checked-locking dance the old code needed.
This bean is a singleton (Spring's default scope), and that is exactly right: the object itself holds no per-thread state directly -- the 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 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

      public void clear(Serializable instanceOrClass)
      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

      public Object get(Serializable instanceOrClass, String method, Serializable... args)
      Retrieves a cached value for the given method invocation, or null if nothing is cached for it (on this thread).
      Parameters:
      instanceOrClass - the object the method is invoked on, or a class name string for static methods
      method - the method name
      args - the arguments the method was called with
      Returns:
      the cached result, or null if not present
    • get

      public Object get(MethodInvocationCacheKey key)
      Caches a value for the given method invocation, visible only to the current thread.
      Parameters:
      key - the key representing the method invocation
    • put

      public void put(Object value, Serializable instanceOrClass, String method, Serializable... args)
      Caches a value for the given method invocation, visible only to the current thread.
      Parameters:
      value - the value to cache
      instanceOrClass - the object the method is invoked on, or a class name string for static methods
      method - the method name
      args - the arguments the method was called with
    • put

      public void put(Object value, MethodInvocationCacheKey key)
      Caches a value for the given method invocation, visible only to the current thread.
      Parameters:
      value - the value to cache
      key - the key representing the method invocation