public class DefaultContextCache extends Object implements ContextCache
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.
ContextCacheUtils.retrieveMaxCacheSize()CONTEXT_CACHE_LOGGING_CATEGORY, DEFAULT_MAX_CONTEXT_CACHE_SIZE, MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME| Constructor and Description |
|---|
DefaultContextCache()
Create a new
DefaultContextCache using the maximum cache size
obtained via ContextCacheUtils.retrieveMaxCacheSize(). |
DefaultContextCache(int maxSize)
Create a new
DefaultContextCache using the supplied maximum
cache size. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clear all contexts from the cache, clearing context hierarchy information as well.
|
void |
clearStatistics()
Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
|
boolean |
contains(MergedContextConfiguration key)
Determine whether there is a cached context for the given key.
|
ApplicationContext |
get(MergedContextConfiguration key)
Obtain a cached
ApplicationContext for the given key. |
int |
getHitCount()
Get the overall hit count for this cache.
|
int |
getMaxSize()
Get the maximum size of this cache.
|
int |
getMissCount()
Get the overall miss count for this cache.
|
int |
getParentContextCount()
Determine the number of parent contexts currently tracked within the cache.
|
void |
logStatistics()
Log the statistics for this
ContextCache at DEBUG level
using the "org.springframework.test.context.cache" logging category. |
void |
put(MergedContextConfiguration key,
ApplicationContext context)
Explicitly add an
ApplicationContext instance to the cache
under the given key, potentially honoring a custom eviction policy. |
void |
remove(MergedContextConfiguration key,
DirtiesContext.HierarchyMode hierarchyMode)
Remove the context with the given key from the cache and explicitly
close
it if it is an instance of
ConfigurableApplicationContext. |
void |
reset()
Reset all state maintained by this cache including statistics.
|
int |
size()
Determine the number of contexts currently stored in the cache.
|
String |
toString()
Generate a text string containing the implementation type of this
cache and its statistics.
|
public DefaultContextCache()
DefaultContextCache using the maximum cache size
obtained via ContextCacheUtils.retrieveMaxCacheSize().DefaultContextCache(int),
ContextCacheUtils.retrieveMaxCacheSize()public DefaultContextCache(int maxSize)
DefaultContextCache using the supplied maximum
cache size.maxSize - the maximum cache sizeIllegalArgumentException - if the supplied maxSize value
is not positiveDefaultContextCache()public boolean contains(MergedContextConfiguration key)
contains in interface ContextCachekey - the context key (never null)true if the cache contains a context with the given keypublic ApplicationContext get(MergedContextConfiguration key)
ApplicationContext for the given key.
get in interface ContextCachekey - the context key (never null)ApplicationContext instance, or null
if not found in the cacheContextCache.remove(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)public void put(MergedContextConfiguration key, ApplicationContext context)
ApplicationContext instance to the cache
under the given key, potentially honoring a custom eviction policy.put in interface ContextCachekey - the context key (never null)context - the ApplicationContext instance (never null)public void remove(MergedContextConfiguration key, DirtiesContext.HierarchyMode hierarchyMode)
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.
remove in interface ContextCachekey - the context key; never nullhierarchyMode - the hierarchy mode; may be null if the context
is not part of a hierarchypublic int size()
If the cache contains more than Integer.MAX_VALUE elements,
this method must return Integer.MAX_VALUE.
size in interface ContextCachepublic int getMaxSize()
public int getParentContextCount()
getParentContextCount in interface ContextCachepublic int getHitCount()
A hit is any access to the cache that returns a non-null context for the queried key.
getHitCount in interface ContextCachepublic int getMissCount()
A miss is any access to the cache that returns a null
context for the queried key.
getMissCount in interface ContextCachepublic void reset()
reset in interface ContextCacheContextCache.clear(),
ContextCache.clearStatistics()public void clear()
clear in interface ContextCachepublic void clearStatistics()
clearStatistics in interface ContextCachepublic void logStatistics()
ContextCache at DEBUG level
using the "org.springframework.test.context.cache" logging category.
The following information should be logged.
ContextCache implementationlogStatistics in interface ContextCachepublic String toString()
The string returned by this method contains all information
required for compliance with the contract for logStatistics().