| 1 | package Tie::Cache::LRU::Expires; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Tie::Cache::LRU 0.21; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | $VERSION="0.55"; | |||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | sub TIEHASH { | ||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | my $class = shift; | |||||||||
| 12 | 1 | my $self = { | |||||||||
| 13 | LRU => undef, | ||||||||||
| 14 | LRUOBJ => undef, | ||||||||||
| 15 | ENTRIES => 100, | ||||||||||
| 16 | EXPIRES => 3600, | ||||||||||
| 17 | @_, | ||||||||||
| 18 | }; | ||||||||||
| 19 | |||||||||||
| 20 | 1 | tie %{$self->{LRU}}, 'Tie::Cache::LRU', $self->{ENTRIES}; | |||||||||
| 21 | 1 | $self->{LRUOBJ}=tied %{$self->{LRU}}; | |||||||||
| 22 | |||||||||||
| 23 | 1 | return bless $self, $class; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 62 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.0
| |||||||||||
| 27 | 62 | my $self =shift; | |||||||||
| 28 | 62 | my $key =shift; | |||||||||
| 29 | |||||||||||
| 30 | 62 | my $value=$self->{LRUOBJ}->FETCH($key); | |||||||||
| 31 | |||||||||||
| 32 | 62 | ▶ | if (defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 41 | my $curtime=time(); | |||||||||
| 34 | 41 | ▶ | if ($curtime <= $value->{BORDER}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 32 | return $value->{VALUE}; | |||||||||
| 36 | } | ||||||||||
| 37 | else { | ||||||||||
| 38 | 9 | return undef; | |||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | else { | ||||||||||
| 42 | 21 | return undef; | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 23 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 23 | my $self = shift; | |||||||||
| 48 | 23 | my $key = shift; | |||||||||
| 49 | 23 | my $val = shift; | |||||||||
| 50 | |||||||||||
| 51 | my $value={ | ||||||||||
| 52 | VALUE => $val, | ||||||||||
| 53 | BORDER => time()+$self->{EXPIRES} | ||||||||||
| 54 | 23 | }; | |||||||||
| 55 | |||||||||||
| 56 | 23 | $self->{LRUOBJ}->STORE($key,$value); | |||||||||
| 57 | 23 | return $val; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||
| 61 | 0 | my $self = shift; | |||||||||
| 62 | 0 | my $key = shift; | |||||||||
| 63 | |||||||||||
| 64 | 0 | $self->{LRUOBJ}->DELETE($key); | |||||||||
| 65 | 0 | return undef; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | ▶ | sub FIRSTKEY { | ||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 1 | return undef; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub NEXTKEY { | ||||||||
|
Subroutine
NEXTKEY: not called CC=1 SCAR=6.9
| |||||||||||
| 73 | 0 | return undef; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 1 | ▶ | sub CLEAR { | ||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 1 | my ($self) = @_; | |||||||||
| 81 | 1 | $self->{LRUOBJ}->CLEAR(); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 3 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: called CC=3 SCAR=11.0
| |||||||||||
| 85 | 3 | my $self = shift; | |||||||||
| 86 | 3 | my $key = shift; | |||||||||
| 87 | 3 | ▶ | if ($self->{LRUOBJ}->EXISTS($key)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 2 | my $curtime=time(); | |||||||||
| 89 | 2 | my $value=$self->{LRUOBJ}->FETCH($key); | |||||||||
| 90 | 2 | ▶ | if ($curtime <= $value->{BORDER}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 1 | return 1; | |||||||||
| 92 | } | ||||||||||
| 93 | else { | ||||||||||
| 94 | 1 | return 0; | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | 1 | return 0; | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 11 | ▶ | sub lru_size { | ||||||||
|
Subroutine
lru_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 103 | 11 | my $self = shift; | |||||||||
| 104 | 11 | my $lru = $self->{LRUOBJ}; | |||||||||
| 105 | 11 | return $lru->curr_size(); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | =pod | ||||||||||
| 109 | |||||||||||
| 110 | =head1 NAME | ||||||||||
| 111 | |||||||||||
| 112 | Tie::Cache::LRU::Expires - Extends Tie::Cache::LRU with expiring | ||||||||||
| 113 | |||||||||||
| 114 | =head1 SYNOPSIS | ||||||||||
| 115 | |||||||||||
| 116 | use Tie::Cache::LRU::Expires; | ||||||||||
| 117 | |||||||||||
| 118 | tie %cache, 'Tie::Cache::LRU::Expires', EXPIRES => 10, ENTRIES => 1000; | ||||||||||
| 119 | $cache_obj = tied %cache; | ||||||||||
| 120 | |||||||||||
| 121 | for(1..1000) { | ||||||||||
| 122 | $cache{$_}="test $_"; | ||||||||||
| 123 | } | ||||||||||
| 124 | sleep 4; | ||||||||||
| 125 | for(1000..1500) { | ||||||||||
| 126 | $cache{$_}="test $_"; | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | print $cache_obj->lru_size(),"\n"; # access to the | ||||||||||
| 130 | # number of entries | ||||||||||
| 131 | # used in the LRU | ||||||||||
| 132 | # cache. | ||||||||||
| 133 | |||||||||||
| 134 | sleep 4; | ||||||||||
| 135 | for(1..10) { print $cache{$_},"\n"; } | ||||||||||
| 136 | for(1100..1110) { print $cache{$_},"\n"; } | ||||||||||
| 137 | sleep 4; | ||||||||||
| 138 | for(1..10) { print $cache{$_},"\n"; } # expired (undefs). | ||||||||||
| 139 | for(1100..1110) { print $cache{$_},"\n"; } | ||||||||||
| 140 | sleep 4; | ||||||||||
| 141 | for(1100..1110) { print $cache{$_},"\n"; } # now also expired. | ||||||||||
| 142 | |||||||||||
| 143 | # Clearing the cache | ||||||||||
| 144 | |||||||||||
| 145 | %cache=(); | ||||||||||
| 146 | |||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | =head1 DESCRIPTION | ||||||||||
| 150 | |||||||||||
| 151 | This is an expiring LRU cache, using Tie::Cache::LRU. Each entry | ||||||||||
| 152 | in this cache expires after 'EXPIRES' seconds (default 3600). The | ||||||||||
| 153 | cache is in RAM (see Tie::Cache::LRU). ENTRIES provides the maximum | ||||||||||
| 154 | number of entries in the Tie::Cache::LRU cache. | ||||||||||
| 155 | |||||||||||
| 156 | It works by checking if a cached entry hasn't expired. If it has, | ||||||||||
| 157 | undef is returned, otherwise it's value. If the entry wasn't cached, | ||||||||||
| 158 | undef is also returned (of course). Expired entries will eventually | ||||||||||
| 159 | drop of the LRU; or, if referenced will (as can be expected, otherwise | ||||||||||
| 160 | they wouldn't be referenced) be refreshed. | ||||||||||
| 161 | |||||||||||
| 162 | NB! If entries keep being referenced and are expired, but never refreshed, | ||||||||||
| 163 | they will never leave the LRU! | ||||||||||
| 164 | |||||||||||
| 165 | =head1 SEE ALSO | ||||||||||
| 166 | |||||||||||
| 167 | L<Tie::Cache::LRU>. | ||||||||||
| 168 | |||||||||||
| 169 | =head1 USAGE | ||||||||||
| 170 | |||||||||||
| 171 | See SYNOPSIS. Too simple to explain. | ||||||||||
| 172 | |||||||||||
| 173 | =head1 AUTHOR | ||||||||||
| 174 | |||||||||||
| 175 | Hans Oesterholt-Dijkema <oesterhol@cpan.org> | ||||||||||
| 176 | |||||||||||
| 177 | =head1 LICENSE | ||||||||||
| 178 | |||||||||||
| 179 | Artistic | ||||||||||
| 180 | |||||||||||
| 181 | =cut | ||||||||||
| 182 | |||||||||||
| 183 | 1 | 1; | |||||||||