Tie::Cache::Autoupdater - Cache that automatically updated
This documentation refers to <Tie::Cache::Autoupdater> version 0.21
<Anton Morozov> (<antonfin@cpan.org>)
use Tie::Cache::Autoupdate; tie my %cache, 'Tie::Cache::Autoupdate', key1 => { source => sub { ....; return $ref }, timeout => 10 }, key2 => { source => \&get_data_from_db, timeout => 30 }, key2 => { source => \&get_data_from_file, timeout => 60 }; my ( $data1, $data2, $data3 ); $data1 = $cache{key1}; # data, that return anonymous subroutine $data2 = $cache{key2}; # data, that return get_data_from_db $data3 = $cache{key3}; # data, that return get_data_from_file ########################## 2 seconds ago ####################### $data1 = $cache{key1}; # update data, call anonymous subroutine $data2 = $cache{key2}; # old data, nothing called $data3 = $cache{key3}; # old data, nothing called ########################## 15 seconds ago ###################### $data1 = $cache{key1}; # update data, call anonymous subroutine $data2 = $cache{key2}; # update data, call get_data_from_db $data3 = $cache{key3}; # old data, nothing called ########################## 1 minute 10 seconds ago ############## $data1 = $cache{key1}; # update data, call anonymous subroutine one more $data2 = $cache{key2}; # update data, call get_data_from_db $data3 = $cache{key3}; # update data, call get_data_from_file # If you have Time::HiRes package, that you can use float timeout $cache{key4} = { source => \&get_data_from_db2, timeout => 0.5 }; # If you plane to change the returned data you may cloned there. # Set true to a flag "clone". $cache{key4} = { source => \&get_data_from_db3, timeout => 1, clone => 1 };
Sometimes I need show in web rarely changes data. You may save it in memory, but you never don't know how long script will be work. For example, fcgi scripts may work few days or weeks, but counters of database tables or site settings may changed more frequent in the day, each hour or each 10 minutes. I wrote package, that help you cached data on fixed time.
You may created hash and tied it usages this package.
tie my %cache, 'Tie::Cache::Autoupdater';
And set it in hash
$cache{db_query} = { timeout => 10, source => sub { my $sth = $DBH->prepare('select * from table'); $sth->execute; return $sth->fetchall_arrayref } };
Package call anonymous subroutine when you want to get value from %cache with key db_query.
%cache
db_query
Or you may set cache parameters when you tied hash. Like this:
tie %cache, 'Tie::Cache::Autoupdater', db_query => { timeout => 10, source => sub { my $sth = $DBH->prepare('select * from table'); $sth->execute; return $sth->fetchall_arrayref } };
You may set unlimited pairs key => value where key is unique cache key. Value is hash reference, where:
It's time for data saving. Default 1 second. If you have Time::HiRes module that you can set float timeout.
Time::HiRes
In next example value for key db_counters will be updated each 0.2 second, and value for file_count will be updated each 2.5 seconds.
db_counters
file_count
tie %cache, 'Tie::Cache::Autoupdater', db_counters => { timeout => 0.2, source => sub { my $sth = $DBH->prepare('select count(*) from table2'); $sth->execute; return ($sth->fetchrow_array); } }, file_count => { timeout => 2.5, source => sub { my $file_count = glob( "$path/*" ); return $file_count; } };
Subroutine reference that return data for cache
A flag that determines to clone or not clone the returned reference
If source subroutine return list, that package automatically convert it in array reference.
If system has Time::HiRes package that Tie::Cache::Autoupdater use Time::HiRes::time for timeout control.
Tie::Cache::Autoupdater
Time::HiRes::time
For cloned references used dclone subroutine from Storage package
dclone
Storage
http://github.com/antonfin/Tie-Cache-Autoupdater
Copyright (c) 2011 (antonfin@cpan.org)
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Tie::Cache::Autoupdater, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Cache::Autoupdater
CPAN shell
perl -MCPAN -e shell install Tie::Cache::Autoupdater
For more information on module installation, please visit the detailed CPAN module installation guide.