Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

import Memoize 0.52 from CPAN

git-cpan-module:   Memoize
git-cpan-version:  0.52
git-cpan-authorid: MJD
git-cpan-file:     authors/id/M/MJ/MJD/Memoize-0.52.tar.gz
  • Loading branch information...
commit 8f310501fea515efdc1263ddc4a5f8fadecc5546 1 parent f61348b
@mjdominus authored schwern committed
View
1  MANIFEST
@@ -8,6 +8,7 @@ Memoize/AnyDBM_File.pm
Memoize/Storable.pm
Memoize/Expire.pm
Memoize/ExpireTest.pm
+Memoize/Saves.pm
t/correctness.t
t/prototype.t
t/array.t
View
2  Makefile.PL
@@ -31,7 +31,7 @@ neighborhood Perl hacker.
WriteMakefile(
NAME => 'Memoize',
- VERSION => '0.51',
+ VERSION => '0.52',
# 'linkext' => {LINKTYPE => ''},
'dist' => {COMPRESS => 'gzip', SUFFIX => 'gz'},
);
View
4 Memoize.pm
@@ -8,10 +8,10 @@
# same terms as Perl itself. If in doubt,
# write to mjd-perl-memoize+@plover.com for a license.
#
-# Version 0.51 beta $Revision: 1.14 $ $Date: 1999/09/17 14:55:57 $
+# Version 0.52 beta $Revision: 1.14 $ $Date: 1999/09/17 14:55:57 $
package Memoize;
-$VERSION = '0.51';
+$VERSION = '0.52';
View
10 Memoize/Expire.pm
@@ -179,7 +179,6 @@ 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.
-
Short summary: You need to create a package that defines four methods:
=over 4
@@ -282,7 +281,14 @@ entirely absent or was older than ten seconds.
It's nice if you allow a C<TIE> argument to C<TIEHASH> that ties the
underlying cache so that the user can specify that the cache is
persistent or that it has some other interesting semantics. The
-sample C<Memoize::Expire> module demonstrates how to do this.
+sample C<Memoize::Expire> module demonstrates how to do this. It
+implements a policy that expires cache items when they get too old or
+when they have been accessed too many times.
+
+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.
=head1 ALTERNATIVES
View
197 Memoize/Saves.pm
@@ -0,0 +1,197 @@
+package Memoize::Saves;
+
+$DEBUG = 0;
+
+sub TIEHASH
+{
+ my ($package, %args) = @_;
+ my %cache;
+
+ # Convert the CACHE to a referenced hash for quick lookup
+ #
+ if( $args{CACHE} )
+ {
+ my %hash;
+ $args{CACHE} = [ $args{CACHE} ] unless ref $args{CACHE} eq "ARRAY";
+ foreach my $value ( @{$args{CACHE}} )
+ {
+ $hash{$value} = 1;
+ }
+ $args{CACHE} = \%hash;
+ }
+
+ # Convert the DUMP list to a referenced hash for quick lookup
+ #
+ if( $args{DUMP} )
+ {
+ my %hash;
+ $args{DUMP} = [ $args{DUMP} ] unless ref $args{DUMP} eq "ARRAY";
+ foreach my $value ( @{$args{DUMP}} )
+ {
+ $hash{$value} = 1;
+ }
+ $args{DUMP} = \%hash;
+ }
+
+ if ($args{TIE})
+ {
+ my ($module, @opts) = @{$args{TIE}};
+ my $modulefile = $module . '.pm';
+ $modulefile =~ s{::}{/}g;
+ eval { require $modulefile };
+ if ($@) {
+ die "Memoize::Saves: Couldn't load hash tie module `$module': $@; aborting";
+ }
+ my $rc = (tie %cache => $module, @opts);
+ unless ($rc) {
+ die "Memoize::Saves: Couldn't tie hash to `$module': $@; aborting";
+ }
+ }
+
+ $args{C} = \%cache;
+ bless \%args => $package;
+}
+
+sub EXISTS
+{
+ my $self = shift;
+ my $key = shift;
+
+ if( exists $self->{C}->{$key} )
+ {
+ return 1;
+ }
+
+ return 0;
+}
+
+
+sub FETCH
+{
+ my $self = shift;
+ my $key = shift;
+
+ return $self->{C}->{$key};
+}
+
+sub STORE
+{
+ my $self = shift;
+ my $key = shift;
+ my $value = shift;
+
+ # If CACHE defined and this is not in our list don't save it
+ #
+ if(( defined $self->{CACHE} )&&
+ ( ! defined $self->{CACHE}->{$value} ))
+ {
+ print "$value not in CACHE list.\n" if $DEBUG;
+ return;
+ }
+
+ # If DUMP is defined and this is in our list don't save it
+ #
+ if(( defined $self->{DUMP} )&&
+ ( defined $self->{DUMP}->{$value} ))
+ {
+ print "$value in DUMP list.\n" if $DEBUG;
+ return;
+ }
+
+ # If REGEX is defined we will store it only if its true
+ #
+ if(( defined $self->{REGEX} )&&
+ ( $value !~ /$self->{REGEX}/ ))
+ {
+ print "$value did not match regex.\n" if $DEBUG;
+ return;
+ }
+
+ # If we get this far we should save the value
+ #
+ print "Saving $key:$value\n" if $DEBUG;
+ $self->{C}->{$key} = $value;
+}
+
+1;
+
+# Documentation
+#
+
+=head1 NAME
+
+Memoize::Saves - Plug-in module to specify which return values should be memoized
+
+=head1 SYNOPSIS
+
+ use Memoize;
+
+ memoize 'function',
+ SCALAR_CACHE => [TIE, Memoize::Saves,
+ CACHE => [ "word1", "word2" ],
+ DUMP => [ "word3", "word4" ],
+ REGEX => "Regular Expression",
+ TIE => [Module, args...],
+ ],
+
+=head1 DESCRIPTION
+
+Memoize::Saves is a plug-in module for Memoize. It allows the
+user to specify which values should be cached or which should be
+dumped. Please read the manual for Memoize for background
+information.
+
+Use the CACHE option to specify a list of return values which should
+be memoized. All other values will need to be recomputed each time.
+
+Use the DUMP option to specify a list of return values which should
+not be memoized. Only these values will need to be recomputed each
+time.
+
+Use the REGEX option to specify a Regular Expression which must match
+for the return value to be saved. You can supply either a plain text
+string or a compiled regular expression using qr//. Obviously the
+second method is prefered.
+
+Specifying multiple options will result in the least common denominator
+being saved.
+
+You can use the TIE option to string multiple Memoize Plug-ins together:
+
+
+memoize ('printme',
+ SCALAR_CACHE =>
+ [TIE, Memoize::Saves,
+ REGEX => qr/my/,
+ TIE => [Memoize::Expire,
+ LIFETIME => 5,
+ TIE => [ GDBM_File, $filename,
+ O_RDWR | O_CREAT, 0666]
+ ]
+ ]
+ );
+
+
+=head1 CAVEATS
+
+This module is experimental, and may contain bugs. Please report bugs
+to the address below.
+
+If you are going to use Memoize::Saves with Memoize::Expire it is
+import to use it in that order. Memoize::Expire changes the return
+value to include expire information and it may no longer match
+your CACHE, DUMP, or REGEX.
+
+
+=head1 AUTHOR
+
+Joshua Gerth <gerth@teleport.com>
+
+=head1 SEE ALSO
+
+perl(1)
+
+The Memoize man page.
+
+
+
View
6 t/tie.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl
use lib qw(. ..);
-use Memoize 0.45 qw(memoize unmemoize);
+use Memoize 0.52 qw(memoize unmemoize);
use Fcntl;
use Memoize::AnyDBM_File;
@@ -39,8 +39,8 @@ $file = catfile($tmpdir, "md$$");
}
-# tryout('Memoize::AnyDBM_File', $file, 1); # Test 1..4
-tryout('DB_File', $file, 1); # Test 1..4
+tryout('Memoize::AnyDBM_File', $file, 1); # Test 1..4
+# tryout('DB_File', $file, 1); # Test 1..4
unlink $file, "$file.dir", "$file.pag";
sub tryout {
Please sign in to comment.
Something went wrong with that request. Please try again.