tags:

views:

627

answers:

3
+1  Q: 

Ehcache and Java

Hi

I am using ehCache to store larges amount of data. This cache is accessed about 3 times every second and must be kept up to date. I have a thread that runs which retrieves all the data i need for the cache from the database every minute into a different cache(different cachemanager also). What i want to do is copy the contents of the data from the new cache into the cache which is being accessed every second in a quick, reliable and synchronized manner. (e.g. call a replace method passing in the existing cache, and the new cache and update the new cache quickly and safely)

Is there any way off hand to do this in ehCache?

Any help is greatly appreciated.

Thanks Damien

+3  A: 

It sounds like you are looking for a complicated solution to a problem ehcache solves out-of-the-box.

You only need one cache manager. Configure it to flush once a minute. If need be, do this by creating a thread that flushes the cache once a minute.

When you try to retrieve something from the cache and it is not there load the data again. Put this cache retrieval bit in a synchronized method or block if you want to be sure that the populating the cache blocks progress.

Steve McLeod
Hi SteveThanks for the reply it is useful.My only concern is if the user submits a request and the item they are looking for is currently being loaded, will this not lead slow results?ThanksDamien
Damo
Yes, it may be a bit slow in that situation. Can you tune the loading of the data also, to make it as fast as possible?
Steve McLeod
A: 

+1 to Steve McLeod.

See SelfPopulatingCache

I believe this will do exactly what you want, and quickly.

If you still want to maintain two caches, this is still the way to go. Just make the CacheFactory instance return cached values from your self-maintained cache.

Ken Gentle
A: 

Thanks Ken

Have you any quick code samples or links to code samples that implement the SelfPopulatingCache?

Thanks Damien

Damo