CacheProvider - Multi-Thread Cache System on Perl

It is a module that provider simple cache system on perl.  You can save data in to memory with a specified life time and idle period and take it back later.

HOW TO

    use CacheProvider;

    &initcache( timeout => 86400, limit => 20000 );

    my $id = 15;
    my $domainname = 'abc.com';
    &putcache($id, $domainname);

    my $value = &getcache(15);

    &closecache();

 

You can initialize the cache system with default life time and idle period of cache item.  The unit is second.  The cache will be expired and removed soon when life time (timeout) expired.  The idleperiod makes the cache unavailable if no one access the cache for more than that.  The limit guards the system not to be overflowed.  If the number of cache items over the limit, the last recent used items will be removed until the items count down to 60% of limit.

The cache system is available using in multi-thread environment but remember to share() your data before putcache if the data is array or hash and put it as reference:

share(%hash);
&putcache($key, \%hash);

It is self maintenance by a housekeeping thread.  The closecache helps you to kill the housekeeping thread when exits.  You can maintain by yourself if you don't want to generate the housekeeping thread since it needs 3 seconds to stop in closecache.  Specify noauto => 1 in initcache.  You MUST call housekeepcache every 15-60 minutes by yourself with this option.

You can debug by function dumpcache.  If your data is array or hash, you can specify a function reference to return a string to display.  e.g.  If your is time, you can call like this:

&dumpcache( $filehandle, sub { return  strftime( "%F %T", localtime($_[0]) ); } );

Please take in to consideration that Perl this not efficient!   This is recommended for testcase or R&D only.  For production, Java, C++, or C# is the only choice.

An example that can be used at once cacheprovider.