Basically what I have is a subroutine Cache::cache() which you call with three arguments: a key, a subroutine reference, and a timeout value. The cache subroutine decides whether or not to run the subref and returns the output of the subref (either the cached or new output).
What do the wise monks think of this approach?
#!/usr/bin/perl -w # This script runs under mod_perl use strict; use CGI qw(:standard); my %data = Cache::cache('info',\&do_it,200); print header('text/html'); print start_html(); print p([$data{data},$data{source},$data{'time'},"Process: $$"]); print end_html(); sub do_it { # do something that takes a long time my $text = `ls -lR /`; return length($text); } package Cache; sub cache { our (%time, %asdf); my ($key, $sub, $time) = @_; if((not defined $asdf{$key}) or (not defined $time{$key}) or ($tim +e{$key} < time - $time)){ $time{$key} = time; $asdf{$key} = &$sub; my %out = (data => $asdf{$key}, 'time' => $time{$key}, source +=> 'db_query'); return %out; } else{ my %out = (data => $asdf{$key}, 'time' => $time{$key}, source +=> 'cache_query'); return %out; } }
-caedes
In reply to Caching data with mod_perl by caedes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |