package My::Apache::UserCache; use strict; use Apache::Constants qw(:common); use My::User; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(get_pass get_groups); use constant CACHE_TIMEOUT => 60; # seconds # # username => { timestamp => unixtime, # password => blah, # groups => [groups], }, # our %cache = (); sub fill_cache { my $user = shift; my $now = time; my ($password, @groups) = Oriel::User->get_pw_groups($user ); # enforce at least 1 character passwords return 0 unless defined $password && length($password); warn "[$$] fill_cache: setting cache [$user] pw:[$password] groups:[".join(',',@groups)."]\n"; $cache{$user} = { timestamp => $now, password => $password, groups => [@groups] }; } sub get_pass { my $user = shift; my $now = time; return $cache{$user}->{password} if ( exists $cache{$user} && $cache{$user}->{timestamp} && ($now - $cache{$user}->{timestamp}) < CACHE_TIMEOUT ); warn "[$$] get_pass: not in cache - retrieving\n"; return undef unless fill_cache( $user ); warn "[$$] get_pass: password now: ".(defined cache{$user} ? $cache{$user}->{password} : 'user undef!')."\n"; $cache{$user}->{password}; } sub get_groups { my $user = shift; my $now = time; return @{$cache{$user}->{groups}} if ( exists $cache{$user} && $cache{$user}->{timestamp} && ($now - $cache{$user}->{timestamp}) < CACHE_TIMEOUT ); warn "[$$] get_groups: not in cache - retrieving\n"; return () unless fill_cache( $user ); warn "[$$] get_groups: groups now: ".(defined $cache{$user} ? @{$cache{$user}->{groups}} : 'user undef!')."\n"; @{$cache{$user}->{groups}}; } 1;