photos by keyword; cache-friendly CGIs in Perl

by Gerald Oskoboiny <gerald@impressive.net>

 Date:  Thu, 12 Oct 2006 03:42:40 -0700
 To:  fogo@impressive.net
 Replies:  No further comments in this thread.
About a month ago I added an index of photos by keyword to my site: 

    http://impressive.net/people/gerald/photos/by/keyword

Just now I added a 'Related keywords' section to the end of each 
individual keyword page, e.g.

    http://impressive.net/people/gerald/photos/by/keyword/mountain

says:
> Related keywords: canada, whistler, biking, downhill, riding, 
> park, bike
> 
> (those are the words that appear most frequently in photos that 
> are also tagged "mountain".)

Calculating and displaying the related keywords took less than 20 
lines of Perl, pretty cool for such little effort. (including a 
keyword cache on disk, refreshed only when necessary)

I also added Last-Modified and If-Modified-Since support, for 
better cacheability and reduced server load. That was also less 
than 20 lines of code; near the top of the script is:

    if ( defined $ENV{HTTP_IF_MODIFIED_SINCE} ) {
        my $ims = str2time( $ENV{HTTP_IF_MODIFIED_SINCE} );
        if ( $ims >= max((stat($update_log))[9],(stat($ENV{SCRIPT_FILENAME}))[9])) {
            print "Status: 304 not modified\n\n";
            exit;
        }
    }

and the Last-Modified header is generated a la:

    my $lastmod = time2str(
            max((stat($keyword_cache))[9],(stat($ENV{SCRIPT_FILENAME}))[9]));

(time2str and str2time functions are from HTTP::Date)

$update_log points to ~/.cvs-update-log from my web mirroring 
system (similar to W3C's), so the pages are considered stale only 
when there is new content on my site (infrequent, maybe once/day 
on average), or the script itself has changed.

-- 
Gerald Oskoboiny <gerald@impressive.net>
http://impressive.net/people/gerald/

HURL: fogo mailing list archives, maintained by Gerald Oskoboiny