Class DefaultContextCache

java.lang.Object
org.springframework.test.context.cache.DefaultContextCache
All Implemented Interfaces:
ContextCache

public class DefaultContextCache extends Object implements ContextCache
Default implementation of the ContextCache API.

Uses a synchronized Map configured with a maximum size and a least recently used (LRU) eviction policy to cache ApplicationContext instances.

The maximum size may be supplied as a constructor argument or set via a system property or Spring property named spring.test.context.cache.maxSize.

Since:
2.5
Author:
Sam Brannen, Juergen Hoeller
See Also:
  • Constructor Details

  • Method Details

    • contains

      public boolean contains(MergedContextConfiguration key)
      Determine whether there is a cached context for the given key.
      Specified by:
      contains in interface ContextCache
      Parameters:
      key - the context key (never null)
      Returns:
      true if the cache contains a context with the given key
    • get

      Obtain a cached ApplicationContext for the given key.

      The hit and miss counts must be updated accordingly.

      Specified by:
      get in interface ContextCache
      Parameters:
      key - the context key (never null)
      Returns:
      the corresponding ApplicationContext instance, or null if not found in the cache
      See Also:
    • put

      public void put(MergedContextConfiguration key, ApplicationContext context)
      Explicitly add an ApplicationContext instance to the cache under the given key, potentially honoring a custom eviction policy.
      Specified by:
      put in interface ContextCache
      Parameters:
      key - the context key (never null)
      context - the ApplicationContext instance (never null)
    • remove

      public void remove(MergedContextConfiguration key, @Nullable DirtiesContext.HierarchyMode hierarchyMode)
      Remove the context with the given key from the cache and explicitly close it if it is an instance of ConfigurableApplicationContext.

      Generally speaking, this method should be called to properly evict a context from the cache (e.g., due to a custom eviction policy) or if the state of a singleton bean has been modified, potentially affecting future interaction with the context.

      In addition, the semantics of the supplied HierarchyMode must be honored. See the Javadoc for DirtiesContext.HierarchyMode for details.

      Specified by:
      remove in interface ContextCache
      Parameters:
      key - the context key; never null
      hierarchyMode - the hierarchy mode; may be null if the context is not part of a hierarchy
    • size

      public int size()
      Determine the number of contexts currently stored in the cache.

      If the cache contains more than Integer.MAX_VALUE elements, this method must return Integer.MAX_VALUE.

      Specified by:
      size in interface ContextCache
    • getMaxSize

      public int getMaxSize()
      Get the maximum size of this cache.
    • getParentContextCount

      public int getParentContextCount()
      Determine the number of parent contexts currently tracked within the cache.
      Specified by:
      getParentContextCount in interface ContextCache
    • getHitCount

      public int getHitCount()
      Get the overall hit count for this cache.

      A hit is any access to the cache that returns a non-null context for the queried key.

      Specified by:
      getHitCount in interface ContextCache
    • getMissCount

      public int getMissCount()
      Get the overall miss count for this cache.

      A miss is any access to the cache that returns a null context for the queried key.

      Specified by:
      getMissCount in interface ContextCache
    • reset

      public void reset()
      Reset all state maintained by this cache including statistics.
      Specified by:
      reset in interface ContextCache
      See Also:
    • clear

      public void clear()
      Clear all contexts from the cache, clearing context hierarchy information as well.
      Specified by:
      clear in interface ContextCache
    • clearStatistics

      public void clearStatistics()
      Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
      Specified by:
      clearStatistics in interface ContextCache
    • logStatistics

      public void logStatistics()
      Log the statistics for this ContextCache at DEBUG level using the "org.springframework.test.context.cache" logging category.

      The following information should be logged.

      Specified by:
      logStatistics in interface ContextCache
    • toString

      public String toString()
      Generate a text string containing the implementation type of this cache and its statistics.

      The string returned by this method contains all information required for compliance with the contract for logStatistics().

      Overrides:
      toString in class Object
      Returns:
      a string representation of this cache, including statistics