类 DefaultContextCache
- 所有已实现的接口:
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 Infra property named
infra.test.context.cache.maxSize.
- 从以下版本开始:
- 4.0
- 作者:
- Sam Brannen, Juergen Hoeller, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private classSimple cache implementation based onLinkedHashMapwith a maximum size and a least recently used (LRU) eviction policy that properly closes application contexts. -
字段概要
字段修饰符和类型字段说明private final Map<MergedContextConfiguration,cn.taketoday.context.ApplicationContext> Map of context keys to InfraApplicationContextinstances.private final Map<MergedContextConfiguration,Integer> Map of context keys to context load failure counts.private final Map<MergedContextConfiguration,Set<MergedContextConfiguration>> Map of parent keys to sets of children keys, representing a top-down tree of context hierarchies.private final AtomicIntegerprivate final intprivate final AtomicIntegerprivate static final cn.taketoday.logging.Loggerprivate final AtomicInteger从接口继承的字段 cn.taketoday.test.context.cache.ContextCache
CONTEXT_CACHE_LOGGING_CATEGORY, DEFAULT_MAX_CONTEXT_CACHE_SIZE, MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME -
构造器概要
构造器构造器说明Create a newDefaultContextCacheusing the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize().DefaultContextCache(int maxSize) Create a newDefaultContextCacheusing the supplied maximum cache size. -
方法概要
修饰符和类型方法说明voidclear()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.cn.taketoday.context.ApplicationContextObtain 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 "cn.taketoday.test.context.cache" logging category.voidput(MergedContextConfiguration key, cn.taketoday.context.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.private voidremove(List<MergedContextConfiguration> removedContexts, MergedContextConfiguration key) 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.
-
字段详细资料
-
statsLogger
private static final cn.taketoday.logging.Logger statsLogger -
contextMap
Map of context keys to InfraApplicationContextinstances. -
hierarchyMap
Map of parent keys to sets of children keys, representing a top-down tree of context hierarchies. This information is used for determining which subtrees need to be recursively removed and closed when removing a context that is a parent of other contexts. -
failureCounts
Map of context keys to context load failure counts. -
totalFailureCount
-
maxSize
private final int maxSize -
hitCount
-
missCount
-
-
构造器详细资料
-
DefaultContextCache
public DefaultContextCache()Create a newDefaultContextCacheusing the maximum cache size obtained viaContextCacheUtils.retrieveMaxCacheSize(). -
DefaultContextCache
public DefaultContextCache(int maxSize) Create a newDefaultContextCacheusing the supplied maximum cache size.- 参数:
maxSize- the maximum cache size- 抛出:
IllegalArgumentException- if the suppliedmaxSizevalue is not positive- 另请参阅:
-
-
方法详细资料
-
contains
Determine whether there is a cached context for the given key.- 指定者:
contains在接口中ContextCache- 参数:
key- the context key (nevernull)- 返回:
trueif the cache contains a context with the given key
-
get
Obtain a cachedApplicationContextfor the given key.- 指定者:
get在接口中ContextCache- 参数:
key- the context key (nevernull)- 返回:
- the corresponding
ApplicationContextinstance, ornullif not found in the cache - 另请参阅:
-
put
Explicitly add anApplicationContextinstance to the cache under the given key, potentially honoring a custom eviction policy.- 指定者:
put在接口中ContextCache- 参数:
key- the context key (nevernull)context- theApplicationContextinstance (nevernull)
-
remove
public 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.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.- 指定者:
remove在接口中ContextCache- 参数:
key- the context key; nevernullhierarchyMode- the hierarchy mode; may benullif the context is not part of a hierarchy
-
remove
private void remove(List<MergedContextConfiguration> removedContexts, MergedContextConfiguration key) -
getFailureCount
Get 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 Infra overrides these methods appropriately.- 指定者:
getFailureCount在接口中ContextCache- 参数:
key- the context key; nevernull- 另请参阅:
-
incrementFailureCount
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 standardContextContextimplementation in Infra overrides these methods appropriately.- 指定者:
incrementFailureCount在接口中ContextCache- 参数:
key- the context key; nevernull- 另请参阅:
-
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.- 指定者:
size在接口中ContextCache
-
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.- 指定者:
getParentContextCount在接口中ContextCache
-
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.
- 指定者:
getHitCount在接口中ContextCache
-
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.- 指定者:
getMissCount在接口中ContextCache
-
reset
public void reset()Reset all state maintained by this cache including statistics.- 指定者:
reset在接口中ContextCache- 另请参阅:
-
clear
public void clear()Clear all contexts from the cache, clearing context hierarchy information as well.- 指定者:
clear在接口中ContextCache
-
clearStatistics
public void clearStatistics()Clear hit count and miss count statistics for the cache (i.e., reset counters to zero).- 指定者:
clearStatistics在接口中ContextCache
-
logStatistics
public void logStatistics()Log the statistics for thisContextCacheatDEBUGlevel using the "cn.taketoday.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
- 指定者:
logStatistics在接口中ContextCache
- 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().
-