2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>Memoize::Expire - Plug-in module for automatic expiration of memoized values
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> Memoize::Expire - Plug-in module for automatic expiration of memoized values
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
25 <li><a href=
"#interface">INTERFACE
</a></li>
26 <li><a href=
"#alternatives">ALTERNATIVES
</a></li>
27 <li><a href=
"#caveats">CAVEATS
</a></li>
28 <li><a href=
"#author">AUTHOR
</a></li>
29 <li><a href=
"#see_also">SEE ALSO
</a></li>
36 <h1><a name=
"name">NAME
</a></h1>
37 <p>Memoize::Expire - Plug-in module for automatic expiration of memoized values
</p>
41 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
45 tie my %cache =
> 'Memoize::Expire',
46 LIFETIME =
> $lifetime, # In seconds
47 NUM_USES =
> $n_uses;
</pre>
49 memoize 'function', SCALAR_CACHE =
> [HASH =
> \%cache ];
</pre>
53 <h1><a name=
"description">DESCRIPTION
</a></h1>
54 <p>Memoize::Expire is a plug-in module for Memoize. It allows the cached
55 values for memoized functions to expire automatically. This manual
56 assumes you are already familiar with the Memoize module. If not, you
57 should study that manual carefully first, paying particular attention
58 to the HASH feature.
</p>
59 <p>Memoize::Expire is a layer of software that you can insert in between
60 Memoize itself and whatever underlying package implements the cache.
61 The layer presents a hash variable whose values expire whenever they
62 get too old, have been used too often, or both. You tell
<code>Memoize
</code> to
63 use this forgetful hash as its cache instead of the default, which is
65 <p>To specify a real-time timeout, supply the
<code>LIFETIME
</code> option with a
66 numeric value. Cached data will expire after this many seconds, and
67 will be looked up afresh when it expires. When a data item is looked
68 up afresh, its lifetime is reset.
</p>
69 <p>If you specify
<code>NUM_USES
</code> with an argument of
<em>n
</em>, then each cached
70 data item will be discarded and looked up afresh after the
<em>n
</em>th time
71 you access it. When a data item is looked up afresh, its number of
73 <p>If you specify both arguments, data will be discarded from the cache
74 when either expiration condition holds.
</p>
75 <p>Memoize::Expire uses a real hash internally to store the cached data.
76 You can use the
<code>HASH
</code> option to Memoize::Expire to supply a tied
77 hash in place of the ordinary hash that Memoize::Expire will normally
78 use. You can use this feature to add Memoize::Expire as a layer in
79 between a persistent disk hash and Memoize. If you do this, you get a
80 persistent disk cache whose entries expire automatically. For
85 # Memoize::Expire enforces data expiration policy
87 # DB_File implements persistence of data in a disk file
96 tie my %disk_cache =
> 'DB_File', $filename, O_CREAT|O_RDWR,
0666];
</pre>
98 # Set up expiration policy, supplying persistent hash as a target
99 tie my %cache =
> 'Memoize::Expire',
100 LIFETIME =
> $lifetime, # In seconds
101 NUM_USES =
> $n_uses,
102 HASH =
> \%disk_cache;
</pre>
104 # Set up memoization, supplying expiring persistent hash for cache
105 memoize 'function', SCALAR_CACHE =
> [ HASH =
> \%cache ];
</pre>
109 <h1><a name=
"interface">INTERFACE
</a></h1>
110 <p>There is nothing special about Memoize::Expire. It is just an
111 example. If you don't like the policy that it implements, you are
112 free to write your own expiration policy module that implements
113 whatever policy you desire. Here is how to do that. Let us suppose
114 that your module will be named MyExpirePolicy.
</p>
115 <p>Short summary: You need to create a package that defines four methods:
</p>
117 <dt><strong><a name=
"item_tiehash">TIEHASH
</a></strong>
120 <p>Construct and return cache object.
</p>
123 <dt><strong><a name=
"item_exists">EXISTS
</a></strong>
126 <p>Given a function argument, is the corresponding function value in the
127 cache, and if so, is it fresh enough to use?
</p>
130 <dt><strong><a name=
"item_fetch">FETCH
</a></strong>
133 <p>Given a function argument, look up the corresponding function value in
134 the cache and return it.
</p>
137 <dt><strong><a name=
"item_store">STORE
</a></strong>
140 <p>Given a function argument and the corresponding function value, store
141 them into the cache.
</p>
144 <dt><strong><a name=
"item_clear">CLEAR
</a></strong>
147 <p>(Optional.) Flush the cache completely.
</p>
151 <p>The user who wants the memoization cache to be expired according to
152 your policy will say so by writing
</p>
154 tie my %cache =
> 'MyExpirePolicy', args...;
155 memoize 'function', SCALAR_CACHE =
> [HASH =
> \%cache];
</pre>
156 <p>This will invoke
<a href=
"#item_tiehash"><code>MyExpirePolicy-
>TIEHASH(args)
</code></a>.
157 MyExpirePolicy::TIEHASH should do whatever is appropriate to set up
158 the cache, and it should return the cache object to the caller.
</p>
159 <p>For example, MyExpirePolicy::TIEHASH might create an object that
160 contains a regular Perl hash (which it will to store the cached
161 values) and some extra information about the arguments and how old the
162 data is and things like that. Let us call this object `C'.
</p>
163 <p>When Memoize needs to check to see if an entry is in the cache
164 already, it will invoke
<a href=
"#item_exists"><code>C-
>EXISTS(key)
</code></a>.
<code>key
</code> is the normalized
165 function argument. MyExpirePolicy::EXISTS should return
0 if the key
166 is not in the cache, or if it has expired, and
1 if an unexpired value
167 is in the cache. It should
<em>not
</em> return
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef
</code></a>, because there is a
168 bug in some versions of Perl that will cause a spurious FETCH if the
169 EXISTS method returns
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef
</code></a>.
</p>
170 <p>If your EXISTS function returns true, Memoize will try to fetch the
171 cached value by invoking
<a href=
"#item_fetch"><code>C-
>FETCH(key)
</code></a>. MyExpirePolicy::FETCH should
172 return the cached value. Otherwise, Memoize will call the memoized
173 function to compute the appropriate value, and will store it into the
174 cache by calling
<a href=
"#item_store"><code>C-
>STORE(key, value)
</code></a>.
</p>
175 <p>Here is a very brief example of a policy module that expires each
176 cache item after ten seconds.
</p>
178 package Memoize::TenSecondExpire;
</pre>
181 my ($package, %args) = @_;
182 my $cache = $args{HASH} || {};
183 bless $cache =
> $package;
187 my ($cache, $key) = @_;
188 if (exists $cache-
>{$key}
&&
189 $cache-
>{$key}{EXPIRE_TIME}
> time) {
192 return
0; # Do NOT return `undef' here.
197 my ($cache, $key) = @_;
198 return $cache-
>{$key}{VALUE};
202 my ($cache, $key, $newvalue) = @_;
203 $cache-
>{$key}{VALUE} = $newvalue;
204 $cache-
>{$key}{EXPIRE_TIME} = time +
10;
206 <p>To use this expiration policy, the user would say
</p>
209 tie my %cache10sec =
> 'Memoize::TenSecondExpire';
210 memoize 'function', SCALAR_CACHE =
> [HASH =
> \%cache10sec];
</pre>
211 <p>Memoize would then call
<code>function
</code> whenever a cached value was
212 entirely absent or was older than ten seconds.
</p>
213 <p>You should always support a
<code>HASH
</code> argument to
<a href=
"#item_tiehash"><code>TIEHASH
</code></a> that ties
214 the underlying cache so that the user can specify that the cache is
215 also persistent or that it has some other interesting semantics. The
216 example above demonstrates how to do this, as does
<code>Memoize::Expire
</code>.
</p>
220 <h1><a name=
"alternatives">ALTERNATIVES
</a></h1>
221 <p>Brent Powers has a
<code>Memoize::ExpireLRU
</code> module that was designed to
222 work with Memoize and provides expiration of least-recently-used data.
223 The cache is held at a fixed number of entries, and when new data
224 comes in, the least-recently used data is expired. See
225 <a href=
"http://search.cpan.org/search?mode=module&query=ExpireLRU">http://search.cpan.org/search
</a>.
</p>
226 <p>Joshua Chamas's Tie::Cache module may be useful as an expiration
227 manager. (If you try this, let me know how it works out.)
</p>
228 <p>If you develop any useful expiration managers that you think should be
229 distributed with Memoize, please let me know.
</p>
233 <h1><a name=
"caveats">CAVEATS
</a></h1>
234 <p>This module is experimental, and may contain bugs. Please report bugs
235 to the address below.
</p>
236 <p>Number-of-uses is stored as a
16-bit unsigned integer, so can't exceed
238 <p>Because of clock granularity, expiration times may occur up to one
239 second sooner than you expect. For example, suppose you store a value
240 with a lifetime of ten seconds, and you store it at
12:
00:
00.998 on a
241 certain day. Memoize will look at the clock and see
12:
00:
00. Then
242 9.01 seconds later, at
12:
00:
10.008 you try to read it back. Memoize
243 will look at the clock and see
12:
00:
10 and conclude that the value
244 has expired. This will probably not occur if you have
245 <code>Time::HiRes
</code> installed.
</p>
249 <h1><a name=
"author">AUTHOR
</a></h1>
250 <p>Mark-Jason Dominus (mjd-perl-memoize+@plover.com)
</p>
251 <p>Mike Cariaso provided valuable insight into the best way to solve this
256 <h1><a name=
"see_also">SEE ALSO
</a></h1>
257 <p><code>perl(
1)
</code></p>
258 <p>The Memoize man page.
</p>
259 <p><a href=
"http://www.plover.com/~mjd/perl/Memoize/">http://www.plover.com/~mjd/perl/Memoize/
</a> (for news and updates)
</p>
260 <p>I maintain a mailing list on which I occasionally announce new
261 versions of Memoize. The list is for announcements only, not
262 discussion. To join, send an empty message to
263 <a href=
"mailto:mjd-perl-memoize-request@Plover.com.">mjd-perl-memoize-request@Plover.com.
</a></p>
264 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
265 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
266 <big><strong><span class=
"block"> Memoize::Expire - Plug-in module for automatic expiration of memoized values
</span></strong></big>