[Rt-commit] [svn] r1366 - in Cache-Simple-TimedExpiry: . lib/Cache/Simple t

jesse at pallas.eruditorum.org jesse at pallas.eruditorum.org
Thu Aug 26 12:11:17 EDT 2004


Author: jesse
Date: Thu Aug 26 12:11:17 2004
New Revision: 1366

Modified:
   Cache-Simple-TimedExpiry/   (props changed)
   Cache-Simple-TimedExpiry/META.yml
   Cache-Simple-TimedExpiry/Makefile
   Cache-Simple-TimedExpiry/lib/Cache/Simple/TimedExpiry.pm
   Cache-Simple-TimedExpiry/t/01basic.t
Log:
 ----------------------------------------------------------------------
 r8439 at tinbook:  jesse | 2004-08-26T16:10:55.774116Z
 Changed expiry mechanism
 ----------------------------------------------------------------------


Modified: Cache-Simple-TimedExpiry/META.yml
==============================================================================
--- Cache-Simple-TimedExpiry/META.yml	(original)
+++ Cache-Simple-TimedExpiry/META.yml	Thu Aug 26 12:11:17 2004
@@ -1,5 +1,5 @@
 name: Cache-Simple-TimedExpiry
-version: 0.01
+version: 0.20
 abstract: A lightweight cache with timed expiration
 author: Robert Spier <rspier at pobox.com>, Jesse Vincent <jesse at bestpractical.com>
 license: perl

Modified: Cache-Simple-TimedExpiry/Makefile
==============================================================================
--- Cache-Simple-TimedExpiry/Makefile	(original)
+++ Cache-Simple-TimedExpiry/Makefile	Thu Aug 26 12:11:17 2004
@@ -18,7 +18,7 @@
 #     NO_META => q[1]
 #     PL_FILES => {  }
 #     PREREQ_PM => {  }
-#     VERSION => q[0.01]
+#     VERSION => q[0.20]
 #     dist => { PREOP=>q[$(PERL) -I. -MModule::Install::Admin -e "dist_preop(q($(DISTVNAME)))"] }
 
 # --- MakeMaker post_initialize section:
@@ -58,11 +58,11 @@
 DIRFILESEP = /
 NAME = Cache::Simple::TimedExpiry
 NAME_SYM = Cache_Simple_TimedExpiry
-VERSION = 0.01
+VERSION = 0.20
 VERSION_MACRO = VERSION
-VERSION_SYM = 0_01
+VERSION_SYM = 0_20
 DEFINE_VERSION = -D$(VERSION_MACRO)=\"$(VERSION)\"
-XS_VERSION = 0.01
+XS_VERSION = 0.20
 XS_VERSION_MACRO = XS_VERSION
 XS_DEFINE_VERSION = -D$(XS_VERSION_MACRO)=\"$(XS_VERSION)\"
 INST_ARCHLIB = blib/arch
@@ -243,7 +243,7 @@
 DIST_CP = best
 DIST_DEFAULT = tardist
 DISTNAME = Cache-Simple-TimedExpiry
-DISTVNAME = Cache-Simple-TimedExpiry-0.01
+DISTVNAME = Cache-Simple-TimedExpiry-0.20
 
 
 # --- MakeMaker macro section:
@@ -694,7 +694,7 @@
 # --- MakeMaker ppd section:
 # Creates a PPD (Perl Package Description) for a binary distribution.
 ppd:
-	$(NOECHO) $(ECHO) '<SOFTPKG NAME="$(DISTNAME)" VERSION="0,01,0,0">' > $(DISTNAME).ppd
+	$(NOECHO) $(ECHO) '<SOFTPKG NAME="$(DISTNAME)" VERSION="0,20,0,0">' > $(DISTNAME).ppd
 	$(NOECHO) $(ECHO) '    <TITLE>$(DISTNAME)</TITLE>' >> $(DISTNAME).ppd
 	$(NOECHO) $(ECHO) '    <ABSTRACT>A lightweight cache with timed expiration</ABSTRACT>' >> $(DISTNAME).ppd
 	$(NOECHO) $(ECHO) '    <AUTHOR>Robert Spier &lt;rspier at pobox.com&gt;, Jesse Vincent &lt;jesse at bestpractical.com&gt;</AUTHOR>' >> $(DISTNAME).ppd

Modified: Cache-Simple-TimedExpiry/lib/Cache/Simple/TimedExpiry.pm
==============================================================================
--- Cache-Simple-TimedExpiry/lib/Cache/Simple/TimedExpiry.pm	(original)
+++ Cache-Simple-TimedExpiry/lib/Cache/Simple/TimedExpiry.pm	Thu Aug 26 12:11:17 2004
@@ -4,7 +4,7 @@
 
 use vars qw/$VERSION/;
 
-$VERSION = '0.01';
+$VERSION = '0.20';
 
 # 0 - expiration delay
 # 1 - hash
@@ -21,6 +21,28 @@
   bless [2,{},[]], "Cache::Simple::TimedExpiry";
 }
 
+
+=head2 expire_after SECONDS
+
+Set the cache's expiry policy to expire entries after SECONDS seconds. Setting this changes the expiry policy for pre-existing cache entries and for new ones.
+
+
+=cut
+
+sub expire_after {
+    my $self = shift;
+    $self->[0] = shift if (@_);
+    return ($self->[0]);
+
+}
+
+
+=head2 has_key KEY
+
+Return true if the cache has an entry with the key KEY
+
+=cut
+
 sub has_key ($$) { # exists
   my ($self, $key) = @_;
   
@@ -28,6 +50,13 @@
   return 0;
 }
 
+=head2 fetch KEY
+
+Return the cache entry with key KEY.
+Returns undef if there is no such entry
+
+=cut
+
 sub fetch ($$) {
   my ($self,$key) = @_;
 
@@ -40,38 +69,35 @@
 
 }
 
-=head2 store KEY VALUE EXPIRYTIME
+=head2 store KEY VALUE
 
 Store VALUE in the cache with accessor KEY.  Expire it from the cache 
 at or after EXPIRYTIME.
 
-EXPIRYTIME is in seconds from now
-
-Setting EXPIRYTIME to 0 means "Never expire"
-
-
 =cut
 
 sub set ($$$) {
-  my ($self,$key,$value, $expiry) = @_;
+  my ($self,$key,$value) = @_;
 
   $self->expire();
 
   $self->[1]->{$key} = $value;
-  if ($expiry) {
 
-    push @{$self->[2]}, [ ($expiry+time), $key ];
-    }
+    push @{$self->[2]}, [ time, $key ];
 }
 
 sub expire ($) {
   my ($self) = @_;
 
-  my $now = time;
+  my $oldest_nonexpired_entry = (time - $self->[0]);
+ 
+
   return unless defined $self->[2]->[0]; # do we have an element in the array?
-  return unless $self->[2]->[0]->[0] < $now; # is it expired?
 
-  while ( @{$self->[2]} && $self->[2]->[0]->[0] < $now ) {
+
+  return unless $self->[2]->[0]->[0] < $oldest_nonexpired_entry; # is it expired?
+
+  while ( @{$self->[2]} && $self->[2]->[0]->[0] <$oldest_nonexpired_entry ) {
     my $key =  $self->[2]->[0]->[1];
     delete $self->[1]->{ $key } if exists $self->[1]->{$key};
     shift @{$self->[2]};

Modified: Cache-Simple-TimedExpiry/t/01basic.t
==============================================================================
--- Cache-Simple-TimedExpiry/t/01basic.t	(original)
+++ Cache-Simple-TimedExpiry/t/01basic.t	Thu Aug 26 12:11:17 2004
@@ -4,19 +4,23 @@
 
 my $h = new Cache::Simple::TimedExpiry;
 
-is(ref($h), 'Cache::Simple::TimedExpiry');
-
-$h->set( Forever => "Don't expire", 0);
+is ($h->expire_after(),2);
+$h->expire_after(10);
+is ($h->expire_after(),10);
 
-is ($h->fetch('Forever'), "Don't expire");
+is(ref($h), 'Cache::Simple::TimedExpiry');
 
-$h->set( 'Temp' => 'Temporary', 2);
-;
+$h->set( 'Temp' => 'Temporary');
+sleep(8);
+$h->set( 'Temp2' => 'Temporary');
 is ($h->fetch('Temp'), 'Temporary');
+is ($h->fetch('Temp2'), 'Temporary');
+sleep(5);
 
-sleep 3;
 
+is ($h->fetch('Temp2'), 'Temporary');
 is ($h->fetch('Temp'), undef);
 
-is ($h->fetch('Forever'), "Don't expire");
+sleep(6);
+is ($h->fetch('Temp2'), undef);
 


More information about the Rt-commit mailing list