如何在JMeter中实现多用户共享HTTP缓存管理器缓存?
Hey there, I totally get what you're trying to do—you want a single cache instance that all 10 of your test threads can reuse, instead of each thread maintaining its own separate cache. The default HTTP Cache Manager in JMeter is thread-isolated by design, even when placed outside the Thread Group, so we need to work around that. Here are three reliable approaches to make global cache sharing work:
1. Use the HTTP Cache Manager (Global) Plugin
This is the simplest and most straightforward solution, purpose-built for cross-thread cache sharing.
- First, install the JMeter Plugins Manager if you haven't already.
- Open the Plugins Manager, search for
HTTP Cache Manager (Global), and install it. - Remove your existing
HTTP Cache Manager, then add theHTTP Cache Manager (Global)to the root of your Test Plan (outside all Thread Groups). - Configure it just like you would the regular cache manager—set cache policies, exclude/include URLs, etc. Now all threads will share this single global cache pool.
2. Manually Implement Global Cache with JSR223 Scripts
If you prefer not to use plugins, you can build a custom global cache using JMeter's shared properties and JSR223 elements:
- Step 1: Populate the global cache
Add asetUp Thread Group(this runs once before your main test threads) and put your cache-warming HTTP request inside it. Then add a JSR223 PostProcessor to the request with this Groovy code:// Use the request URL as the unique cache key def cacheKey = sampler.getUrl().toString() // Store the response content in JMeter's global properties def cachedResponse = prev.getResponseDataAsString() props.put(cacheKey, cachedResponse) - Step 2: Reuse the cache in main threads
In your main Thread Group, add a JSR223 PreProcessor to every HTTP request that should use the cached content. Use this code:
This checks if the URL is in the global cache first—if yes, it skips sending the real request and returns the cached response instead.def cacheKey = sampler.getUrl().toString() if (props.containsKey(cacheKey)) { // If cache exists, override the request to return cached data sampler.setResponseData(props.get(cacheKey).getBytes()) sampler.setSuccessful(true) sampler.setResponseCode("200") sampler.setResponseMessage("Served from global cache") }
3. Use a Shared File Cache
You can also configure JMeter to use a single file-based cache that all threads read from:
- First, run a single thread to populate the cache:
- Add a regular
HTTP Cache Managerto a temporary Thread Group, setCache Storage ImplementationtoFile Cache, and specify a local file path for the cache (e.g.,./jmeter_global_cache). - Run this thread once to generate the cache file.
- Add a regular
- Then, in your main test plan:
- Remove the temporary Thread Group, and add the
HTTP Cache Managerto your main Thread Group (or root level). - Set the same
Cache Storage ImplementationtoFile Cacheand point it to the same cache file path you used earlier. - Enable
Use Cache Control/Expires header when processing requestsif needed. Now all threads will read from this shared cache file.
- Remove the temporary Thread Group, and add the
Important Notes
- For the script-based approach, JMeter's
propsobject is thread-safe, so you don't have to worry about race conditions when accessing the cache. - If your cached content has expiration rules, you'll need to add logic to check and invalidate stale entries (either in the plugin's settings or your custom scripts).
- The plugin method is recommended for most cases since it handles edge cases like cache invalidation and HTTP compliance out of the box.
内容的提问来源于stack exchange,提问作者Jugi

