NAME
Db::Mediasurface::Cache - caches a specified number of key-value pairs, disgarding underused pairs.
VERSION
This document refers to version 0.03 of DB::Mediasurface::Cache, released July 24, 2001.
SYNOPSIS
use Db::Mediasurface::Cache;
my $url = 'http://some.site.com/some/path?version=2';
my $id = undef;
my $cache = Db::Mediasurface::Cache->new( size => 1000 );
unless (defined ($id = $cache->get($url))) { $id = urldecode2id($url); $cache->set($url,$id); }
DESCRIPTION
Overview
Mediasurface relies on retrieving a unique ID for almost every object lookup. This module aims to cache url->id lookups in memory. The module works with a linked list, which is significantly slower at lookups than a standard Perl hash. However, the linked list allows commonly used key-value pairs to be stored towards the 'fresh' end of the list, and seldomly used pairs to drift towards the 'stale' end, from where they will eventually be pushed into oblivion, should the cache reach its maximum size. Basically, it's a trade-off between size and speed - the module will perform best when you need to perform lots of lookups of a wide range of urls, but the majority of lookups are contained within a much smaller subset of urls. Be warned - this module will cause a *reduction* in performance if used for single lookups, or if lookups are non-repetitive.
Constructor
- $cache = Db::Mediasurface::Cache->new(size=>1000);
-
This class method constructs a new cache. the size parameter can be used to set the maximum number of key-value pairs to be cached. If size is omitted, the cache defaults to using a straight hash, which is a *lot* faster than the linked list method, but which has no protection from eating all your available RAM [NOTE: this is a change in behaviour from version 0.02].
Methods
- $cache->set($key_1,$value_1...$key_n,$value_n)
- $cache->set(%key_value_hash)
-
Sets key-value pairs.
- $id = $cache->get($key1);
-
Gets the value of a given key. Returns the value, or undef if the key doesn't exist.
- $cache->unset($key1);
-
Delete the key-value pair specified by the given key.
AUTHOR
Nigel Wetters (nwetters@cpan.org)
COPYRIGHT
Copyright (c) 2001, Nigel Wetters. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.