File Coverage

blib/lib/Cache/Adaptive/ByLoad.pm
Criterion Covered Total %
statement 23 35 65.7
branch 5 12 41.6
condition n/a
subroutine 6 8 75.0
pod 1 1 100.0
total 35 56 62.5


line stmt bran cond sub pod time code
1             package Cache::Adaptive::ByLoad;
2              
3 2     2   137 use strict;
  2         4  
  2         64  
4 2     2   30 use warnings;
  2         5  
  2         67  
5              
6 2     2   10 use base qw(Cache::Adaptive);
  2         3  
  2         2228  
7              
8             our $VERSION = '0.01';
9              
10             my %MY_DEFAULTS = (
11             load_factor => 8,
12             target_loadavg => 1,
13             );
14              
15             my %DEFAULTS = (
16             %MY_DEFAULTS,
17             expires_initial => 1,
18             expires_min => 0.3,
19             increase_factor => 1.25,
20             decrease_factor => 0.8,
21             expires_max => 60,
22             purge_after => 80,
23             check_interval => 10,
24             );
25              
26             __PACKAGE__->mk_accessors($_) for keys %MY_DEFAULTS;
27            
28             BEGIN {
29 2     2   4 my $load_avg;
30 2 50       9 eval {
31 2         7498 require Sys::Statistics::Linux::LoadAVG;
32 2         2034 my $l = Sys::Statistics::Linux::LoadAVG->new;
33             $load_avg = sub {
34 1     1   12 $l->get->{avg_1};
35 2         63 };
36             } unless $load_avg;
37 2 50       15 eval {
38 0         0 require BSD::Sysctl;
39             $load_avg = sub {
40 0         0 my $la = BSD::Sysctl::sysctl('vm.loadavg');
41 0         0 $la->[0];
42 0         0 };
43             } unless $load_avg;
44 2 50       9 eval {
45 0         0 require BSD::getloadavg;
46             $load_avg = sub {
47 0         0 my @la = BSD::getloadavg::getloadavg();
48 0         0 $la[0];
49 0         0 };
50             } unless $load_avg;
51            
52 2 50       10 die "Cache::Adaptive::ByLoad requires either of the following: Sys::Statistics::Linux::LoadAVG, BSD::Sysctl, BSD::Getloadavg.\n" unless $load_avg;
53 2         570 *_load_avg = $load_avg;
54             };
55              
56             sub new {
57 1     1 1 2 my ($class, $opts) = @_;
58 1 50       14 my $self = Cache::Adaptive::new($class, {
59             %DEFAULTS,
60             $opts ? %$opts : (),
61             });
62 1     0   10 $self->check_load(sub { $self->_check_load(@_); });
  0         0  
63 1         7 $self;
64             }
65              
66             sub _check_load {
67 0     0     my ($self, $entry, $params) = @_;
68 0           my $l = _load_avg() * $self->target_loadavg;
69 0 0         int($params->{load} * $self->load_factor * $l <= 1 ? $l : $l ** 2) - 1;
70             }
71              
72             1;
73              
74             =head1 NAME
75              
76             Cache::Adaptive::ByLoad - Automatically adjusts the cache lifetime by load
77              
78             =head1 SYNOPSIS
79              
80             use Cache::Adaptive::ByLoad;
81             use Cache::FileCache;
82              
83             my $cache = Cache::Adaptive::ByLoad->new({
84             backend => Cache::FileCache->new({
85             namespace => 'cache_adaptive',
86             }),
87             });
88            
89             ...
90            
91             print "Content-Type: text/html\n\n";
92             print $cache->access({
93             key => $uri,
94             builder => sub {
95             # your HTML build logic here
96             $html;
97             },
98             });
99              
100             =head1 DESCRIPTION
101              
102             C is a subclass of L. The module adjusts cache lifetime by two factors; the load average of the platform and the percentage of the total time spent by the builder. In other words, the module tries to utilize the cache for bottlenecks under heavy load.
103              
104             =head1 METHODS
105              
106             =head2 new
107              
108             Constructor. Takes a hashref of properties.
109              
110             =head1 PROPERTIES
111              
112             C defines two properties in addition to the properties defined by L.
113              
114             =head2 load_factor
115              
116             =head2 target_loadavg
117              
118             =head1 SEE ALSO
119              
120             L
121              
122             =head1 AUTHOR
123              
124             Copyright (c) 2007 Cybozu Labs, Inc. All rights reserved.
125              
126             written by Kazuho Oku Ekazuhooku@gmail.comE
127              
128             =head1 LICENSE
129              
130             This program is free software; you can redistribute it and/or modify it under t
131             he same terms as Perl itself.
132              
133             See http://www.perl.com/perl/misc/Artistic.html
134              
135             =cut