# require 5.00556;
use Carp;
$DEBUG = 0;
-$VERSION = '0.65';
+$VERSION = '1.00';
# This package will implement expiration by prepending a fixed-length header
# to the font of the cached data. The format of the header will be:
# TIE => [Memoize::Expire, LIFETIME => sec, NUM_USES => n,
# TIE => [...] ]
+BEGIN {
+ eval {require Time::HiRes};
+ unless ($@) {
+ Time::HiRes->import('time');
+ }
+}
+
sub TIEHASH {
my ($package, %args) = @_;
my %cache;
sub FETCH {
$DEBUG and print STDERR " >> Fetch cached value for $_[1]\n";
my ($data, $last_access, $expire_time, $num_uses_left) = _get_item($_[0]{C}{$_[1]});
- $DEBUG and print STDERR " >> (ttl: ", ($expire_time-time), ", nuses: $num_uses_left)\n";
+ $DEBUG and print STDERR " >> (ttl: ", ($expire_time-time()), ", nuses: $num_uses_left)\n";
$num_uses_left--;
$last_access = time;
_set_header(@_, $data, $last_access, $expire_time, $num_uses_left);
1;
-# Below is the stub of documentation for your module. You better edit it!
-
=head1 NAME
Memoize::Expire - Plug-in module for automatic expiration of memoized values
use Memoize;
use Memoize::Expire;
- tie my %cache => 'Memoize::Expire',
+ tie my %cache => 'Memoize::Expire',
LIFETIME => $lifetime, # In seconds
NUM_USES => $n_uses;
Given a function argument and the corresponding function value, store
them into the cache.
+=item
+CLEAR
+
+(Optional.) Flush the cache completely.
+
=back
The user who wants the memoization cache to be expired according to
sub TIEHASH {
my ($package, %args) = @_;
- my $cache = $args{$HASH} || {};
+ my $cache = $args{HASH} || {};
bless $cache => $package;
}
You should always support a C<HASH> argument to C<TIEHASH> 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 C<Memozie::Expire>.
-
-Another sample module, C<Memoize::Saves>, is included with this
-package. It implements a policy that allows you to specify that
-certain function values whould always be looked up afresh. See the
-documentation for details.
+example above demonstrates how to do this, as does C<Memoize::Expire>.
=head1 ALTERNATIVES
Brent Powers has a C<Memoize::ExpireLRU> module that was designed to
-wotk with Memoize and provides expiration of least-recently-used data.
+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
L<http://search.cpan.org/search?mode=module&query=ExpireLRU>.
to the address below.
Number-of-uses is stored as a 16-bit unsigned integer, so can't exceed
-65535.
+65535.
Because of clock granularity, expiration times may occur up to one
second sooner than you expect. For example, suppose you store a value
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. Solution: Build an expiration policy module that uses
-Time::HiRes to examine a clock with better granularity. Contributions
-are welcome. Send them to:
+has expired. This will probably not occur if you have
+C<Time::HiRes> installed.
=head1 AUTHOR
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.
+mjd-perl-memoize-request@Plover.com.
=cut