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 theContextCacheAPI.Uses a synchronized
Mapconfigured with a maximum size and a least recently used (LRU) eviction policy to cacheApplicationContextinstances.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:
ContextCacheUtils.retrieveMaxCacheSize()
Field Summary
Fields inherited from interface org.springframework.test.context.cache.ContextCache
CONTEXT_CACHE_LOGGING_CATEGORY, DEFAULT_MAX_CONTEXT_CACHE_SIZE, MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME
Constructor Summary
Constructors Constructor Description DefaultContextCache()Create a newDefaultContextCacheusing the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize().DefaultContextCache(int maxSize)Create a newDefaultContextCacheusing the supplied maximum cache size.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clear all contexts from the cache, clearing context hierarchy information as well.voidclearStatistics()Clear hit and miss count statistics for the cache (i.e., reset counters to zero).booleancontains(MergedContextConfiguration key)Determine whether there is a cached context for the given key.ApplicationContextget(MergedContextConfiguration key)Obtain a cachedApplicationContextfor the given key.intgetHitCount()Get the overall hit count for this cache.intgetMaxSize()Get the maximum size of this cache.intgetMissCount()Get the overall miss count for this cache.intgetParentContextCount()Determine the number of parent contexts currently tracked within the cache.voidlogStatistics()Log the statistics for thisContextCacheatDEBUGlevel using the "org.springframework.test.context.cache" logging category.voidput(MergedContextConfiguration key, ApplicationContext context)Explicitly add anApplicationContextinstance to the cache under the given key, potentially honoring a custom eviction policy.voidremove(MergedContextConfiguration key, DirtiesContext.HierarchyMode hierarchyMode)Remove the context with the given key from the cache and explicitly close it if it is an instance ofConfigurableApplicationContext.voidreset()Reset all state maintained by this cache including statistics.intsize()Determine the number of contexts currently stored in the cache.StringtoString()Generate a text string containing the implementation type of this cache and its statistics.
Constructor Detail
DefaultContextCache
public DefaultContextCache()
Create a newDefaultContextCacheusing the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize().- Since:
- 4.3
- See Also:
DefaultContextCache(int),ContextCacheUtils.retrieveMaxCacheSize()
DefaultContextCache
public DefaultContextCache(int maxSize)
Create a newDefaultContextCacheusing the supplied maximum cache size.- Parameters:
maxSize- the maximum cache size- Throws:
IllegalArgumentException- if the suppliedmaxSizevalue is not positive- Since:
- 4.3
- See Also:
DefaultContextCache()
Method Detail
contains
public boolean contains(MergedContextConfiguration key)
Determine whether there is a cached context for the given key.- Specified by:
containsin interfaceContextCache- Parameters:
key- the context key (nevernull)- Returns:
trueif the cache contains a context with the given key
get
public ApplicationContext get(MergedContextConfiguration key)
Obtain a cachedApplicationContextfor the given key.- Specified by:
getin interfaceContextCache- Parameters:
key- the context key (nevernull)- Returns:
- the corresponding
ApplicationContextinstance, ornullif not found in the cache - See Also:
ContextCache.remove(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
put
public void put(MergedContextConfiguration key, ApplicationContext context)
Explicitly add anApplicationContextinstance to the cache under the given key, potentially honoring a custom eviction policy.- Specified by:
putin interfaceContextCache- Parameters:
key- the context key (nevernull)context- theApplicationContextinstance (nevernull)
remove
public 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 ofConfigurableApplicationContext.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
HierarchyModemust be honored. See the Javadoc forDirtiesContext.HierarchyModefor details.- Specified by:
removein interfaceContextCache- Parameters:
key- the context key; nevernullhierarchyMode- the hierarchy mode; may benullif 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_VALUEelements, this method must returnInteger.MAX_VALUE.- Specified by:
sizein interfaceContextCache
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:
getParentContextCountin interfaceContextCache
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:
getHitCountin interfaceContextCache
getMissCount
public int getMissCount()
Get the overall miss count for this cache.A miss is any access to the cache that returns a
nullcontext for the queried key.- Specified by:
getMissCountin interfaceContextCache
reset
public void reset()
Reset all state maintained by this cache including statistics.- Specified by:
resetin interfaceContextCache- See Also:
ContextCache.clear(),ContextCache.clearStatistics()
clear
public void clear()
Clear all contexts from the cache, clearing context hierarchy information as well.- Specified by:
clearin interfaceContextCache
clearStatistics
public void clearStatistics()
Clear hit and miss count statistics for the cache (i.e., reset counters to zero).- Specified by:
clearStatisticsin interfaceContextCache
logStatistics
public void logStatistics()
Log the statistics for thisContextCacheatDEBUGlevel using the "org.springframework.test.context.cache" logging category.The following information should be logged.
- name of the concrete
ContextCacheimplementation - ContextCache.size()
- parent context count
- hit count
- miss count
- any other information useful for monitoring the state of this cache
- Specified by:
logStatisticsin interfaceContextCache
- name of the concrete
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().