line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gantry::Plugins::PageCache; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
635
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use File::Spec; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my %registered_callbacks; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# lets export a do method and some conf accessors |
11
|
1
|
|
|
1
|
|
3
|
use base 'Exporter'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1288
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw( |
13
|
|
|
|
|
|
|
gantry_pagecache_location |
14
|
|
|
|
|
|
|
gantry_pagecache_crud |
15
|
|
|
|
|
|
|
gantry_pagecache_action |
16
|
|
|
|
|
|
|
gantry_pagecache_mime_type |
17
|
|
|
|
|
|
|
gantry_pagecache_uri |
18
|
|
|
|
|
|
|
gantry_pagecache |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#----------------------------------------------------------- |
23
|
|
|
|
|
|
|
# $class->get_callbacks( $namespace ) |
24
|
|
|
|
|
|
|
#----------------------------------------------------------- |
25
|
|
|
|
|
|
|
sub get_callbacks { |
26
|
0
|
|
|
0
|
1
|
|
my ( $class, $namespace ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
return if ( $registered_callbacks{ $namespace }++ ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
return ( |
31
|
0
|
|
|
|
|
|
{ phase => 'post_init', callback => \&gantry_pagecache_retrieve }, |
32
|
|
|
|
|
|
|
{ phase => 'post_process', callback => \&gantry_pagecache_store }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#----------------------------------------------------------- |
37
|
|
|
|
|
|
|
# gantry_pagecache_retrieve |
38
|
|
|
|
|
|
|
#----------------------------------------------------------- |
39
|
|
|
|
|
|
|
sub gantry_pagecache_retrieve { |
40
|
0
|
|
|
0
|
1
|
|
my ( $gobj ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# set config |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# 0/1 |
45
|
0
|
|
0
|
|
|
|
$gobj->gantry_pagecache_crud( |
46
|
|
|
|
|
|
|
$gobj->fish_config( 'gantry_pagecache_crud' ) || 0 |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# do_add, do_delete, ... |
50
|
0
|
|
0
|
|
|
|
$gobj->gantry_pagecache_action( |
51
|
|
|
|
|
|
|
$gobj->fish_config( 'gantry_pagecache_action' ) || '' |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# text/html, text/rss, ... |
55
|
0
|
|
0
|
|
|
|
$gobj->gantry_pagecache_mime_type( |
56
|
|
|
|
|
|
|
$gobj->fish_config( 'gantry_pagecache_mime_type' ) || '' |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# /controller1, ... |
60
|
0
|
|
0
|
|
|
|
$gobj->gantry_pagecache_location( |
61
|
|
|
|
|
|
|
$gobj->fish_config( 'gantry_pagecache_location' ) || '' |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# /controller1/view, ..., |
65
|
0
|
|
0
|
|
|
|
$gobj->gantry_pagecache_uri( |
66
|
|
|
|
|
|
|
$gobj->fish_config( 'gantry_pagecache_uri' ) || '' |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $cache_key; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# special CRUD caching |
72
|
0
|
0
|
|
|
|
|
if ( $gobj->gantry_pagecache_crud() ) { |
|
|
0
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
0
|
|
|
|
if ( $gobj->is_post() |
75
|
|
|
|
|
|
|
&& $gobj->action() =~ /do_edit|do_add|do_delete/ ) { |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$cache_key = _make_gantry_cache_key( $gobj ); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# flag for cache store |
80
|
0
|
|
|
|
|
|
$gobj->gantry_pagecache( $cache_key ); |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
$gobj->cache_del( $cache_key . "content_type" ); |
83
|
0
|
|
|
|
|
|
$gobj->cache_del( $cache_key ); |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my @parts = split( '/', $cache_key ); |
86
|
0
|
|
|
|
|
|
pop( @parts ); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$gobj->cache_del( join( '/', @parts ) . "content_type" ); |
89
|
0
|
|
|
|
|
|
$gobj->cache_del( join( '/', @parts ) ); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# all other caching |
95
|
|
|
|
|
|
|
elsif ( _caching_enabled( $gobj ) ) { |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
$cache_key = _make_gantry_cache_key( $gobj, { serialize => 1 } ); |
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
if ( my $page = $gobj->cache_get( $cache_key ) ) { |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# set the content-type |
102
|
0
|
|
|
|
|
|
$gobj->content_type( |
103
|
|
|
|
|
|
|
$gobj->cache_get( $cache_key . "content_type" ) |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# set cached page |
107
|
0
|
|
|
|
|
|
$gobj->gantry_response_page( $page ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
# flag it for the cache store method |
110
|
|
|
|
|
|
|
else { |
111
|
0
|
|
|
|
|
|
$gobj->gantry_pagecache( $cache_key ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
#----------------------------------------------------------- |
118
|
|
|
|
|
|
|
# gantry_pagecache_put |
119
|
|
|
|
|
|
|
#----------------------------------------------------------- |
120
|
|
|
|
|
|
|
sub _caching_enabled { |
121
|
0
|
|
|
0
|
|
|
my( $gobj ) = @_; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $cache = 0; |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if ( my @actions = split( /\s*,\s*/, $gobj->gantry_pagecache_action() ) ) { |
126
|
0
|
|
|
|
|
|
my $current_action = $gobj->action(); |
127
|
0
|
|
|
|
|
|
foreach ( @actions ) { |
128
|
0
|
0
|
|
|
|
|
if ( $_ eq $current_action ) { |
129
|
0
|
|
|
|
|
|
return( 1 ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if ( my @types = split( /\s*,\s*/, $gobj->gantry_pagecache_mime_type() ) ) { |
135
|
0
|
|
|
|
|
|
my $current_type = $gobj->content_type(); |
136
|
0
|
|
|
|
|
|
foreach ( @types ) { |
137
|
0
|
0
|
|
|
|
|
if ( $_ eq $current_type ) { |
138
|
0
|
|
|
|
|
|
return( 1 ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
0
|
0
|
|
|
|
|
if ( my @locs = split( /\s*,\s*/, $gobj->gantry_pagecache_location() ) ) { |
144
|
0
|
|
|
|
|
|
my $current_loc = $gobj->location(); |
145
|
0
|
|
|
|
|
|
foreach ( @locs ) { |
146
|
0
|
0
|
|
|
|
|
if ( $_ eq $current_loc ) { |
147
|
0
|
|
|
|
|
|
return( 1 ); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
0
|
0
|
|
|
|
|
if ( my @uris = split( /\s*,\s*/, $gobj->gantry_pagecache_uri() ) ) { |
153
|
0
|
|
|
|
|
|
my $current_uri = $gobj->uri(); |
154
|
0
|
|
|
|
|
|
foreach ( @uris ) { |
155
|
0
|
0
|
|
|
|
|
if ( $_ eq $current_uri ) { |
156
|
0
|
|
|
|
|
|
return( 1 ); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
return( 0 ); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
#----------------------------------------------------------- |
165
|
|
|
|
|
|
|
# gantry_pagecache_put |
166
|
|
|
|
|
|
|
#----------------------------------------------------------- |
167
|
|
|
|
|
|
|
sub gantry_pagecache_store { |
168
|
0
|
|
|
0
|
1
|
|
my ( $gobj ) = @_; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# special CRUD caching |
171
|
0
|
0
|
|
|
|
|
if ( $gobj->gantry_pagecache_crud() ) { |
|
|
0
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $cache_key = $gobj->gantry_pagecache(); |
173
|
|
|
|
|
|
|
|
174
|
0
|
0
|
|
|
|
|
if ( $gobj->action() =~ /do_edit|do_add|do_delete/ ) { |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# set page |
177
|
0
|
|
|
|
|
|
$gobj->cache_set( $cache_key, $gobj->gantry_response_page() ); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# set content-type |
180
|
0
|
|
|
|
|
|
$gobj->cache_set( |
181
|
|
|
|
|
|
|
( $cache_key . 'content_type' ), |
182
|
|
|
|
|
|
|
$gobj->content_type() |
183
|
|
|
|
|
|
|
); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# all other caching |
188
|
|
|
|
|
|
|
elsif( _caching_enabled( $gobj ) ) { |
189
|
0
|
|
|
|
|
|
my $cache_key = $gobj->gantry_pagecache(); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# set page |
192
|
0
|
|
|
|
|
|
$gobj->cache_set( $cache_key, $gobj->gantry_response_page() ); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# set content-type |
195
|
0
|
|
|
|
|
|
$gobj->cache_set( |
196
|
|
|
|
|
|
|
( $cache_key . 'content_type' ), |
197
|
|
|
|
|
|
|
$gobj->content_type() |
198
|
|
|
|
|
|
|
); |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
#----------------------------------------------------------- |
205
|
|
|
|
|
|
|
# _make_gantry_cache_key |
206
|
|
|
|
|
|
|
#----------------------------------------------------------- |
207
|
|
|
|
|
|
|
sub _make_gantry_cache_key { |
208
|
0
|
|
|
0
|
|
|
my( $gobj, $opt ) = @_; |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
my @parts; |
211
|
0
|
|
|
|
|
|
push( @parts, |
212
|
|
|
|
|
|
|
$gobj->namespace, |
213
|
|
|
|
|
|
|
$gobj->uri, |
214
|
|
|
|
|
|
|
); |
215
|
|
|
|
|
|
|
|
216
|
0
|
0
|
|
|
|
|
if ( $opt->{serialize} ) { |
217
|
0
|
|
|
|
|
|
my $serial = _serialize_params( $gobj ); |
218
|
|
|
|
|
|
|
|
219
|
0
|
0
|
|
|
|
|
push( @parts, |
220
|
|
|
|
|
|
|
( $serial ? ( '?' . $serial ) : '' ), |
221
|
|
|
|
|
|
|
); |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
return join( '', @parts ); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
#----------------------------------------------------------- |
228
|
|
|
|
|
|
|
# _serialize_params |
229
|
|
|
|
|
|
|
#----------------------------------------------------------- |
230
|
|
|
|
|
|
|
sub _serialize_params { |
231
|
0
|
|
|
0
|
|
|
my( $gobj ) = @_; |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
my %param = $gobj->get_param_hash; |
234
|
0
|
|
|
|
|
|
my @param_serial; |
235
|
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
foreach my $k ( sort { $a cmp $b } keys %param ) { |
|
0
|
|
|
|
|
|
|
237
|
0
|
0
|
|
|
|
|
next if substr( $k, 0, 1 ) eq '.'; |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
push( @param_serial, "$k=$param{$k}" ); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
return join( '&', @param_serial ); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
#------------------------------------------------- |
246
|
|
|
|
|
|
|
# $self->gantry_pagecache_crud |
247
|
|
|
|
|
|
|
#------------------------------------------------- |
248
|
|
|
|
|
|
|
sub gantry_pagecache_crud { |
249
|
0
|
|
|
0
|
1
|
|
my ( $self, $p ) = ( shift, shift ); |
250
|
|
|
|
|
|
|
|
251
|
0
|
0
|
|
|
|
|
$$self{__GANTRY_PAGECACHE_CRUD__} = $p if defined $p; |
252
|
0
|
|
|
|
|
|
return( $$self{__GANTRY_PAGECACHE_CRUD__} ); |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
} # end gantry_pagecache_crud |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
#------------------------------------------------- |
257
|
|
|
|
|
|
|
# $self->gantry_pagecache_location |
258
|
|
|
|
|
|
|
#------------------------------------------------- |
259
|
|
|
|
|
|
|
sub gantry_pagecache_location { |
260
|
0
|
|
|
0
|
1
|
|
my ( $self, $p ) = ( shift, shift ); |
261
|
|
|
|
|
|
|
|
262
|
0
|
0
|
|
|
|
|
$$self{__GANTRY_PAGECACHE_LOCATION__} = $p if defined $p; |
263
|
0
|
|
|
|
|
|
return( $$self{__GANTRY_PAGECACHE_LOCATION__} ); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
} # end gantry_pagecache_location |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
#------------------------------------------------- |
268
|
|
|
|
|
|
|
# $self->gantry_pagecache_action |
269
|
|
|
|
|
|
|
#------------------------------------------------- |
270
|
|
|
|
|
|
|
sub gantry_pagecache_action { |
271
|
0
|
|
|
0
|
1
|
|
my ( $self, $p ) = ( shift, shift ); |
272
|
|
|
|
|
|
|
|
273
|
0
|
0
|
|
|
|
|
$$self{__GANTRY_PAGECACHE_ACTION__} = $p if defined $p; |
274
|
0
|
|
|
|
|
|
return( $$self{__GANTRY_PAGECACHE_ACTION__} ); |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
} # end gantry_pagecache_action |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
#------------------------------------------------- |
279
|
|
|
|
|
|
|
# $self->gantry_pagecache_mime_type |
280
|
|
|
|
|
|
|
#------------------------------------------------- |
281
|
|
|
|
|
|
|
sub gantry_pagecache_mime_type { |
282
|
0
|
|
|
0
|
1
|
|
my ( $self, $p ) = ( shift, shift ); |
283
|
|
|
|
|
|
|
|
284
|
0
|
0
|
|
|
|
|
$$self{__GANTRY_PAGECACHE_MIMETYPE__} = $p if defined $p; |
285
|
0
|
|
|
|
|
|
return( $$self{__GANTRY_PAGECACHE_MIMETYPE__} ); |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
} # end gantry_pagecache_mime_type |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
#------------------------------------------------- |
290
|
|
|
|
|
|
|
# $self->gantry_pagecache_uri |
291
|
|
|
|
|
|
|
#------------------------------------------------- |
292
|
|
|
|
|
|
|
sub gantry_pagecache_uri { |
293
|
0
|
|
|
0
|
1
|
|
my ( $self, $p ) = ( shift, shift ); |
294
|
|
|
|
|
|
|
|
295
|
0
|
0
|
|
|
|
|
$$self{__GANTRY_PAGECACHE_URI__} = $p if defined $p; |
296
|
0
|
|
|
|
|
|
return( $$self{__GANTRY_PAGECACHE_URI__} ); |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
} # end gantry_pagecache_uri |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
#------------------------------------------------- |
301
|
|
|
|
|
|
|
# $self->gantry_pagecache |
302
|
|
|
|
|
|
|
#------------------------------------------------- |
303
|
|
|
|
|
|
|
sub gantry_pagecache { |
304
|
0
|
|
|
0
|
1
|
|
my ( $self, $p ) = ( shift, shift ); |
305
|
|
|
|
|
|
|
|
306
|
0
|
0
|
|
|
|
|
$$self{__GANTRY_PAGECACHE__} = $p if defined $p; |
307
|
0
|
|
|
|
|
|
return( $$self{__GANTRY_PAGECACHE__} ); |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
} # end gantry_pagecache |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
1; |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
__END__ |