File Coverage

blib/lib/Autocache/Strategy/Statistics.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 2 0.0
condition n/a
subroutine 4 11 36.3
pod 4 5 80.0
total 20 51 39.2


line stmt bran cond sub pod time code
1             package Autocache::Strategy::Statistics;
2              
3 1     1   5 use Any::Moose;
  1         2  
  1         9  
4              
5             extends 'Autocache::Strategy';
6              
7 1     1   762 use Autocache;
  1         2  
  1         68  
8 1     1   6 use Autocache::Logger qw(get_logger);
  1         2  
  1         712  
9              
10             #
11             # base_strategy : underlying strategy that handles storage and expiry -
12             # defaults
13             #
14             has 'base_strategy' => (
15             is => 'ro',
16             isa => 'Autocache::Strategy',
17             lazy_build => 1,
18             );
19              
20             #
21             # hashref containing our stats
22             #
23             has 'statistics' => (
24             is => 'rw',
25             isa => 'HashRef',
26             lazy_build => 1,
27             );
28              
29             #
30             # create REQ
31             #
32             sub create
33             {
34 0     0 0   my ($self,$req) = @_;
35 0           get_logger()->debug( "create" );
36 0           ++$self->statistics->{create};
37 0           return $self->base_strategy->create( $req );
38             }
39              
40             #
41             # get REQ
42             #
43             sub get
44             {
45 0     0 1   my ($self,$req) = @_;
46 0           get_logger()->debug( "get" );
47 0           my $rec = $self->base_strategy->get(
48             $req );
49 0 0         if( $rec )
50             {
51 0           ++$self->statistics->{hit};
52             }
53             else
54             {
55 0           ++$self->statistics->{miss};
56             }
57 0           ++$self->statistics->{total};
58 0           return $rec;
59             }
60              
61             #
62             # REQ REC
63             #
64             sub set
65             {
66 0     0 1   my ($self,$req,$rec) = @_;
67 0           get_logger()->debug( "set " . $rec->name );
68 0           return $self->base_strategy->set( $req, $rec );
69             }
70              
71             #
72             # delete KEY
73             #
74             sub delete
75             {
76 0     0 1   my ($self,$key) = @_;
77 0           return $self->base_strategy->delete( $key );
78             }
79              
80             sub clear
81             {
82 0     0 1   my ($self) = @_;
83 0           return $self->base_strategy->clear;
84             }
85              
86             sub _build_base_strategy
87             {
88 0     0     return Autocache->singleton->get_default_strategy();
89             }
90              
91             sub _build_statistics
92             {
93             return {
94 0     0     hit => 0,
95             miss => 0,
96             create => 0,
97             total => 0,
98             };
99             }
100              
101             around BUILDARGS => sub
102             {
103             my $orig = shift;
104             my $class = shift;
105              
106             get_logger()->debug( __PACKAGE__ . " - BUILDARGS" );
107              
108             if( ref $_[0] )
109             {
110             my $config = $_[0];
111             my %args;
112             my $node;
113              
114             if( $node = $config->get_node( 'base_strategy' ) )
115             {
116             get_logger()->debug( "base strategy node found" );
117             $args{base_strategy} = Autocache->singleton->get_strategy( $node->value );
118             }
119              
120             return $class->$orig( %args );
121             }
122             else
123             {
124             return $class->$orig(@_);
125             }
126             };
127              
128 1     1   7 no Any::Moose;
  1         2  
  1         7  
129             __PACKAGE__->meta->make_immutable;
130              
131             1;