views:

37

answers:

1

I'm planning on caching some information using ehcache. Normally, I would follow the pattern:

if data in cache
   return from cache
else
   lookup from source
   put into cache
   return data

But for this data, I'd like it to always be in memory. I'd prefer to do:

  • Populate cache while appserver starts up (I'm using JBoss)
  • Have a "process/thread/something" that periodically updates the cache

My first thought is to define a servlet with a ServletContextListener that does the initial load and then starts a thread to do the updates.

Is there some better way to do this?

+2  A: 

The problem you have is in deciding what data goes into the cache in the first place. I'm assuming the cache is smaller than the data you wish to store. If that's not the case, why use a cache? Just store the data in-memory and be done with it.

Having said that, once you've decided that, I see nothing wrong with your proposal. We actually used a similar scheme when reloading our Java code in an early version of WAS. Part of the startup code for the app server was modified to access the pages we wanted pre-compiled (sort of a web scraper) so that there would be no delay the first time the user tried to access it.

As to periodically updating the cache, this should be done by the cache itself. If all changes are going through the cache (write-behind or write-through), the cache should be automatically updated for you, without a separate process required.

paxdiablo
Thanks for the comments. In this case the data will fit into memory. I am thinking of using Oracle Change Notification and/or triggers to know when the data has changed. The reason for that is that I cannot force all updates to go through my application.
Dave