Bhagirathy
Bhagirathy

Reputation: 29

Does ehcache uses same cache for all clients

I am trying to use ehcache cache. For two different user my webservice creates different cache which is acting like session. Can i create single cache which will work same as context concept

Upvotes: 1

Views: 1869

Answers (1)

lanimall
lanimall

Reputation: 451

When instantiating an EhCache CacheManager (which is the first step in getting a reference to a Cache object) you have several options per EhCache APIs:

  1. "Traditionnal" object contructor: new CacheManager(). As explained in the API page, "This method does not act as a singleton. Callers must maintain their own reference to it."
  2. Static constructor: CacheManager.create(). This creation method does act as a singleton, which means that you can call the CacheManager.create() for every requests or sessions...and it will always return the same CacheManager.

Since you haven't provided any detail on your implementation, I can only suppose that you are (knowingly or not) creating a new CacheManager object (http://ehcache.org/apidocs/net/sf/ehcache/CacheManager.html) per service request or session...and that would explain the behavior you're noticing.

If you were using the singleton concept for creating the CacheManager (method 2), you would indeed create a single CacheManager + get a single reference to a Cache object that can be access accross threads (requests, sessions, etc ...)

Upvotes: 1

Related Questions