package My::Apache::UserCache; use strict; use My::User; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(get_pass get_groups); use constant CACHE_TIMEOUT => 60; # seconds my %cache = (); sub fill_cache { my $user = shift; my $now = time; my ($password,@groups) = My::User->get_pw_groups( $user ); # hits DB ... other checks ... $cache{$user} = { timestamp => $now, password => $password, groups => [@groups] }; } sub get_pass { my $user = shift; my $now = time; return $cache{$user}->{password} if $exists_and_hasnt_timed_out_etc; return undef unless fill_cache( $user ); $cache{$user}->{password}; # get direct from cache } sub get_groups { # similar to above }