ContextCache.java 5.3 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2015 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.test.context;

import org.springframework.context.ApplicationContext;
20
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
A
Arjen Poutsma 已提交
21 22

/**
23 24 25
 * {@code ContextCache} defines the public API for caching Spring
 * {@link ApplicationContext ApplicationContexts} within the <em>Spring
 * TestContext Framework</em>.
A
Arjen Poutsma 已提交
26
 *
27 28
 * <p>A {@code ContextCache} maintains a cache of {@code ApplicationContexts}
 * keyed by {@link MergedContextConfiguration} instances.
A
Arjen Poutsma 已提交
29
 *
30 31 32 33 34 35 36
 * <h3>Rationale</h3>
 * <p>Context caching can have significant performance benefits if context
 * initialization is complex. So, although initializing a Spring context itself
 * is typically very quick, some beans in a context &mdash; for example, an
 * in-memory database or a {@code LocalSessionFactoryBean} for working with
 * Hibernate &mdash; may take several seconds to initialize. Hence it often
 * makes sense to perform that initialization only once per test suite.
37
 *
A
Arjen Poutsma 已提交
38 39
 * @author Sam Brannen
 * @author Juergen Hoeller
40
 * @since 4.2
A
Arjen Poutsma 已提交
41
 */
42
public interface ContextCache {
A
Arjen Poutsma 已提交
43

44 45 46 47 48 49 50
	/**
	 * The name of the logging category used for reporting {@code ContextCache}
	 * statistics.
	 */
	public static final String CONTEXT_CACHE_LOGGING_CATEGORY = "org.springframework.test.context.cache";


A
Arjen Poutsma 已提交
51
	/**
S
Sam Brannen 已提交
52
	 * Determine whether there is a cached context for the given key.
53
	 * @param key the context key (never {@code null})
S
Sam Brannen 已提交
54
	 * @return {@code true} if the cache contains a context with the given key
A
Arjen Poutsma 已提交
55
	 */
56
	boolean contains(MergedContextConfiguration key);
A
Arjen Poutsma 已提交
57 58

	/**
59
	 * Obtain a cached {@code ApplicationContext} for the given key.
60 61
	 * <p>The {@link #getHitCount() hit} and {@link #getMissCount() miss} counts
	 * must be updated accordingly.
62
	 * @param key the context key (never {@code null})
63 64
	 * @return the corresponding {@code ApplicationContext} instance, or {@code null}
	 * if not found in the cache
A
Arjen Poutsma 已提交
65 66
	 * @see #remove
	 */
67
	ApplicationContext get(MergedContextConfiguration key);
A
Arjen Poutsma 已提交
68 69

	/**
70 71
	 * Explicitly add an {@code ApplicationContext} instance to the cache
	 * under the given key.
72
	 * @param key the context key (never {@code null})
73
	 * @param context the {@code ApplicationContext} instance (never {@code null})
A
Arjen Poutsma 已提交
74
	 */
75
	void put(MergedContextConfiguration key, ApplicationContext context);
A
Arjen Poutsma 已提交
76 77

	/**
78
	 * Remove the context with the given key from the cache and explicitly
79 80 81 82 83 84
	 * {@linkplain org.springframework.context.ConfigurableApplicationContext#close() close}
	 * it if it is an instance of {@code ConfigurableApplicationContext}.
	 * <p>Generally speaking, this method should be called if the state of
	 * a singleton bean has been modified, potentially affecting future
	 * interaction with the context.
	 * <p>In addition, the semantics of the supplied {@code HierarchyMode} must
85 86 87 88
	 * be honored. See the Javadoc for {@link HierarchyMode} for details.
	 * @param key the context key; never {@code null}
	 * @param hierarchyMode the hierarchy mode; may be {@code null} if the context
	 * is not part of a hierarchy
A
Arjen Poutsma 已提交
89
	 */
90
	void remove(MergedContextConfiguration key, HierarchyMode hierarchyMode);
91

92 93 94 95 96 97
	/**
	 * Determine the number of contexts currently stored in the cache.
	 * <p>If the cache contains more than {@code Integer.MAX_VALUE} elements,
	 * this method must return {@code Integer.MAX_VALUE}.
	 */
	int size();
98

99 100 101 102
	/**
	 * Determine the number of parent contexts currently tracked within the cache.
	 */
	int getParentContextCount();
103

104 105 106 107 108 109
	/**
	 * Get the overall hit count for this cache.
	 * <p>A <em>hit</em> is any access to the cache that returns a non-null
	 * context for the queried key.
	 */
	int getHitCount();
110

111 112 113 114 115 116
	/**
	 * Get the overall miss count for this cache.
	 * <p>A <em>miss</em> is any access to the cache that returns a {@code null}
	 * context for the queried key.
	 */
	int getMissCount();
117

118 119 120 121 122 123
	/**
	 * Reset all state maintained by this cache including statistics.
	 * @see #clear()
	 * @see #clearStatistics()
	 */
	void reset();
A
Arjen Poutsma 已提交
124 125

	/**
126
	 * Clear all contexts from the cache, clearing context hierarchy information as well.
A
Arjen Poutsma 已提交
127
	 */
128
	void clear();
129 130

	/**
131
	 * Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
132
	 */
133
	void clearStatistics();
A
Arjen Poutsma 已提交
134 135

	/**
136 137 138 139 140 141 142 143 144 145 146
	 * Log the statistics for this {@code ContextCache} at {@code DEBUG} level
	 * using the {@value #CONTEXT_CACHE_LOGGING_CATEGORY} logging category.
	 * <p>The following information should be logged.
	 * <ul>
	 * <li>name of the concrete {@code ContextCache} implementation</li>
	 * <li>{@linkplain #size}</li>
	 * <li>{@linkplain #getParentContextCount() parent context count}</li>
	 * <li>{@linkplain #getHitCount() hit count}</li>
	 * <li>{@linkplain #getMissCount() miss count}</li>
	 * <li>any other information useful for monitoring the state of this cache</li>
	 * </ul>
A
Arjen Poutsma 已提交
147
	 */
148
	void logStatistics();
A
Arjen Poutsma 已提交
149 150

}