Anton Morozov > Tie-Cache-Autoupdater-0.21 > Tie::Cache::Autoupdater

Download:
Tie-Cache-Autoupdater-0.21.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.21   Source  

NAME ^

Tie::Cache::Autoupdater - Cache that automatically updated

VERSION ^

This documentation refers to <Tie::Cache::Autoupdater> version 0.21

AUTHOR ^

<Anton Morozov> (<antonfin@cpan.org>)

SYNOPSIS ^

        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
        };

DESCRIPTION ^

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.

How to use

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.

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
            }
        };

Parameters

You may set unlimited pairs key => value where key is unique cache key. Value is hash reference, where:

timeout

It's time for data saving. Default 1 second. If you have Time::HiRes module that you can set float timeout.

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.

        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;
                }
            };

source

Subroutine reference that return data for cache

clone

A flag that determines to clone or not clone the returned reference

NOTE1

If source subroutine return list, that package automatically convert it in array reference.

NOTE2

If system has Time::HiRes package that Tie::Cache::Autoupdater use Time::HiRes::time for timeout control.

NOTE3

For cloned references used dclone subroutine from Storage package

DEVELOPMENT ^

Repository

http://github.com/antonfin/Tie-Cache-Autoupdater

LICENSE AND COPYRIGHT ^

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.

syntax highlighting: