Красимир Беров > CGI-Ex-Recipes > CGI::Ex::Recipes::Cache



Annotate this POD

View/Report Bugs
Module Version: 0.01   Source  


CGI::Ex::Recipes::Cache - Naive caching in a database table


Example from CGI::Ex::Recipes::Template::Menu::list_item():

    # ... somewhere at the beginning of a method/subroutine which does heavy computations
    if( $out = $app->cache->get($cache_key) ){ return $out; }
    # ... here are your heavy calculations spread accross many lines
    # making database calls generating HTML etc.
    # ... just before the return of the method
    #try cache support
    $app->cache->set($cache_key, $out);
    return $out;


I found that when I cached in memory some output from CGI::Ex::Recipes::Template::Menu, the performance under mod_perl jumped from:

    Requests per second:    19.42 [#/sec] (mean)
    Time per request:       154.441 [ms] (mean)
    Time per request:       51.480 [ms] (mean, across all concurrent requests)
    Transfer rate:          67.73 [Kbytes/sec] received


    Requests per second:    42.99 [#/sec] (mean)
    Time per request:       69.792 [ms] (mean)
    Time per request:       23.264 [ms] (mean, across all concurrent requests)
    Transfer rate:          151.74 [Kbytes/sec] received

ApacheBench was invoked like this:

    berov@berovi:~> /opt/apache2/bin/ab -c3  -n300 http://localhost:8081/recipes/index.pl

Of cource this is copied and pasted from my shell. Your results will be different.

I searched CPAN and after realizing that there are too many alternatives I decided to have some fun and write another one -- simple, stupid and naive.

After implementing my naive caching in a database table I have the following results:

    Requests per second:    58.35 [#/sec] (mean)
    Time per request:       51.418 [ms] (mean)
    Time per request:       17.139 [ms] (mean, across all concurrent requests)
    Transfer rate:          206.74 [Kbytes/sec] received

Not bad, I would say.


    This module is not necessarily compatible with the L<Cache|Cache> interface nor near complete.


head2 new

The constructor.

    our $cache_obj = CGI::Ex::Recipes::Cache->new(
        {cache_hash =>\%CACHE_HASH, dbh=>$dbh , expires =>3600*24 }
    Arguments(a hashref):
        cache_hash: Applicaton-wide HASH reference.
                    useful only under mod_perl.
        dbh:        A DBI object.
        expires:    Default expiration for cache entries. Default:time + 3600(one hour)
    Returns:    $self - The cache object used in CGI::Ex::Recipes


    my $bool = $cache->exists('somekey');

Checks for existence of a given key in $self->{cache_hash}. Returns 1 on success, 0 otherwize.


    if( $out = $app->cache->get($cache_key) ){ return $out; }

Returns the value of a cache entry. First tries to get it from the $self->{cache_hash} then tries to find it in the databse. If the entry is not available or is expired, returns undef.


    $app->cache->set($cache_key, $value [,$expires]);

Inserts a cache entry in the cache database table and returns the result of the operation.

    NOTE:underlying SQL code is currently compatible only with SQLITE and MySQL

The get/set methods DO NOT serialize complex data types. Use freeze/thaw as appropriate.


Clears all entries from the <cache> table and VACUUMS the database. This method should be called immediately after you INSERT or UPDATE something in the database.

    #... in CGI::Ex::Recipes::Edit::finalize
    return 1;

It is an 'all or nothing' sollution.


    $app->cache->freze($cache_key, $struct [,$expires]);

Identical to 'set', except that c<$struct> may be any complex data type that will be serialized via Storable.


    my $struct = $app->cache->thaw($cache_key);

Identical to 'get', except that it will return a complex data type that was set via 'freeze'.


The set and get methods only allow for working with simple scalar types, but if you want to store more complex types they need to be serialized first. To assist with this, the freeze and thaw methods are provided.


Generalize the underlying SQL code


Cache Cache::Entry


Красимир Беров, <k.berov at gmail.com>


Copyright 2007 Красимир Беров, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: