<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><div>I am pondering writing a simple little package and sticking it on CPAN that would try and do a little generic sql caching.&nbsp; The topic seems to come up about once a week, and I think I have a pretty good solution, kind of gleaned from much discussion on the list over the years.&nbsp; The code is at the end of this message.<br><br>You have sql with ?s as place holders.&nbsp; You pass the sql and what will fill the place holders ($binds).&nbsp; There is also a memcache_version_key which tries to keep a bit of state.&nbsp; The memcache_version_key might look like<br><br>'mvk.product_id.15'<br><br>When data_fallback runs, it generates a memkey like so<br><br><span id="1_messageHeaderCC" class="headerCC">my $memkey = 'df.' . ( $memcache-&gt;get($memcache_version_key) || 0 ) . '.' .
 md5_hex($sql) . '.' . md5_hex( join( ', ', @$binds ) );<br><br>if data_fallback can find anything based on that memkey, it returns it, otherwise, it queries and sets memcache.<br><br>This is not a panacea, as you need to know when certain queries have become invalid and invalidate the cache yourself, so when you know that the data for the product with id 15 changes, you do something like<br><br>$memcache-&gt;incr($memcache_version_key);<br><br>Anyway, I think it works well, and the paradigm has worked well in the past, though the code below is from last night.&nbsp; The tactic is especially good for querying a table in many different ways.<br><br>There are also a couple niceties; like getting hash refs back, or making queries that return </span><span id="1_messageHeaderCC" class="headerCC">only </span><span id="1_messageHeaderCC" class="headerCC">one column into a simple array ref.<br><br>Ideas for a name are welcome.&nbsp; It would be nice to have some
 simple CPAN code to point to when the discussion comes up again.&nbsp; This strategy alleviated my need for namespaces, and might be a nice stopgap until tags are incorporated.<br><br>Thoughts?<br>
</span><br>Thanks,<br>Earl<br><br><span id="1_messageHeaderCC" class="headerCC">use Carp qw(confess);<br>use Digest::MD5 qw(md5_hex);<br><br>sub data_fallback {<br>&nbsp;&nbsp;&nbsp; my $hash = shift || confess 'need a hash';<br><br>&nbsp;&nbsp;&nbsp; my $dbh&nbsp;&nbsp; = $hash-&gt;{dbh}&nbsp;&nbsp; || confess 'need a dbh';<br>&nbsp;&nbsp;&nbsp; my $sql&nbsp;&nbsp; = $hash-&gt;{sql}&nbsp;&nbsp; || confess 'need some sql';<br>&nbsp;&nbsp;&nbsp; my $binds = $hash-&gt;{binds} || confess 'need some binds';<br>&nbsp;&nbsp;&nbsp; my $memcache_version_key = $hash-&gt;{memcache_version_key}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; || confess 'need a memcache_version_key';<br>&nbsp;&nbsp;&nbsp; my $ttl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = $hash-&gt;{ttl}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; || 3600;<br>&nbsp;&nbsp;&nbsp; my $memcache = $hash-&gt;{memcache} || confess 'need a memcache object';<br>&nbsp;&nbsp;&nbsp; my $as_hash_ref =
 $hash-&gt;{as_hash_ref};<br><br>&nbsp;&nbsp;&nbsp; my $memkey = 'df.'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . ( $memcache-&gt;get($memcache_version_key) || 0 ) . '.'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . md5_hex($sql) . '.'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . md5_hex( join( ', ', @$binds ) );<br><br>&nbsp;&nbsp;&nbsp; my $data;<br>&nbsp;&nbsp;&nbsp; if ( $data = $memcache-&gt;get($memkey) ) {<br>&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $data = [];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $sth = $dbh-&gt;prepare($sql);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $sth-&gt;execute( @$binds );<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($as_hash_ref) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ( my $hash_ref = $sth-&gt;fetchrow_hashref('NAME_lc') )
 {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push @$data, $hash_ref;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ( my @array = $sth-&gt;fetchrow_array ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( scalar @array == 1 ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push @$data, $array[0];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push @$data, \@array;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $memcache-&gt;set( $memkey, $data, $ttl );<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return $data;<br>}<br></span></div></div><br>
      <hr size=1>Luggage? GPS? Comic books? <br>
Check out fitting <a href="http://us.rd.yahoo.com/evt=48249/*http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz"> gifts for grads</a> at Yahoo! Search.</body></html>