blib/lib/Cache/Adaptive/ByLoad.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 71 CC 7 SCAR 25.2
CC
7
Cov
0%
BEGIN
30.0
new
6.0
_check_load
6.0
CRAP 12.4
1 package Cache::Adaptive::ByLoad;
2  
3 use strict;
4 use warnings;
5  
6 use base qw(Cache::Adaptive);
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 my $load_avg;
30 eval {
31 require Sys::Statistics::Linux::LoadAVG;
32 my $l = Sys::Statistics::Linux::LoadAVG->new;
33 $load_avg = sub {
34 $l->get->{avg_1};
35 };
36 } unless $load_avg;
37 eval {
38 require BSD::Sysctl;
39 $load_avg = sub {
40 my $la = BSD::Sysctl::sysctl('vm.loadavg');
41 $la->[0];
42 };
43 } unless $load_avg;
44 eval {
45 require BSD::getloadavg;
46 $load_avg = sub {
47 my @la = BSD::getloadavg::getloadavg();
48 $la[0];
49 };
50 } unless $load_avg;
51
52 die "Cache::Adaptive::ByLoad requires either of the following: Sys::Statistics::Linux::LoadAVG, BSD::Sysctl, BSD::Getloadavg.\n" unless $load_avg;
53 *_load_avg = $load_avg;
54 };
55  
56 sub new {
57 my ($class, $opts) = @_;
58 my $self = Cache::Adaptive::new($class, {
59 %DEFAULTS,
60 $opts ? %$opts : (),
61 });
62 $self->check_load(sub { $self->_check_load(@_); });
63 $self;
64 }
65  
66 sub _check_load {
67 my ($self, $entry, $params) = @_;
68 my $l = _load_avg() * $self->target_loadavg;
69 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<Cache::Adaptive::ByLoad> is a subclass of L<Cache::Adaptive>. 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<Cache::Adaptive::ByLoad> defines two properties in addition to the properties defined by L<Cache::Adaptive>.
113  
114 =head2 load_factor
115  
116 =head2 target_loadavg
117  
118 =head1 SEE ALSO
119  
120 L<Cache::Adaptive>
121  
122 =head1 AUTHOR
123  
124 Copyright (c) 2007 Cybozu Labs, Inc. All rights reserved.
125  
126 written by Kazuho Oku E<lt>kazuhooku@gmail.comE<gt>
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