line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Autocache::Strategy::CostBased; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use Any::Moose; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
extends 'Autocache::Strategy'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
589
|
use Autocache; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
8
|
1
|
|
|
1
|
|
529
|
use Time::HiRes qw( gettimeofday tv_interval ); |
|
1
|
|
|
|
|
1360
|
|
|
1
|
|
|
|
|
4
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
215
|
use Autocache::Logger qw(get_logger); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
652
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# Cost-Based Strategy - only cache content that takes over a certain amount |
14
|
|
|
|
|
|
|
# of time to generate |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
# cost_threshold : miniumum time that a function result must take to |
19
|
|
|
|
|
|
|
# generate before it is considered for caching. (milliseconds) |
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
has 'cost_threshold' => ( |
22
|
|
|
|
|
|
|
is => 'ro', |
23
|
|
|
|
|
|
|
isa => 'Int', |
24
|
|
|
|
|
|
|
default => 1000, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# base_strategy : underlying strategy that handles storage and expiry - |
29
|
|
|
|
|
|
|
# defaults |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
has 'base_strategy' => ( |
32
|
|
|
|
|
|
|
is => 'ro', |
33
|
|
|
|
|
|
|
isa => 'Autocache::Strategy', |
34
|
|
|
|
|
|
|
lazy_build => 1, |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# |
38
|
|
|
|
|
|
|
# create REQ |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
sub create |
41
|
|
|
|
|
|
|
{ |
42
|
0
|
|
|
0
|
0
|
|
my ($self,$req) = @_; |
43
|
0
|
|
|
|
|
|
get_logger()->debug( "create" ); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $t0 = [gettimeofday]; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $rec = $self->base_strategy->create( $req ); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $elapsed = tv_interval ( $t0 ); |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
$rec->{time_cost} = $elapsed * 1_000; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
get_logger()->debug( "record time_cost : " . $rec->time_cost ); |
54
|
0
|
|
|
|
|
|
get_logger()->debug( "cost threshold : " . $self->cost_threshold ); |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
return $rec; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# get REQ |
61
|
|
|
|
|
|
|
# |
62
|
|
|
|
|
|
|
sub get |
63
|
|
|
|
|
|
|
{ |
64
|
0
|
|
|
0
|
1
|
|
my ($self,$req) = @_; |
65
|
0
|
|
|
|
|
|
get_logger()->debug( "get" ); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my $rec = $self->base_strategy->get( $req ); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
return $rec; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# set REQ REC |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
sub set |
76
|
|
|
|
|
|
|
{ |
77
|
0
|
|
|
0
|
1
|
|
my ($self,$req,$rec) = @_; |
78
|
0
|
|
|
|
|
|
get_logger()->debug( "set " . $rec->name ); |
79
|
|
|
|
|
|
|
# only put in cache if it has exceeded our cost threshold |
80
|
0
|
0
|
|
|
|
|
if( $rec->time_cost > $self->cost_threshold ) |
81
|
|
|
|
|
|
|
{ |
82
|
0
|
|
|
|
|
|
get_logger()->debug( "cost threshold exceeded setting in cache" ); |
83
|
0
|
|
|
|
|
|
return $self->base_strategy->set( $req, $rec ); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# |
88
|
|
|
|
|
|
|
# delete KEY |
89
|
|
|
|
|
|
|
# |
90
|
|
|
|
|
|
|
sub delete |
91
|
|
|
|
|
|
|
{ |
92
|
0
|
|
|
0
|
1
|
|
my ($self,$key) = @_; |
93
|
0
|
|
|
|
|
|
return $self->base_strategy->delete( $key ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub clear |
97
|
|
|
|
|
|
|
{ |
98
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
99
|
0
|
|
|
|
|
|
return $self->base_strategy->clear; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _build_base_strategy |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
0
|
|
|
return Autocache->singleton->get_default_strategy(); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
around BUILDARGS => sub |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
my $orig = shift; |
110
|
|
|
|
|
|
|
my $class = shift; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
get_logger()->debug( __PACKAGE__ . " - BUILDARGS" ); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
if( ref $_[0] ) |
115
|
|
|
|
|
|
|
{ |
116
|
|
|
|
|
|
|
my $config = $_[0]; |
117
|
|
|
|
|
|
|
my %args; |
118
|
|
|
|
|
|
|
my $node; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
if( $node = $config->get_node( 'base_strategy' ) ) |
121
|
|
|
|
|
|
|
{ |
122
|
|
|
|
|
|
|
get_logger()->debug( "base strategy node found" ); |
123
|
|
|
|
|
|
|
$args{base_strategy} = Autocache->singleton->get_strategy( $node->value ); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
if( $node = $config->get_node( 'cost_threshold' ) ) |
127
|
|
|
|
|
|
|
{ |
128
|
|
|
|
|
|
|
get_logger()->debug( "cost threshold node found" ); |
129
|
|
|
|
|
|
|
my $millis = $node->value; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
unless( $millis =~ /^\d+$/ ) |
132
|
|
|
|
|
|
|
{ |
133
|
|
|
|
|
|
|
if( $millis =~ /(\d+)ms/ ) |
134
|
|
|
|
|
|
|
{ |
135
|
|
|
|
|
|
|
$millis = $1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
elsif( $millis =~ /(\d+)s/ ) |
138
|
|
|
|
|
|
|
{ |
139
|
|
|
|
|
|
|
$millis = $1 * 1000; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
elsif( $millis =~ /(\d+)m/ ) |
142
|
|
|
|
|
|
|
{ |
143
|
|
|
|
|
|
|
$millis = $1 * 1000 * 60; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$args{cost_threshold} = $millis; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
get_logger()->debug( sprintf 'cost threshold : %dms', $millis ); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
return $class->$orig( %args ); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
else |
155
|
|
|
|
|
|
|
{ |
156
|
|
|
|
|
|
|
return $class->$orig(@_); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
1
|
|
|
1
|
|
6
|
no Any::Moose; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
162
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |