#!/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 ($time{$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; } }