| 1 | package DBI::Util::CacheMemory; | ||||||||||||
| 2 | |||||||||||||
| 3 | # $Id: CacheMemory.pm 10314 2007-11-26 22:25:33Z Tim $ | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # Copyright (c) 2007, Tim Bunce, Ireland | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # You may distribute under the terms of either the GNU General Public | ||||||||||||
| 8 | # License or the Artistic License, as specified in the Perl README file. | ||||||||||||
| 9 | |||||||||||||
| 10 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | =head1 NAME | ||||||||||||
| 14 | |||||||||||||
| 15 | DBI::Util::CacheMemory - a very fast but very minimal subset of Cache::Memory | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||
| 18 | |||||||||||||
| 19 | Like Cache::Memory (part of the Cache distribution) but doesn't support any fancy features. | ||||||||||||
| 20 | |||||||||||||
| 21 | This module aims to be a very fast compatible strict sub-set for simple cases, | ||||||||||||
| 22 | such as basic client-side caching for DBD::Gofer. | ||||||||||||
| 23 | |||||||||||||
| 24 | Like Cache::Memory, and other caches in the Cache and Cache::Cache | ||||||||||||
| 25 | distributions, the data will remain in the cache until cleared, it expires, | ||||||||||||
| 26 | or the process dies. The cache object simply going out of scope will I<not> | ||||||||||||
| 27 | destroy the data. | ||||||||||||
| 28 | |||||||||||||
| 29 | =head1 METHODS WITH CHANGES | ||||||||||||
| 30 | |||||||||||||
| 31 | =head2 new | ||||||||||||
| 32 | |||||||||||||
| 33 | All options except C<namespace> are ignored. | ||||||||||||
| 34 | |||||||||||||
| 35 | =head2 set | ||||||||||||
| 36 | |||||||||||||
| 37 | Doesn't support expiry. | ||||||||||||
| 38 | |||||||||||||
| 39 | =head2 purge | ||||||||||||
| 40 | |||||||||||||
| 41 | Same as clear() - deletes everything in the namespace. | ||||||||||||
| 42 | |||||||||||||
| 43 | =head1 METHODS WITHOUT CHANGES | ||||||||||||
| 44 | |||||||||||||
| 45 | =over | ||||||||||||
| 46 | |||||||||||||
| 47 | =item clear | ||||||||||||
| 48 | |||||||||||||
| 49 | =item count | ||||||||||||
| 50 | |||||||||||||
| 51 | =item exists | ||||||||||||
| 52 | |||||||||||||
| 53 | =item remove | ||||||||||||
| 54 | |||||||||||||
| 55 | =back | ||||||||||||
| 56 | |||||||||||||
| 57 | =head1 UNSUPPORTED METHODS | ||||||||||||
| 58 | |||||||||||||
| 59 | If it's not listed above, it's not supported. | ||||||||||||
| 60 | |||||||||||||
| 61 | =cut | ||||||||||||
| 62 | |||||||||||||
| 63 | 4 | our $VERSION = "0.010315"; | |||||||||||
| 64 | |||||||||||||
| 65 | 4 | my %cache; | |||||||||||
| 66 | |||||||||||||
| 67 | 8 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 68 | 8 | my ($class, %options ) = @_; | |||||||||||
| 69 | 8 | ▶ | my $namespace = $options{namespace} ||= 'Default'; | ||||||||||
|
Condition100%2/2
$options{"namespace"} ||= "Default"
MC/DC100%1/1
$options{"namespace"} ||= "Default"
$options{"namespace"}
Truth table100%2/2
A
A= $options{"namespace"}
| |||||||||||||
| 70 | #$options{_cache} = \%cache; # can be handy for debugging/dumping | ||||||||||||
| 71 | 8 | my $self = bless \%options => $class; | |||||||||||
| 72 | 8 | ▶ | $cache{ $namespace } ||= {}; # init - ensure it exists | ||||||||||
|
Condition100%2/2
$cache{$namespace} ||= {}
MC/DC100%1/1
$cache{$namespace} ||= {}
$cache{$namespace}
Truth table100%2/2
A
A= $cache{$namespace}
| |||||||||||||
| 73 | 8 | return $self; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 14 | ▶ | sub set { | ||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 77 | 14 | my ($self, $key, $value) = @_; | |||||||||||
| 78 | 14 | $cache{ $self->{namespace} }->{$key} = $value; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 18 | ▶ | sub get { | ||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 82 | 18 | my ($self, $key) = @_; | |||||||||||
| 83 | 18 | return $cache{ $self->{namespace} }->{$key}; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | ▶ | sub exists { | ||||||||||
|
Subroutine
exists: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 87 | 0 | my ($self, $key) = @_; | |||||||||||
| 88 | 0 | return exists $cache{ $self->{namespace} }->{$key}; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | sub remove { | ||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 92 | 0 | my ($self, $key) = @_; | |||||||||||
| 93 | 0 | return delete $cache{ $self->{namespace} }->{$key}; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | sub purge { | ||||||||||
|
Subroutine
purge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 97 | 0 | return shift->clear; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 6 | ▶ | sub clear { | ||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 101 | 6 | $cache{ shift->{namespace} } = {}; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 8 | ▶ | sub count { | ||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 105 | 8 | return scalar keys %{ $cache{ shift->{namespace} } }; | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 12 | ▶ | sub size { | ||||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 109 | 12 | my $c = $cache{ shift->{namespace} }; | |||||||||||
| 110 | 12 | my $size = 0; | |||||||||||
| 111 | 12 | ▶ | while ( my ($k,$v) = each %$c ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 8 | $size += length($k) + length($v); | |||||||||||
| 113 | } | ||||||||||||
| 114 | 12 | return $size; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 4 | 1; | |||||||||||