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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface ContextCache
ContextCache.LoadFunction
-
Field Summary
Fields inherited from interface ContextCache
CONTEXT_CACHE_LOGGING_CATEGORY, DEFAULT_MAX_CONTEXT_CACHE_SIZE, MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newDefaultContextCache
using the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize()
.DefaultContextCache
(int maxSize) Create a newDefaultContextCache
using the supplied maximum cache size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear all contexts from the cache, clearing context hierarchy information as well.void
Clear hit count and miss count statistics for the cache (i.e., reset counters to zero).boolean
Determine whether there is a cached context for the given key.Obtain a cachedApplicationContext
for the given key.int
Determine the number of contexts within the cache that are currently in use.int
Get the failure count for the given key.int
Get the overall hit count for this cache.int
Get the maximum size of this cache.int
Get the overall miss count for this cache.int
Determine the number of parent contexts currently tracked within the cache.void
Increment the failure count for the given key.void
Log the statistics for thisContextCache
atDEBUG
level using the "org.springframework.test.context.cache" logging category.void
put
(MergedContextConfiguration key, ApplicationContext context) Deprecated.put
(MergedContextConfiguration key, ContextCache.LoadFunction loadFunction) Explicitly add anApplicationContext
to the cache under the given key, potentially honoring a custom eviction policy.void
registerContextUsage
(MergedContextConfiguration mergedConfig, Class<?> testClass) Register usage of theApplicationContext
for the suppliedMergedContextConfiguration
and any of its parents.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 ofConfigurableApplicationContext
.void
reset()
Reset all state maintained by this cache including statistics.int
size()
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.void
unregisterContextUsage
(MergedContextConfiguration mergedConfig, Class<?> testClass) Unregister usage of theApplicationContext
for the suppliedMergedContextConfiguration
and any of its parents.
-
Constructor Details
-
DefaultContextCache
public DefaultContextCache()Create a newDefaultContextCache
using the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize()
.- Since:
- 4.3
- See Also:
-
DefaultContextCache
public DefaultContextCache(int maxSize) Create a newDefaultContextCache
using the supplied maximum cache size.- Parameters:
maxSize
- the maximum cache size- Throws:
IllegalArgumentException
- if the suppliedmaxSize
value is not positive- Since:
- 4.3
- See Also:
-
-
Method Details
-
contains
Description copied from interface:ContextCache
Determine whether there is a cached context for the given key.- Specified by:
contains
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
- Returns:
true
if the cache contains a context with the given key
-
get
Description copied from interface:ContextCache
Obtain a cachedApplicationContext
for the given key.If the cached application context was previously paused, it must be restarted. This applies to parent contexts as well.
In addition, the hit and miss counts must be updated accordingly.
- Specified by:
get
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
- Returns:
- the corresponding
ApplicationContext
instance, ornull
if not found in the cache - See Also:
-
put
@Deprecated(since="7.0") public void put(MergedContextConfiguration key, ApplicationContext context) Deprecated.Description copied from interface:ContextCache
Explicitly add anApplicationContext
to the cache under the given key, potentially honoring a custom eviction policy.- Specified by:
put
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
context
- theApplicationContext
; nevernull
- See Also:
-
put
public ApplicationContext put(MergedContextConfiguration key, ContextCache.LoadFunction loadFunction) Description copied from interface:ContextCache
Explicitly add anApplicationContext
to the cache under the given key, potentially honoring a custom eviction policy.The supplied
ContextCache.LoadFunction
will be invoked to load theApplicationContext
.Concrete implementations which honor a custom eviction policy must override this method to ensure that an evicted context is removed from the cache and closed before a new context is loaded via the supplied
LoadFunction
.- Specified by:
put
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
loadFunction
- a function which loads the context for the supplied key; nevernull
- Returns:
- the
ApplicationContext
; nevernull
- See Also:
-
registerContextUsage
Description copied from interface:ContextCache
Register usage of theApplicationContext
for the suppliedMergedContextConfiguration
and any of its parents.The default implementation of this method does nothing. Concrete implementations are therefore highly encouraged to override this method,
ContextCache.unregisterContextUsage(MergedContextConfiguration, Class)
, andContextCache.getContextUsageCount()
with appropriate behavior. Note that the standardContextContext
implementation in Spring overrides these methods appropriately.- Specified by:
registerContextUsage
in interfaceContextCache
- Parameters:
mergedConfig
- the context key; nevernull
testClass
- the test class that is using the application context(s)- See Also:
-
unregisterContextUsage
Description copied from interface:ContextCache
Unregister usage of theApplicationContext
for the suppliedMergedContextConfiguration
and any of its parents.If no other test classes are actively using the same application context(s), the application context(s) should be paused.
The default implementation of this method does nothing. Concrete implementations are therefore highly encouraged to override this method,
ContextCache.registerContextUsage(MergedContextConfiguration, Class)
, andContextCache.getContextUsageCount()
with appropriate behavior. Note that the standardContextContext
implementation in Spring overrides these methods appropriately.- Specified by:
unregisterContextUsage
in interfaceContextCache
- Parameters:
mergedConfig
- the context key; nevernull
testClass
- the test class that is no longer using the application context(s)- See Also:
-
remove
public void remove(MergedContextConfiguration key, @Nullable DirtiesContext.HierarchyMode hierarchyMode) Description copied from interface:ContextCache
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 (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
HierarchyMode
must be honored. See the Javadoc forDirtiesContext.HierarchyMode
for details.- Specified by:
remove
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
hierarchyMode
- the hierarchy mode; may benull
if the context is not part of a hierarchy
-
getFailureCount
Description copied from interface:ContextCache
Get the failure count for the given key.A failure is any attempt to load the
ApplicationContext
for 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 standardContextContext
implementation in Spring overrides these methods appropriately.- Specified by:
getFailureCount
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
- See Also:
-
incrementFailureCount
Description copied from interface:ContextCache
Increment 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 standardContextContext
implementation in Spring overrides these methods appropriately.- Specified by:
incrementFailureCount
in interfaceContextCache
- Parameters:
key
- the context key; nevernull
- See Also:
-
size
public int size()Description copied from interface:ContextCache
Determine the number of contexts currently stored in the cache.If the cache contains more than
Integer.MAX_VALUE
elements, this method must returnInteger.MAX_VALUE
.- Specified by:
size
in interfaceContextCache
-
getMaxSize
public int getMaxSize()Get the maximum size of this cache. -
getContextUsageCount
public int getContextUsageCount()Description copied from interface:ContextCache
Determine the number of contexts within the cache that are currently in use.The default implementation of this method always returns
0
. Concrete implementations are therefore highly encouraged to override this method,ContextCache.registerContextUsage(MergedContextConfiguration, Class)
, andContextCache.unregisterContextUsage(MergedContextConfiguration, Class)
with appropriate behavior. Note that the standardContextContext
implementation in Spring overrides these methods appropriately.- Specified by:
getContextUsageCount
in interfaceContextCache
- See Also:
-
getParentContextCount
public int getParentContextCount()Description copied from interface:ContextCache
Determine the number of parent contexts currently tracked within the cache.- Specified by:
getParentContextCount
in interfaceContextCache
-
getHitCount
public int getHitCount()Description copied from interface:ContextCache
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 interfaceContextCache
-
getMissCount
public int getMissCount()Description copied from interface:ContextCache
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 interfaceContextCache
-
reset
public void reset()Description copied from interface:ContextCache
Reset all state maintained by this cache including statistics.- Specified by:
reset
in interfaceContextCache
- See Also:
-
clear
public void clear()Description copied from interface:ContextCache
Clear all contexts from the cache, clearing context hierarchy information as well.- Specified by:
clear
in interfaceContextCache
-
clearStatistics
public void clearStatistics()Description copied from interface:ContextCache
Clear hit count and miss count statistics for the cache (i.e., reset counters to zero).- Specified by:
clearStatistics
in interfaceContextCache
-
logStatistics
public void logStatistics()Description copied from interface:ContextCache
Log the statistics for thisContextCache
atDEBUG
level using the "org.springframework.test.context.cache" logging category.The following information should be logged.
- name of the concrete
ContextCache
implementation - ContextCache.size()
- context usage count
- parent context count
- hit count
- miss count
- any other information useful for monitoring the state of this cache
- Specified by:
logStatistics
in interfaceContextCache
- name of the concrete
-
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()
.
-