Class DefaultContextCache
- All Implemented Interfaces:
- 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".
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
- 
Field SummaryFields inherited from interface org.springframework.test.context.cache.ContextCacheCONTEXT_CACHE_LOGGING_CATEGORY, DEFAULT_MAX_CONTEXT_CACHE_SIZE, MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newDefaultContextCacheusing the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize().DefaultContextCache(int maxSize) Create a newDefaultContextCacheusing the supplied maximum cache size.
- 
Method SummaryModifier and TypeMethodDescriptionvoidclear()Clear all contexts from the cache, clearing context hierarchy information as well.voidClear hit count and miss count statistics for the cache (i.e., reset counters to zero).booleanDetermine whether there is a cached context for the given key.Obtain a cachedApplicationContextfor the given key.intGet the failure count for the given key.intGet the overall hit count for this cache.intGet the maximum size of this cache.intGet the overall miss count for this cache.intDetermine the number of parent contexts currently tracked within the cache.voidIncrement the failure count for the given key.voidLog 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, @Nullable 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.toString()Generate a text string containing the implementation type of this cache and its statistics.
- 
Constructor Details- 
DefaultContextCachepublic DefaultContextCache()Create a newDefaultContextCacheusing the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize().- Since:
- 4.3
- See Also:
 
- 
DefaultContextCachepublic DefaultContextCache(int maxSize) Create a newDefaultContextCacheusing the supplied maximum cache size.- Parameters:
- maxSize- the maximum cache size
- Throws:
- IllegalArgumentException- if the supplied- maxSizevalue is not positive
- Since:
- 4.3
- See Also:
 
 
- 
- 
Method Details- 
containsDescription copied from interface:ContextCacheDetermine whether there is a cached context for the given key.- Specified by:
- containsin interface- ContextCache
- Parameters:
- key- the context key (never- null)
- Returns:
- trueif the cache contains a context with the given key
 
- 
getDescription copied from interface:ContextCacheObtain a cachedApplicationContextfor the given key.- Specified by:
- getin interface- ContextCache
- Parameters:
- key- the context key (never- null)
- Returns:
- the corresponding ApplicationContextinstance, ornullif not found in the cache
- See Also:
 
- 
putDescription copied from interface:ContextCacheExplicitly add anApplicationContextinstance to the cache under the given key, potentially honoring a custom eviction policy.- Specified by:
- putin interface- ContextCache
- Parameters:
- key- the context key (never- null)
- context- the- ApplicationContextinstance (never- null)
 
- 
removepublic void remove(MergedContextConfiguration key, @Nullable DirtiesContext.HierarchyMode hierarchyMode) Description copied from interface:ContextCacheRemove 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 (for example, 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 interface- ContextCache
- Parameters:
- key- the context key; never- null
- hierarchyMode- the hierarchy mode; may be- nullif the context is not part of a hierarchy
 
- 
getFailureCountDescription copied from interface:ContextCacheGet the failure count for the given key.A failure is any attempt to load the ApplicationContextfor the given key that results in an exception.The default implementation of this method always returns 0. Concrete implementations are therefore highly encouraged to override this method andContextCache.incrementFailureCount(MergedContextConfiguration)with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Specified by:
- getFailureCountin interface- ContextCache
- Parameters:
- key- the context key; never- null
- See Also:
 
- 
incrementFailureCountDescription copied from interface:ContextCacheIncrement the failure count for the given key.The default implementation of this method does nothing. Concrete implementations are therefore highly encouraged to override this method and ContextCache.getFailureCount(MergedContextConfiguration)with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Specified by:
- incrementFailureCountin interface- ContextCache
- Parameters:
- key- the context key; never- null
- See Also:
 
- 
sizepublic int size()Description copied from interface:ContextCacheDetermine 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 interface- ContextCache
 
- 
getMaxSizepublic int getMaxSize()Get the maximum size of this cache.
- 
getParentContextCountpublic int getParentContextCount()Description copied from interface:ContextCacheDetermine the number of parent contexts currently tracked within the cache.- Specified by:
- getParentContextCountin interface- ContextCache
 
- 
getHitCountpublic int getHitCount()Description copied from interface:ContextCacheGet 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 interface- ContextCache
 
- 
getMissCountpublic int getMissCount()Description copied from interface:ContextCacheGet 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 interface- ContextCache
 
- 
resetpublic void reset()Description copied from interface:ContextCacheReset all state maintained by this cache including statistics.- Specified by:
- resetin interface- ContextCache
- See Also:
 
- 
clearpublic void clear()Description copied from interface:ContextCacheClear all contexts from the cache, clearing context hierarchy information as well.- Specified by:
- clearin interface- ContextCache
 
- 
clearStatisticspublic void clearStatistics()Description copied from interface:ContextCacheClear hit count and miss count statistics for the cache (i.e., reset counters to zero).- Specified by:
- clearStatisticsin interface- ContextCache
 
- 
logStatisticspublic void logStatistics()Description copied from interface:ContextCacheLog 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 interface- ContextCache
 
- name of the concrete 
- 
toStringGenerate 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().
 
-