| 1 | package Bot::Cobalt::Plugin::RDB::SearchCache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $Bot::Cobalt::Plugin::RDB::SearchCache::VERSION = '0.021003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## This is a fairly generic in-memory cache object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ## It's intended for use with Plugin::RDB, but will likely work for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | ## just about situation where you want to store a set number of keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | ## mapping an identifier to an array reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | ## This can be useful for caching the results of deep searches against | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | ## Bot::Cobalt::DB instances, for example. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | ## This may get moved out to the core lib directory, in which case this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ## module will become a placeholder. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my $self = +{ Cache => +{} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | $self->MaxKeys($opts{MaxKeys} || 30); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'MaxKeys'} || 30
MC/DC0%0/1
$opts{'MaxKeys'} || 30
$opts{'MaxKeys'}
Truth table50%1/2
A
A= $opts{'MaxKeys'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | $self | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 8 | ▶ | sub cache { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cache: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | my ($self, $ckey, $match, $resultset) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ## should be passed rdb, search str, and array of matching indices | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 8 | ▶ | return unless defined $ckey and defined $match; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ckey and defined $match
MC/DC0%0/2
defined $ckey and defined $match
defined $ckey
defined $match
Truth table33%1/3
A and B
A= defined $ckeyB= defined $match
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 8 | ▶ | $resultset = [] unless ref $resultset eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ## _shrink will do the right thing depending on size of cache | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ## (MaxKeys can be used to adjust cachesize per-rdb 'on the fly') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 8 | $self->_shrink($ckey); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 8 | $self->{Cache}->{$ckey}->{$match} = +{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | TS => Time::HiRes::time(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | Results => $resultset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=19.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my ($self, $ckey, $match) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | unless defined $ckey | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | and defined $match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | and exists $self->{Cache}->{$ckey} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | ▶ | and $self->{Cache}->{$ckey}->{$match}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
defined $ckey and defined $match and exists $self->{"Cache"}{$ckey} and $self->{"Cache"}{$ckey}{$match}
defined $ckey and defined $match and exists $self->{"Cache"}{$ckey}
defined $ckey and defined $match
MC/DC25%1/4
defined $ckey and defined $match and exists $self->{"Cache"}{$ckey} and $self->{"Cache"}{$ckey}{$match}
defined $ckey
defined $match
exists $self->{"Cache"}{$ckey}
$self->{"Cache"}{$ckey}{$match}
Truth table40%2/5
A and B and C and D
A= defined $ckeyB= defined $matchC= exists $self->{"Cache"}{$ckey}D= $self->{"Cache"}{$ckey}{$match}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | my $ref = $self->{Cache}->{$ckey}->{$match}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | wantarray ? @{ $ref->{Results} } : $ref->{Results} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | sub MaxKeys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
MaxKeys: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | my ($self, $max) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 10 | ▶ | return $self->{MAX_KEYS} = $max if defined $max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | $self->{MAX_KEYS} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub invalidate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
invalidate: called CC=5 SCAR=19.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | my ($self, $ckey, $match) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ## should be called on add/del operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | unless (defined $ckey) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ## invalidate all by not passing an arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->{Cache} = +{}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | return unless exists $self->{Cache}->{$ckey} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | and keys %{ $self->{Cache}->{$ckey} } ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"Cache"}{$ckey} and keys %{$$self{"Cache"}{$ckey};}
MC/DC0%0/2
exists $self->{"Cache"}{$ckey} and keys %{$$self{"Cache"}{$ckey};}
exists $self->{"Cache"}{$ckey}
keys %{$$self{"Cache"}{$ckey};}
Truth table33%1/3
A and B
A= exists $self->{"Cache"}{$ckey}B= keys %{$$self{"Cache"}{$ckey};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | return delete $self->{Cache}->{$ckey}->{$match} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | if defined $match; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | delete $self->{Cache}->{$ckey} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | ▶ | sub _shrink { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_shrink: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | my ($self, $ckey) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 8 | ▶ | return unless defined $ckey and ref $self->{Cache}->{$ckey}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ckey and ref $self->{"Cache"}{$ckey}
MC/DC50%1/2
defined $ckey and ref $self->{"Cache"}{$ckey}
defined $ckey
ref $self->{"Cache"}{$ckey}
Truth table66%2/3
A and B
A= defined $ckeyB= ref $self->{"Cache"}{$ckey}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 6 | my $cacheref = $self->{Cache}->{$ckey}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 6 | ▶ | return unless scalar keys %$cacheref > $self->MaxKeys; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | my @cached = sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $cacheref->{$a}->{TS} <=> $cacheref->{$b}->{TS} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 10 | } keys %$cacheref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $deleted = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | while (scalar keys %$cacheref > $self->MaxKeys) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my $nextkey = shift @cached; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | ++$deleted if delete $cacheref->{$nextkey}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | $deleted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||