Nigel Wetters Gourlay


Db::Mediasurface::Cache - caches a specified number of key-value pairs, disgarding underused pairs.


This document refers to version 0.04 of DB::Mediasurface::Cache, released August 3, 2001.


use Db::Mediasurface::Cache;

my $url = '';

my $id = undef;

my $cache = Db::Mediasurface::Cache->new( size => 1000 );

unless (defined ($id = $cache->get($url))) { $id = urldecode2id($url); $cache->set($url,$id); }



Mediasurface relies on retrieving a unique ID for almost every object lookup. This module aims to cache url->id lookups in memory. The module allows commonly used key-value pairs to be stored towards the 'fresh' end of the store, 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.


$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 an infinite store, which has no protection from eating all your available RAM [NOTE: this is a change in behaviour from version 0.02].



Sets key-value pairs. [Note that this method can now only accept *one* key-value pair. This is a change of behaviour from version 0.03].

$id = $cache->get($key);

Gets the value of a given key. Returns the value, or undef if the key doesn't exist.


Delete the key-value pair specified by the given key.


Nigel Wetters (


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.