caedes has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Caching data with mod_perl
by Revelation (Deacon) on Jul 14, 2002 at 02:38 UTC | |
by flocto (Pilgrim) on Jul 14, 2002 at 09:15 UTC | |
by perrin (Chancellor) on Jul 14, 2002 at 21:56 UTC | |
by caedes (Pilgrim) on Jul 15, 2002 at 04:07 UTC | |
by caedes (Pilgrim) on Jul 14, 2002 at 02:56 UTC | |
by chromatic (Archbishop) on Jul 14, 2002 at 07:38 UTC | |
by Aristotle (Chancellor) on Jul 15, 2002 at 15:39 UTC | |
by caedes (Pilgrim) on Jul 14, 2002 at 03:13 UTC | |
|
Re: Caching data with mod_perl
by IlyaM (Parson) on Jul 14, 2002 at 18:41 UTC |