line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Ex::Recipes::Cache; |
2
|
2
|
|
|
2
|
|
17710
|
use utf8; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
14
|
|
3
|
2
|
|
|
2
|
|
64
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
75
|
|
5
|
2
|
|
|
2
|
|
12
|
use Carp qw(croak); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
130
|
|
6
|
2
|
|
|
2
|
|
815
|
use CGI::Ex::Dump qw(debug dex_warn ctrace dex_trace); |
|
2
|
|
|
|
|
1833
|
|
|
2
|
|
|
|
|
172
|
|
7
|
2
|
|
|
2
|
|
6525
|
use Storable; |
|
2
|
|
|
|
|
11019
|
|
|
2
|
|
|
|
|
1468
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
50
|
1
|
0
|
1564
|
my $class = shift || __PACKAGE__; |
12
|
1
|
|
50
|
|
|
9
|
my $args = shift || {}; |
13
|
1
|
|
33
|
|
|
52
|
$args->{expires} ||= time + 3600;#one hour by default |
14
|
1
|
|
50
|
|
|
9
|
$args->{cache_hash} ||= {}; |
15
|
1
|
50
|
|
|
|
6
|
$args->{dbh} || croak 'Please provide a database handle with a `cache` table in the database!'; |
16
|
1
|
|
|
|
|
106
|
return bless {%$args}, $class; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#tries first the 'our %CACHE_HASH', then the database. |
20
|
|
|
|
|
|
|
sub exists { |
21
|
0
|
|
|
0
|
1
|
0
|
my ($self,$key) = @_; |
22
|
0
|
0
|
|
|
|
0
|
1 if(exists $self->{cache_hash}{$key}); |
23
|
0
|
|
|
|
|
0
|
0; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub get { |
27
|
3
|
|
|
3
|
1
|
119320
|
my ($self,$key) = @_; |
28
|
|
|
|
|
|
|
#dex_trace(); #debug $self; |
29
|
3
|
50
|
|
|
|
21
|
if(exists $self->{cache_hash}{$key}) { |
30
|
0
|
|
|
|
|
0
|
return $self->{cache_hash}{$key}{value}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
#warn 'getting $key'.$key.' from database'; |
33
|
3
|
|
100
|
|
|
63
|
my $row = $self->{dbh}->selectrow_hashref('SELECT * FROM cache WHERE id=?',{},$key) |
34
|
|
|
|
|
|
|
|| return undef; |
35
|
2
|
|
|
|
|
1168
|
$self->{cache_hash}{$key} = $row; |
36
|
2
|
50
|
|
|
|
14
|
if($self->{cache_hash}{$key}{expires} < time ){ |
37
|
|
|
|
|
|
|
#warn 'could not $key'.$key.' from database. data expired.'; |
38
|
0
|
|
|
|
|
0
|
return undef; |
39
|
|
|
|
|
|
|
} |
40
|
2
|
|
|
|
|
20
|
return $self->{cache_hash}{$key}{value}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub set { |
44
|
2
|
50
|
|
2
|
1
|
114
|
if (!$_[2]) { |
45
|
0
|
|
|
|
|
0
|
croak 'Please provide a value to be set!'; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
#NOTE: compatible only with SQLITE and MySQL |
48
|
2
|
100
|
|
|
|
30
|
$_[0]->{dbh}->prepare( |
49
|
|
|
|
|
|
|
'REPLACE INTO `cache` (id, value, tstamp, expires) VALUES ( ?,?,?,? )' |
50
|
|
|
|
|
|
|
)->execute( $_[1], $_[2], time, ($_[3]?time+$_[3]:$_[0]->{expires}) ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub clear { |
54
|
1
|
|
|
1
|
1
|
4
|
$_[0]->{cache_hash} = {}; |
55
|
1
|
50
|
|
|
|
14
|
$_[0]->{dbh}->do('DELETE FROM `cache`') |
56
|
|
|
|
|
|
|
and $_[0]->{dbh}->do('VACUUM'); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub freeze { |
61
|
1
|
50
|
33
|
1
|
1
|
12
|
$_[0]->set( |
62
|
|
|
|
|
|
|
$_[1], |
63
|
|
|
|
|
|
|
ref $_[2] ? Storable::nfreeze($_[2]) : $_[2], |
64
|
|
|
|
|
|
|
$_[3] || $_[0]->{expires}, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub thaw { |
69
|
1
|
|
|
1
|
1
|
8
|
Storable::thaw( $_[0]->get($_[1]) ) ; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |