File Coverage

blib/lib/AnyEvent/InMemoryCache.pm
Criterion Covered Total %
statement 44 46 95.6
branch 6 6 100.0
condition 3 4 75.0
subroutine 14 15 93.3
pod 5 5 100.0
total 72 76 94.7


line stmt bran cond sub pod time code
1             package AnyEvent::InMemoryCache;
2 4     4   103241 use 5.008005;
  4         14  
  4         145  
3 4     4   21 use strict;
  4         9  
  4         136  
4 4     4   19 use warnings;
  4         48  
  4         193  
5              
6             our $VERSION = "0.01";
7              
8 4     4   1381 use AnyEvent;
  4         5821  
  4         95  
9 4     4   3691 use Time::Duration::Parse;
  4         10135  
  4         30  
10              
11             sub new {
12 3     3 1 17076 my $class = shift;
13 3         11 my %args = @_;
14 3 100       15 if ( exists $args{expires_in} ) {
15 2         12 $args{expires_in} = parse_duration($args{expires_in});
16             } else {
17 1         3 $args{expires_in} = -1; # endless
18             }
19 3         78 $args{_datastore} = {};
20 3         13 bless \%args, $class;
21             }
22              
23             sub set {
24 11     11 1 31 my ($self, $key, $val, $expires_in) = @_;
25 11 100       34 if ( @_ < 4) {
26 6         16 $expires_in = $self->{expires_in};
27             } else {
28 5         22 $expires_in = parse_duration($expires_in);
29             }
30             $self->{_datastore}{$key} = [
31             $val,
32 7     7   6976844 ($expires_in < 0 ? undef : AE::timer $expires_in, 0, sub{ delete $self->{_datastore}{$key} })
33 11 100       288 ];
34 11         68 $val;
35             }
36              
37             sub get {
38 23     23 1 148 my ($self, $key) = @_;
39 23   50     174 ($self->{_datastore}{$key} || [])->[0];
40             }
41              
42             sub exists {
43 44     44 1 4990967 my ($self, $key) = @_;
44 44         437 exists $self->{_datastore}{$key};
45             }
46              
47             sub delete {
48 3     3 1 8 my ($self, $key) = @_;
49 3   100     30 (delete $self->{_datastore}{$key} || [])->[0];
50             }
51              
52              
53             # Tie-hash subroutines
54              
55             *TIEHASH = \&new;
56             *FETCH = \&get;
57             *STORE = \&set;
58             *DELETE = \&delete;
59             *EXISTS = \&exists;
60              
61             sub CLEAR {
62 0     0   0 %{$_[0]->{_datastore}} = ();
  0         0  
63             };
64              
65             sub FIRSTKEY {
66 13     13   2000365 my $self = shift;
67 13         26 keys %{$self->{_datastore}}; # rest iterator
  13         41  
68 13         23 scalar each %{$self->{_datastore}};
  13         103  
69             }
70              
71             sub NEXTKEY {
72 18     18   25 scalar each %{$_[0]->{_datastore}};
  18         118  
73             }
74              
75             sub SCALAR {
76 9     9   16 scalar %{$_[0]->{_datastore}};
  9         68  
77             }
78              
79              
80             1;
81             __END__