Question

Does ehcache support multi-threading by default or does it require any configuration changes? On multi threading my application with Ehcache i found that the DB hit count is actually increasing i.e. there is no global cache available for all the threads despite the fact that my cache's are all Singletons. Any suggestions?

Was it helpful?

Solution

This may help answer your question, from the FAQ:

Is it thread safe to modify Element values after retrieval from a Cache?

Remember that a value in a cache element is globally accessible from multiple threads. It is inherently not thread safe to modify the value. It is safer to retrieve a value, delete the cache element and then reinsert the value.

(emphasis added by me)

OTHER TIPS

From the official documentation appears it is specifically built and tested to run well under highly concurrent access, as long as you do not modify Element from the multiple threads.

But, of course, this does not mean that using Ehcache makes other parts of your code thread safe. If you fetch the same value from multiple threads (using the same key), the instance may be shared and you need to know what you are doing before modifying it. Best seems to use immutable objects like strings as cached values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top