Bio-EnsEMBL
view release on metacpan or search on metacpan
lib/Bio/EnsEMBL/Utils/Cache.pm view on Meta::CPAN
Tie::Cache - LRU Cache in Memory
=head1 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
=head1 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.
=cut
sub TIEHASH {
my($class, $max_count, $options) = @_;
if(ref($max_count)) {
$options = $max_count;
$max_count = $options->{MaxCount};
}
unless($max_count || $options->{MaxBytes}) {
die('you must specify cache size with either MaxBytes or MaxCount');
}
my $sync = exists($options->{WriteSync}) ? $options->{WriteSync} : 1;
my $self = bless
{
# how many items to cache
max_count=> $max_count,
# max bytes to cache
max_bytes => $options->{MaxBytes},
# max size (in bytes) of an individual cache entry
max_size => $options->{MaxSize} || ($options->{MaxBytes} ? (int($options->{MaxBytes}/10) + 1) : 0),
# class track, so know if overridden subs should be used
'class' => $class,
'subclass' => $class ne 'Tie::Cache' ? 1 : 0,
# current sizes
count=>0,
bytes=>0,
# inner structures
head=>0,
tail=>0,
nodes=>{},
'keys'=>[],
# statistics
hit => 0,
miss => 0,
# config
sync => $sync,
dbg => $options->{Debug} || $Debug
}, $class;
if (($self->{max_bytes} && ! $self->{max_size})) {
die("MaxSize must be defined when MaxBytes is");
( run in 0.522 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )