Plugin: HTTP Cache Control, improve performance & minimize resource usage

Hello all,

While learning about HTTP caching and the Conditional GET mechanism, I wrote this plugin for Zenphoto. Might it be useful for other people, and if so, should I upload it to Trac?
http://www.tummblr.com/my-code/zenphoto-plugin-http-cache-control/

httpCacheControl is a plugin that makes your Zenphoto pages cacheable in order to increase site performance and minimize resource usage.

Making pages cacheable by browsers, proxies, HTTP accelerators can significantly increase your site’s performance and eliminate wastage of resources (CPU cycles, memory, bandwidth, time). While caching might be a complicated matter for sites with time-critical or user-sensitive information, it is a great performance-booster with hardly any side-effects for relatively static sites. Zenphoto is just one such application as it primarily serves static images.

Even if you cannot allow the possibility of visitors ever seeing stale content, this plugin can improve your site’s performance. httpCacheControl can make your pages cacheable but require that visitors revalidate their cache on every single visit. This plugin can determine if the requested page changed since the last visit using a fraction of the resources it would take to process, output, and transmit the entire page. Thus, within a few milliseconds, httpCacheControl can tell the visitor, “this page hasn’t changed since your last visit; use your cached copy,” or “this page has changed; I will now send you a fresh copy.”

httpCacheControl is designed to be used in the album.php, albumarchive.php, image.php, index.php of your theme; in other words, the ZP_ALBUM, ZP_IMAGE, ZP_INDEX contexts.

Comments

  • trisweb Administrator
    Excellent, I'll have to try it out. I never did take a good look at ZP's HTTP headers for caching purposes...

    If we deem it useful for the Zenphoto core, would you be alright with us integrating your code? Since it's only HTTP header modification I think it's a useful core function.
  • I'd be honored to have this feature integrated in Zenphoto core.

    This is just a first stab at HTTP caching though. I'm not certain if my shortcut for verifying if a page has changed is 100% accurate, or if there are faster methods of verifying.

    Also, I haven't given any thought to caching that's compatible with comments and other social features since I haven't used those features yet.
  • trisweb Administrator
    I think your shortcut is sufficient. We can add a check for new comments, but that shouldn't be a common case when someone's browsing a gallery in sequence.

    Of course, if you set the cache-control correctly, the browser won't even bother checking, so it won't matter what you return. The conditional get is only when it goes back to ask, if it's own cache has been invalidated, afaik.

    Nice work!
  • I've updated the function to support comments.

    I'm using this query
    `'SELECT date,inmoderation FROM '. prefix('comments') ." WHERE imageid=$imageid"`
    and processing it in PHP instead of getting the latest comment date with
    `'SELECT date FROM '. prefix('comments') ." WHERE inmoderation=0 AND imageid=$imageid ORDER BY id DESC LIMIT 1"`
    since I'm afraid that on a site with lots of comments, the comments table would be huge and the second query would require scanning the entire table. The first query uses the imageid INDEX. Does that make sense? (backticks omitted)

    Known issue:
    Although the plugin can determine the creation date of the most recent comment of a page, it cannot tell if the comment is edited subsequently because the edit date doesn’t appear to be stored in the database. Thus, a page might be considered fresh even if one of the comments has been updated since the last visit.

    Next step, make staticCacheControl comments-friendly.
  • I would love to use this plugin, but the link in the first post is dead. Does it still exist? Could anyone re-upload it so others can continue to use it? :) Or has it been integrated into ZenPhoto 1.2.2 by now?
Sign In or Register to comment.