matonb has asked for the wisdom of the Perl Monks concerning the following question:
I'm probably missing something fundamentally simple here, but I can't work it out....
I'm trying to cache database results by putting the return of fetchrow_hashref into a hash, single thread works fine but when I share the cache hash I'm getting an "Invalid value for shared scalar" error...
Example of what I'm trying:
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 a +nything 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::errst +r\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(); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: threads::shared and DBI fetchrow_hashref
by BrowserUk (Patriarch) on May 27, 2011 at 08:59 UTC | |
by matonb (Initiate) on May 27, 2011 at 09:08 UTC |