.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Memoize::Expire 3"
.TH Memoize::Expire 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
Memoize::Expire \- Plug\-in module for automatic expiration of memoized values
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 5
\& use Memoize;
\& use Memoize::Expire;
\& tie my %cache => 'Memoize::Expire',
\& LIFETIME => $lifetime, # In seconds
\& NUM_USES => $n_uses;
.Ve
.PP
.Vb 1
\& memoize 'function', SCALAR_CACHE => [HASH => \e%cache ];
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Memoize::Expire is a plug-in module for Memoize. It allows the cached
values for memoized functions to expire automatically. This manual
assumes you are already familiar with the Memoize module. If not, you
should study that manual carefully first, paying particular attention
to the \s-1HASH\s0 feature.
.PP
Memoize::Expire is a layer of software that you can insert in between
Memoize itself and whatever underlying package implements the cache.
The layer presents a hash variable whose values expire whenever they
get too old, have been used too often, or both. You tell \f(CW\*(C`Memoize\*(C'\fR to
use this forgetful hash as its cache instead of the default, which is
an ordinary hash.
.PP
To specify a real-time timeout, supply the \f(CW\*(C`LIFETIME\*(C'\fR option with a
numeric value. Cached data will expire after this many seconds, and
will be looked up afresh when it expires. When a data item is looked
up afresh, its lifetime is reset.
.PP
If you specify \f(CW\*(C`NUM_USES\*(C'\fR with an argument of \fIn\fR, then each cached
data item will be discarded and looked up afresh after the \fIn\fRth time
you access it. When a data item is looked up afresh, its number of
uses is reset.
.PP
If you specify both arguments, data will be discarded from the cache
when either expiration condition holds.
.PP
Memoize::Expire uses a real hash internally to store the cached data.
You can use the \f(CW\*(C`HASH\*(C'\fR option to Memoize::Expire to supply a tied
hash in place of the ordinary hash that Memoize::Expire will normally
use. You can use this feature to add Memoize::Expire as a layer in
between a persistent disk hash and Memoize. If you do this, you get a
persistent disk cache whose entries expire automatically. For
example:
.PP
.Vb 7
\& # Memoize
\& # |
\& # Memoize::Expire enforces data expiration policy
\& # |
\& # DB_File implements persistence of data in a disk file
\& # |
\& # Disk file
.Ve
.PP
.Vb 3
\& use Memoize;
\& use Memoize::Expire;
\& use DB_File;
.Ve
.PP
.Vb 2
\& # Set up persistence
\& tie my %disk_cache => 'DB_File', $filename, O_CREAT|O_RDWR, 0666];
.Ve
.PP
.Vb 5
\& # Set up expiration policy, supplying persistent hash as a target
\& tie my %cache => 'Memoize::Expire',
\& LIFETIME => $lifetime, # In seconds
\& NUM_USES => $n_uses,
\& HASH => \e%disk_cache;
.Ve
.PP
.Vb 2
\& # Set up memoization, supplying expiring persistent hash for cache
\& memoize 'function', SCALAR_CACHE => [ HASH => \e%cache ];
.Ve
.SH "INTERFACE"
.IX Header "INTERFACE"
There is nothing special about Memoize::Expire. It is just an
example. If you don't like the policy that it implements, you are
free to write your own expiration policy module that implements
whatever policy you desire. Here is how to do that. Let us suppose
that your module will be named MyExpirePolicy.
.PP
Short summary: You need to create a package that defines four methods:
.IP "\s-1TIEHASH\s0" 4
.IX Item "TIEHASH"
Construct and return cache object.
.IP "\s-1EXISTS\s0" 4
.IX Item "EXISTS"
Given a function argument, is the corresponding function value in the
cache, and if so, is it fresh enough to use?
.IP "\s-1FETCH\s0" 4
.IX Item "FETCH"
Given a function argument, look up the corresponding function value in
the cache and return it.
.IP "\s-1STORE\s0" 4
.IX Item "STORE"
Given a function argument and the corresponding function value, store
them into the cache.
.IP "\s-1CLEAR\s0" 4
.IX Item "CLEAR"
(Optional.) Flush the cache completely.
.PP
The user who wants the memoization cache to be expired according to
your policy will say so by writing
.PP
.Vb 2
\& tie my %cache => 'MyExpirePolicy', args...;
\& memoize 'function', SCALAR_CACHE => [HASH => \e%cache];
.Ve
.PP
This will invoke \f(CW\*(C`MyExpirePolicy\->TIEHASH(args)\*(C'\fR.
MyExpirePolicy::TIEHASH should do whatever is appropriate to set up
the cache, and it should return the cache object to the caller.
.PP
For example, MyExpirePolicy::TIEHASH might create an object that
contains a regular Perl hash (which it will to store the cached
values) and some extra information about the arguments and how old the
data is and things like that. Let us call this object `C'.
.PP
When Memoize needs to check to see if an entry is in the cache
already, it will invoke \f(CW\*(C`C\->EXISTS(key)\*(C'\fR. \f(CW\*(C`key\*(C'\fR is the normalized
function argument. MyExpirePolicy::EXISTS should return 0 if the key
is not in the cache, or if it has expired, and 1 if an unexpired value
is in the cache. It should \fInot\fR return \f(CW\*(C`undef\*(C'\fR, because there is a
bug in some versions of Perl that will cause a spurious \s-1FETCH\s0 if the
\&\s-1EXISTS\s0 method returns \f(CW\*(C`undef\*(C'\fR.
.PP
If your \s-1EXISTS\s0 function returns true, Memoize will try to fetch the
cached value by invoking \f(CW\*(C`C\->FETCH(key)\*(C'\fR. MyExpirePolicy::FETCH should
return the cached value. Otherwise, Memoize will call the memoized
function to compute the appropriate value, and will store it into the
cache by calling \f(CW\*(C`C\->STORE(key, value)\*(C'\fR.
.PP
Here is a very brief example of a policy module that expires each
cache item after ten seconds.
.PP
.Vb 1
\& package Memoize::TenSecondExpire;
.Ve
.PP
.Vb 5
\& sub TIEHASH {
\& my ($package, %args) = @_;
\& my $cache = $args{HASH} || {};
\& bless $cache => $package;
\& }
.Ve
.PP
.Vb 9
\& sub EXISTS {
\& my ($cache, $key) = @_;
\& if (exists $cache->{$key} &&
\& $cache->{$key}{EXPIRE_TIME} > time) {
\& return 1
\& } else {
\& return 0; # Do NOT return `undef' here.
\& }
\& }
.Ve
.PP
.Vb 4
\& sub FETCH {
\& my ($cache, $key) = @_;
\& return $cache->{$key}{VALUE};
\& }
.Ve
.PP
.Vb 5
\& sub STORE {
\& my ($cache, $key, $newvalue) = @_;
\& $cache->{$key}{VALUE} = $newvalue;
\& $cache->{$key}{EXPIRE_TIME} = time + 10;
\& }
.Ve
.PP
To use this expiration policy, the user would say
.PP
.Vb 3
\& use Memoize;
\& tie my %cache10sec => 'Memoize::TenSecondExpire';
\& memoize 'function', SCALAR_CACHE => [HASH => \e%cache10sec];
.Ve
.PP
Memoize would then call \f(CW\*(C`function\*(C'\fR whenever a cached value was
entirely absent or was older than ten seconds.
.PP
You should always support a \f(CW\*(C`HASH\*(C'\fR argument to \f(CW\*(C`TIEHASH\*(C'\fR that ties
the underlying cache so that the user can specify that the cache is
also persistent or that it has some other interesting semantics. The
example above demonstrates how to do this, as does \f(CW\*(C`Memoize::Expire\*(C'\fR.
.SH "ALTERNATIVES"
.IX Header "ALTERNATIVES"
Brent Powers has a \f(CW\*(C`Memoize::ExpireLRU\*(C'\fR module that was designed to
work with Memoize and provides expiration of least-recently-used data.
The cache is held at a fixed number of entries, and when new data
comes in, the least-recently used data is expired. See
<http://search.cpan.org/search?mode=module&query=ExpireLRU>.
.PP
Joshua Chamas's Tie::Cache module may be useful as an expiration
manager. (If you try this, let me know how it works out.)
.PP
If you develop any useful expiration managers that you think should be
distributed with Memoize, please let me know.
.SH "CAVEATS"
.IX Header "CAVEATS"
This module is experimental, and may contain bugs. Please report bugs
to the address below.
.PP
Number-of-uses is stored as a 16\-bit unsigned integer, so can't exceed
65535.
.PP
Because of clock granularity, expiration times may occur up to one
second sooner than you expect. For example, suppose you store a value
with a lifetime of ten seconds, and you store it at 12:00:00.998 on a
certain day. Memoize will look at the clock and see 12:00:00. Then
9.01 seconds later, at 12:00:10.008 you try to read it back. Memoize
will look at the clock and see 12:00:10 and conclude that the value
has expired. This will probably not occur if you have
\&\f(CW\*(C`Time::HiRes\*(C'\fR installed.
.SH "AUTHOR"
.IX Header "AUTHOR"
Mark-Jason Dominus (mjd\-perl\-memoize+@plover.com)
.PP
Mike Cariaso provided valuable insight into the best way to solve this
problem.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\fIperl\fR\|(1)
.PP
The Memoize man page.
.PP
http://www.plover.com/~mjd/perl/Memoize/ (for news and updates)
.PP
I maintain a mailing list on which I occasionally announce new
versions of Memoize. The list is for announcements only, not
discussion. To join, send an empty message to
mjd\-perl\-memoize\-request@Plover.com.
|