forked from KD8EYF/TRBO-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DupeCache.pm
67 lines (44 loc) · 1.07 KB
/
DupeCache.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package TRBO::DupeCache;
=head1 NAME
TRBO::DupeCache - module for caching message IDs and detecting dupes
=head1 ABSTRACT
=head1 FUNCTIONS
=cut
use TRBO::Common;
our @ISA = ('TRBO::Common');
sub init($)
{
my($self) = @_;
$self->{'debug'} = 1;
$self->_debug('init');
$self->{'cache'} = {};
}
sub add($$)
{
my($self, $key) = @_;
if (defined $self->{'cache'}->{$key}) {
$self->_debug("cache hit: $key");
return 1;
}
$self->_debug("add: $key");
$self->{'cache'}->{$key} = time();
return 0;
}
sub scan($$)
{
my($self, $timeout) = @_;
#$self->_debug("scan, timeout $timeout");
my $now = time();
my $expire_below = $now - $timeout;
$c = $self->{'cache'};
foreach my $k (keys %{ $self->{'cache'} }) {
#$self->_debug("checking: $k");
if ($c->{$k} > $now) {
$c->{$k} = $now; # clock jumpd backwards
} elsif ($c->{$k} < $expire_below) {
delete $c->{$k};
#$self->_debug("evicted: $k");
}
}
}
1;