line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Application::Plugin::CHI; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
49609
|
use strict; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
296
|
|
4
|
6
|
|
|
6
|
|
37
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
214
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
35
|
use base 'Exporter'; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
994
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
3575
|
use CHI; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Scalar::Util 'blessed', 'reftype'; |
10
|
|
|
|
|
|
|
use Carp 'croak'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.03; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# export by default since we're doing a mixin-style class |
15
|
|
|
|
|
|
|
our @EXPORT = ( 'cache_config', 'cache_default', 'cache', 'rmcache', '__get_cache' ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %CONFIG; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub cache_config { |
21
|
|
|
|
|
|
|
my $class = shift; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
if ( @_ == 1 ) { |
24
|
|
|
|
|
|
|
croak 'first argument to cache_config() must be a hashref or cache name' |
25
|
|
|
|
|
|
|
unless reftype $_[0] eq 'HASH'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
return $CONFIG{default} = $_[0]; |
28
|
|
|
|
|
|
|
} elsif ( @_ >= 2 ) { |
29
|
|
|
|
|
|
|
my %args = @_; |
30
|
|
|
|
|
|
|
foreach my $key( keys %args ) { |
31
|
|
|
|
|
|
|
croak 'multiple-argument form of cache_config() requires name => hashref pairs' |
32
|
|
|
|
|
|
|
unless ref( $args{$key} ) && reftype $args{$key} eq 'HASH'; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$CONFIG{named}{$key} = $args{$key}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return 1; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
croak 'no arguments to cache_config()'; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub cache_default { |
44
|
|
|
|
|
|
|
my $class = shift; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
croak 'cache_default requires one argument' |
47
|
|
|
|
|
|
|
unless @_ == 1; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
if ( my $def_conf = $CONFIG{named}{$_[0]} ) { |
50
|
|
|
|
|
|
|
return $CONFIG{default} = $def_conf; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
croak "no such cache named '$_[0]'"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub cache { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
croak 'cache must be called as an object method' |
60
|
|
|
|
|
|
|
unless ref $self; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
croak 'too many arguments to cache()' |
63
|
|
|
|
|
|
|
if @_ > 1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
return $self->__get_cache( [ @_ ] ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub rmcache { |
69
|
|
|
|
|
|
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
croak 'rmcache must be called as an object method' |
72
|
|
|
|
|
|
|
unless ref $self; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $ns = blessed( $self ) . '::' . $self->get_current_runmode; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
return $self->__get_cache( [ @_ ], $ns ); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub __get_cache { |
81
|
|
|
|
|
|
|
my $self = shift; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my @args = @{ $_[0] }; |
84
|
|
|
|
|
|
|
my $ns = $_[1] || ''; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
if ( @args == 0 ) { |
87
|
|
|
|
|
|
|
if ( my $conf = $CONFIG{default} ) { |
88
|
|
|
|
|
|
|
return CHI->new( %$conf, |
89
|
|
|
|
|
|
|
$ns |
90
|
|
|
|
|
|
|
? ( namespace => $ns ) |
91
|
|
|
|
|
|
|
: ( ) |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} else { |
95
|
|
|
|
|
|
|
croak "no default cache configured"; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} elsif ( @args == 1 ) { |
98
|
|
|
|
|
|
|
if ( my $conf = $CONFIG{named}{$args[0]} ) { |
99
|
|
|
|
|
|
|
return CHI->new( %$conf, |
100
|
|
|
|
|
|
|
$ns |
101
|
|
|
|
|
|
|
? ( namespace => $ns ) |
102
|
|
|
|
|
|
|
: ( ) |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
croak "no such cache '$args[0]' configured"; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
die 'too many args for _get_cache'; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# really only used for testing |
114
|
|
|
|
|
|
|
sub _clean_conf { |
115
|
|
|
|
|
|
|
%CONFIG = ( ); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |