line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::Cache; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
80875
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
4
|
1
|
|
|
1
|
|
9
|
use vars qw( $VERSION ); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
50
|
|
5
|
1
|
|
|
1
|
|
19
|
use base qw( Template::Plugin ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
921
|
|
6
|
1
|
|
|
1
|
|
808
|
use Template::Plugin; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
414
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.13'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
11
|
|
|
|
|
|
|
# new(\%options) |
12
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
3
|
|
|
3
|
1
|
22285
|
my ( $class, $context, $params ) = @_; |
16
|
3
|
|
|
|
|
7
|
my $cache; |
17
|
3
|
50
|
|
|
|
12
|
if ( $params->{cache} ) { |
18
|
0
|
|
|
|
|
0
|
$cache = delete $params->{cache}; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
else { |
21
|
3
|
|
|
|
|
27
|
require Cache::FileCache; |
22
|
3
|
|
|
|
|
43
|
$cache = Cache::FileCache->new($params); |
23
|
|
|
|
|
|
|
} |
24
|
3
|
|
|
|
|
1151
|
my $self = bless { |
25
|
|
|
|
|
|
|
CACHE => $cache, |
26
|
|
|
|
|
|
|
CONFIG => $params, |
27
|
|
|
|
|
|
|
CONTEXT => $context, |
28
|
|
|
|
|
|
|
}, $class; |
29
|
3
|
|
|
|
|
12
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
33
|
|
|
|
|
|
|
# $cache->include({ |
34
|
|
|
|
|
|
|
# template => 'foo.html', |
35
|
|
|
|
|
|
|
# keys => {'user.name', user.name}, |
36
|
|
|
|
|
|
|
# ttl => 60, #seconds |
37
|
|
|
|
|
|
|
# }); |
38
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub inc { |
41
|
1
|
|
|
1
|
0
|
43
|
my ( $self, $params ) = @_; |
42
|
1
|
|
|
|
|
5
|
$self->_cached_action( 'include', $params ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub proc { |
46
|
3
|
|
|
3
|
0
|
141
|
my ( $self, $params ) = @_; |
47
|
3
|
|
|
|
|
78
|
$self->_cached_action( 'process', $params ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _cached_action { |
51
|
4
|
|
|
4
|
|
9
|
my ( $self, $action, $params ) = @_; |
52
|
4
|
|
|
|
|
6
|
my $key; |
53
|
4
|
50
|
|
|
|
13
|
if ( $params->{key} ) { |
54
|
0
|
|
|
|
|
0
|
$key = delete $params->{key}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else { |
57
|
4
|
|
|
|
|
9
|
my $cache_keys = $params->{keys}; |
58
|
2
|
|
|
|
|
11
|
$key = join( |
59
|
|
|
|
|
|
|
':', |
60
|
|
|
|
|
|
|
( |
61
|
|
|
|
|
|
|
$params->{template}, |
62
|
4
|
|
|
|
|
8
|
map { "$_=$cache_keys->{$_}" } keys %{$cache_keys} |
|
4
|
|
|
|
|
12
|
|
63
|
|
|
|
|
|
|
) |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
} |
66
|
4
|
|
|
|
|
33
|
my $result = $self->{CACHE}->get($key); |
67
|
4
|
100
|
|
|
|
1816
|
if ( !$result ) { |
68
|
3
|
|
|
|
|
25
|
$result = $self->{CONTEXT}->$action( $params->{template} ); |
69
|
3
|
|
|
|
|
887
|
$self->{CACHE}->set( $key, $result, $params->{ttl} ); |
70
|
|
|
|
|
|
|
} |
71
|
4
|
|
|
|
|
6693
|
return $result; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
__END__ |