my (%aCache) : shared; my ($cacheHit) : shared = 0; my ($cacheMiss) : shared = 0; sub Do_Stuff () { my ($Id, $aDate, $aValue) = @_; my ($dbh, $sth, $row); # Check if the required information is in the cache before doing anything else. my ($cacheKey) = sprintf("%s:%s:%s", $Id, $aDate, $aValue); if (exists $aCache{$cacheKey}) { lock ($cacheHit); $cacheHit++; # # Put cached data in $row # $row = $aCache{$cacheKey}; } else { { lock ($cacheMiss); $cacheMiss++; } my $dbh = DBI->connect("Whatever DB",'Default', '') or die "Can't connect to Whatever DB database: $DBI::errstr\n"; $sth = $dbh->prepare( "SELECT a, " . "b, " . "c " . "FROM mytable " . "WHERE a = ? ") or die "Couldn't prepare statement: $dbh->errstr"; $sth->bind_param(1, $Id); $sth->execute(); # # Store new row in the cache. # This causes an "Invalid value for shared scalar error: $aCache{$cacheKey} = $sth->fetchrow_hashref(); # # Put the result in $row # $row = $aCache{$cacheKey}; $sth->finish(); $dbh->disconnect(); } }