line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::Cache; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
182073
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
113
|
|
4
|
3
|
|
|
3
|
|
16
|
use vars qw( $VERSION ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
167
|
|
5
|
3
|
|
|
3
|
|
16
|
use base qw( Template::Plugin ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1747
|
|
6
|
3
|
|
|
3
|
|
6432
|
use Template::Plugin; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
910
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.14'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
11
|
|
|
|
|
|
|
# new(\%options) |
12
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
4
|
|
|
4
|
1
|
15569
|
my ( $class, $context, $params ) = @_; |
16
|
4
|
|
|
|
|
8
|
my $cache; |
17
|
4
|
50
|
|
|
|
29
|
if ( $params->{cache} ) { |
18
|
0
|
|
|
|
|
0
|
$cache = delete $params->{cache}; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
else { |
21
|
4
|
|
|
|
|
34
|
require Cache::FileCache; |
22
|
4
|
|
|
|
|
55
|
$cache = Cache::FileCache->new($params); |
23
|
|
|
|
|
|
|
} |
24
|
4
|
|
|
|
|
1312
|
my $self = bless { |
25
|
|
|
|
|
|
|
CACHE => $cache, |
26
|
|
|
|
|
|
|
CONFIG => $params, |
27
|
|
|
|
|
|
|
CONTEXT => $context, |
28
|
|
|
|
|
|
|
}, $class; |
29
|
4
|
|
|
|
|
17
|
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
|
37
|
my ( $self, $params ) = @_; |
42
|
1
|
|
|
|
|
5
|
$self->_cached_action( 'include', $params ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub proc { |
46
|
5
|
|
|
5
|
0
|
431
|
my ( $self, $params ) = @_; |
47
|
5
|
|
|
|
|
15
|
$self->_cached_action( 'process', $params ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _cached_action { |
51
|
6
|
|
|
6
|
|
17
|
my ( $self, $action, $params ) = @_; |
52
|
6
|
|
|
|
|
28
|
my $key; |
53
|
6
|
50
|
|
|
|
20
|
if ( $params->{key} ) { |
54
|
0
|
|
|
|
|
0
|
$key = delete $params->{key}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else { |
57
|
6
|
|
|
|
|
11
|
my $cache_keys = $params->{keys}; |
58
|
|
|
|
|
|
|
$key = join( |
59
|
|
|
|
|
|
|
':', |
60
|
|
|
|
|
|
|
( |
61
|
|
|
|
|
|
|
$params->{template}, |
62
|
6
|
|
|
|
|
13
|
map { "$_=$cache_keys->{$_}" } sort keys %{$cache_keys} |
|
14
|
|
|
|
|
38
|
|
|
6
|
|
|
|
|
34
|
|
63
|
|
|
|
|
|
|
) |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
} |
66
|
6
|
|
|
|
|
44
|
my $result = $self->{CACHE}->get($key); |
67
|
6
|
100
|
|
|
|
2124
|
if ( !$result ) { |
68
|
4
|
|
|
|
|
45
|
$result = $self->{CONTEXT}->$action( $params->{template} ); |
69
|
4
|
|
|
|
|
809
|
$self->{CACHE}->set( $key, $result, $params->{ttl} ); |
70
|
|
|
|
|
|
|
} |
71
|
6
|
|
|
|
|
6611
|
return $result; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
__END__ |