开发者

How to prime cache and keep it updated

I'm planning on caching some开发者_如何学Go 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜