2 # A class implimenting LRU sematics with hash look up
4 # Copyright (c) 2002 Dirk Koopman, Tobit Computer Co Ltd
8 # The structure of the objects stored are:-
10 # [next, prev, obj, callsign]
12 # The structure of the base is:-
14 # [next, prev, max objects, count ]
29 use vars qw($VERSION $BRANCH);
30 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
31 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
32 $main::build += $VERSION;
33 $main::branch += $BRANCH;
40 confess "LRU->newbase requires a name and maximal count" unless $name && $max;
41 return $pkg->SUPER::new({ }, $max, 0, $name);
46 my ($self, $call) = @_;
47 if (my $p = $self->obj->{$call}) {
48 dbg("LRU $self->[5] cache hit $call") if isdbg('lru');
57 my ($self, $call, $ref) = @_;
58 confess("need a call and a reference") unless defined $call && $ref;
59 my $p = $self->obj->{$call};
61 # update the reference and rechain it
62 dbg("LRU $self->[5] cache update $call") if isdbg('lru');
66 # delete one of the end of the chain if required
67 while ($self->[4] >= $self->[3] ) {
70 dbg("LRU $self->[5] cache LRUed out $call now $self->[4]/$self->[3]") if isdbg('lru');
75 dbg("LRU $self->[5] cache add $call now $self->[4]/$self->[3]") if isdbg('lru');
76 $p = $self->new($ref, $call);
78 $self->obj->{$call} = $p;
85 my ($self, $call) = @_;
86 my $q = $self->obj->{$call};
87 confess("$call is already removed") unless $q;
88 dbg("LRU $self->[5] cache remove $call now $self->[4]/$self->[3]") if isdbg('lru');
91 delete $self->obj->{$call};