modperl-tie_cache_module

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Date: Mon, 19 Mar 2001 11:28:06 -0800
From: Joshua Chamas <joshua@chamas.com>
To: Mod Perl <modperl@apache.org>
Subject: [ANNOUNCE] Tie::Cache v.11

Hey,

Tie::Cache v.11 is in your local CPAN or
  http://www.perl.com/CPAN-local/modules/by-module/Tie/

Its a memory size limiting LRU cache based on key count, or
total bytes approximation, useful for mod_perl environments
where you want to limit the amount of memory your process
is taking.

Some of the README is below.  Recent changes include
a better MaxBytes algorithm, where its takes into account
total Tie::Cache memory overhead, with a partial rewrite
using arrays internally instead of hashes for elements
in the doubly linked list.  The latter was done for speed.
  
-- Josh

NAME
    Tie::Cache - LRU Cache in Memory

SYNOPSIS
     use Tie::Cache;
     tie %cache, 'Tie::Cache', 100, { Debug => 1 };   
     tie %cache2, 'Tie::Cache', { MaxCount => 100, MaxBytes => 50000 };
     tie %cache3, 'Tie::Cache', 100, { Debug => 1 , WriteSync => 0};   

     # Options ##################################################################
     #
     # Debug =>      0 - DEFAULT, no debugging output
     #               1 - prints cache statistics upon destroying
     #               2 - prints detailed debugging info
     #
     # MaxCount =>   Maximum entries in cache.
     #
     # MaxBytes =>   Maximum bytes taken in memory for cache based on approximate 
     #               size of total cache structure in memory
     #
     #               There is approximately 240 bytes used per key/value pair in the cache for 
     #               the cache data structures, so a cache of 5000 entries would take
     #               at approximately 1.2M plus the size of the data being cached.
     #
     # MaxSize  =>   Maximum size of each cache entry. Larger entries are not cached.
     #                   This helps prevent much of the cache being flushed when 
     #                   you set an exceptionally large entry.  Defaults to MaxBytes/10
     #
     # WriteSync =>  1 - DEFAULT, write() when data is dirtied for 
     #                   TRUE CACHE (see below)
     #               0 - write() dirty data as late as possible, when leaving 
     #                   cache, or when cache is being DESTROY'd
     #
     ############################################################################

     # cache supports normal tied hash functions
     $cache{1} = 2;       # STORE
     print "$cache{1}\n"; # FETCH

     # FIRSTKEY, NEXTKEY
     while(($k, $v) = each %cache) { print "$k: $v\n"; } 
 
     delete $cache{1};    # DELETE
     %cache = ();         # CLEAR

DESCRIPTION
    This module implements a least recently used (LRU) cache in memory
    through a tie interface. Any time data is stored in the tied hash, that
    key/value pair has an entry time associated with it, and as the cache
    fills up, those members of the cache that are the oldest are removed to
    make room for new entries.

    So, the cache only "remembers" the last written entries, up to the size
    of the cache. This can be especially useful if you access great amounts
    of data, but only access a minority of the data a majority of the time.

    The implementation is a hash, for quick lookups, overlaying a doubly
    linked list for quick insertion and deletion. On a WinNT PII 300, writes
    to the hash were done at a rate 3100 per second, and reads from the hash
    at 6300 per second. Work has been done to optimize refreshing cache
    entries that are frequently read from, code like $cache{entry}, which
    moves the entry to the end of the linked list internally.

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu