File Coverage

blib/lib/EntityModel/Cache/Perl.pm
Criterion Covered Total %
statement 13 34 38.2
branch 0 4 0.0
condition n/a
subroutine 4 8 50.0
pod 5 6 83.3
total 22 52 42.3


line stmt bran cond sub pod time code
1             package EntityModel::Cache::Perl;
2             {
3             $EntityModel::Cache::Perl::VERSION = '0.102';
4             }
5             use EntityModel::Class {
6 1         11 _isa => [qw(EntityModel::Cache)],
7 1     1   2307 };
  1         3  
8              
9             =head1 NAME
10              
11             EntityModel::Cache::Perl - simple proof-of-concept Perl-level caching layer
12              
13             =head1 VERSION
14              
15             version 0.102
16              
17             =head1 SYNOPSIS
18              
19             =head1 DESCRIPTION
20              
21             =cut
22              
23 1     1   1754 use Tie::Cache::LRU;
  1         13937  
  1         477  
24              
25             our %cache;
26             tie %cache, 'Tie::Cache::LRU', 1024;
27              
28             =head1 METHODS
29              
30             =cut
31              
32             sub get {
33 5     5 1 8 my $self = shift;
34 5         6 my $k = shift;
35 5         28 return $cache{$k};
36             }
37              
38             sub remove {
39 0     0 1 0 my $self = shift;
40 0         0 my $k = shift;
41 0         0 delete $cache{$k};
42 0         0 return $self;
43             }
44              
45             sub incr {
46 0     0 1 0 my $self = shift;
47 0         0 my $k = shift;
48 0         0 ++$cache{$k};
49             }
50              
51             sub decr {
52 0     0 0 0 my $self = shift;
53 0         0 my $k = shift;
54 0         0 --$cache{$k};
55             }
56              
57             sub set {
58 5     5 1 5815 my $self = shift;
59 5         7 my $k = shift;
60 5         36 $cache{$k} = shift;
61 5         89 return $self;
62             }
63              
64             sub atomic {
65 0     0 1   my $self = shift;
66 0 0         die 'This is an instance method' unless ref($self);
67 0           my $k = shift;
68 0           my $f = shift;
69 0           my $v = $self->get($k);
70              
71 0 0         if($v) {
72 0           logDebug('[%s] is cached, %d bytes', $k, length($v));
73 0           return $v;
74             }
75              
76 0           $v = $f->($k); # old memcached without cas support may die here
77 0           $self->set($k, $v, 5);
78 0           return $v;
79             }
80              
81             1;
82              
83             __END__