Caching in web apps

Beatnik on 2007-05-31T21:41:00

My CMS is building web pages on the fly, based on a number of components (template, navbar, macros, etc). Most of the content is semi-static. The user edits it once in a while so it can't really be a static page (the application does support mixing virtual and file-based contents already). What I was looking for was a way to still save some load time but keep the dynamic support. The hack I came up with is a bit as follows: I keep a hash in memory with all the necessary information (name, complete content data, modification time) of the contents that need caching (this is set in admin panel). I do a very minimal query from the database to check if caching is needed for the page. If so, I skip all the extras and just dump the cached content. Problems that might arise:

  1. Location collisions: Since I load a global hash, different website setups can access the same cache hash. Can be fixed easily enough.
  2. Memory: Loading pages in cache takes up memory. Depending on the approach, it might end up taking a lot of memory.
  3. Automatic renewal of cache: At the moment, content data is loaded into cache only once (on the first request). I still need a way to refresh the cache (based on edits/last modified time).
  4. Dynamic items: Page counters, for example, can't be used in the cacheable pages.
Some stats: requesting 10,000 pages takes about 80 seconds with caching and 270 seconds without caching.