The latest addition to DBIx::Recordset::Playground, which is the living documentation for DBIx::Recordset based on DBSchema::Sample, is pretty exciting to me. I learn something new every time I try to figure out what the author of Recordset means with his German-heavy English documentation.

For the past few hours, I have been going between source code and docs and test suite and now present one of many aspects of Recordset: looking up records with a default refresh rate.

DBIx::Recordset allows you to tie a hash to a database table, and retrieve the records of the table via the hash's key. You can tie the entire table or create an expirable ``view'' of a subset of the table via Recordset's !PreFetch option. Your view can be expired based on a fixed amount of seconds or via a boolean subroutine.

prefetch-insert.pl

#!/usr/bin/perl require 'dbconn.pl'; use DBIx::Recordset; use strict; # This program repeatedly presents sales data on STDOUT, refreshing # the view every $view_refresh seconds. It refreshes its # model (from the database) every $model_refresh seconds. # The default values for $model_refresh and $view_refresh imply that # the model will refreshed after 2.6 view refreshes or practically spe +aking # on every 3rd view refresh. # You can verify that it makes new hits on the database by noting the # DBIx::Recordset log messages. You will see this after every 3 view # displays: # DB: 'SELECT * FROM sales ORDER BY sonum DESC LIMIT 6' bind_val +ues=<> bind_types=<> # To spice things up, you can open a different terminal window and run # prefetch-insert.pl, which will insert a new record into the sales ta +ble # every $x seconds. # This program requires a version of DBIx::Recordset > 0.24, which is +the # current CPAN release. Or you can apply the patch recently posted to # the embperl@perl.apache.org mailing list. my $model_refresh = 13; my $view_refresh = 5; use vars qw(%sales); tie %sales, 'DBIx::Recordset::Hash', { conn_dbh(), '!Table' => 'sales', '!PreFetch' => { '$max' => 5, '$order' => 'sonum DESC' }, '!PrimKey' => 'sonum', '!Expires' => $model_refresh }; sub bynumber { $a <=> $b } while (1) { my (@key) = keys %sales; print $sales{$_}{sonum}, $/ for sort bynumber @key; sleep $view_refresh; print $/; }

prefetch-insert.pl

require 'dbconn.pl'; use DBIx::Recordset; use strict; # This program takes one argument, an integer indicating how often it +should # insert a random record into the sales table. my $insert_frequency = shift or die 'must specify insert frequency'; use vars qw(*set); sub rand_ponum { sprintf "%s%d%s", chr(65 + rand 25), rand 400 + rand 1000, lc chr(65 + rand 25); } *set = DBIx::Recordset->Search ({ conn_dbh(), '!Table' => 'sales', '!Fields' => 'max(sonum) as max_id', }); my $max_id = $set{max_id}; while (1) { DBIx::Recordset->Insert ( { conn_dbh(), '!Table' => 'sales', sonum => ++$max_id, stor_id => (sprintf "%d", 7000 + rand 1000), ponum => rand_ponum, sdate => '2003-10-22' } ); sleep $insert_frequency; }

DBSchema::Sample