Package org.springframework.test.context.cache

# 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:
  - `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 new `DefaultContextCache` using the maximum cache size obtained via `ContextCacheUtils.retrieveMaxCacheSize()`.
`DefaultContextCache(int maxSize)`
Create a new `DefaultContextCache` using the supplied maximum cache size.

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
Clear all contexts from the cache, clearing context hierarchy information as well.
`void`
`clearStatistics()`
Clear hit count 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.
`org.springframework.context.ApplicationContext`
`get(MergedContextConfiguration key)`
Obtain a cached `ApplicationContext` for the given key.
`int`
`getFailureCount(MergedContextConfiguration key)`
Get the failure count 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`
`incrementFailureCount(MergedContextConfiguration key)`
Increment the failure count for the given key.
`void`
`logStatistics()`
Log the statistics for this `ContextCache` at `DEBUG` level using the "org.springframework.test.context.cache" logging category.
`void`
`put(MergedContextConfiguration key, org.springframework.context.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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### DefaultContextCache

public DefaultContextCache()
Create a new `DefaultContextCache` using the maximum cache size obtained via `ContextCacheUtils.retrieveMaxCacheSize()`.

Since:
  4.3
See Also:
  - `DefaultContextCache(int)`
  - `ContextCacheUtils.retrieveMaxCacheSize()`

### DefaultContextCache

public DefaultContextCache(int maxSize)
Create a new `DefaultContextCache` using the supplied maximum cache size.

Parameters:
  `maxSize` - the maximum cache size
Throws:
  `IllegalArgumentException` - if the supplied `maxSize` value is not positive
Since:
  4.3
See Also:
  - `DefaultContextCache()`

## Method Details

### contains

public boolean contains(MergedContextConfiguration key)
Description copied from interface: `ContextCache`
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

@Nullable public org.springframework.context.ApplicationContext get(MergedContextConfiguration key)
Description copied from interface: `ContextCache`
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:
  - `ContextCache.remove(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)`

### put

public void put(MergedContextConfiguration key, org.springframework.context.ApplicationContext context)
Description copied from interface: `ContextCache`
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)
Description copied from interface: `ContextCache`
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 (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 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

### getFailureCount

public int getFailureCount(MergedContextConfiguration key)
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 and `ContextCache.incrementFailureCount(MergedContextConfiguration)` with appropriate behavior. Note that the standard `ContextContext` implementation in Spring overrides these methods appropriately.

Specified by:
  `getFailureCount` in interface `ContextCache`
Parameters:
  `key` - the context key; never `null`
See Also:
  - `ContextCache.incrementFailureCount(MergedContextConfiguration)`

### incrementFailureCount

public void incrementFailureCount(MergedContextConfiguration key)
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 standard `ContextContext` implementation in Spring overrides these methods appropriately.

Specified by:
  `incrementFailureCount` in interface `ContextCache`
Parameters:
  `key` - the context key; never `null`
See Also:
  - `ContextCache.getFailureCount(MergedContextConfiguration)`

### 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 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()
Description copied from interface: `ContextCache`
Determine the number of parent contexts currently tracked within the cache.

Specified by:
  `getParentContextCount` in interface `ContextCache`

### 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 interface `ContextCache`

### 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 interface `ContextCache`

### reset

public void reset()
Description copied from interface: `ContextCache`
Reset all state maintained by this cache including statistics.

Specified by:
  `reset` in interface `ContextCache`
See Also:
  - `ContextCache.clear()`
  - `ContextCache.clearStatistics()`

### 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 interface `ContextCache`

### 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 interface `ContextCache`

### logStatistics

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

The following information should be logged.

- name of the concrete `ContextCache` implementation
- ContextCache.size()
- parent context count
- hit count
- miss count
- any other information useful for monitoring the state of this cache

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