line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gantry::Plugins::Cache::Storable; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4443
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
12
|
use Storable qw(store nstore_fd fd_retrieve retrieve freeze thaw dclone); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
142
|
|
7
|
1
|
|
|
1
|
|
7
|
use Gantry::Plugins::Cache; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
19
|
use base 'Exporter'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1335
|
|
10
|
|
|
|
|
|
|
our @EXPORT = qw( |
11
|
|
|
|
|
|
|
cache_clear |
12
|
|
|
|
|
|
|
cache_del |
13
|
|
|
|
|
|
|
cache_get |
14
|
|
|
|
|
|
|
cache_set |
15
|
|
|
|
|
|
|
cache_keys |
16
|
|
|
|
|
|
|
cache_init |
17
|
|
|
|
|
|
|
cache_purge |
18
|
|
|
|
|
|
|
cache_handle |
19
|
|
|
|
|
|
|
cache_inited |
20
|
|
|
|
|
|
|
cache_expires |
21
|
|
|
|
|
|
|
cache_namespace |
22
|
|
|
|
|
|
|
cache_filename |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub cache_init { |
26
|
0
|
|
|
0
|
1
|
|
my ($gobj) = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#my $num_pages = $gobj->fish_config('cache_pages') || '256'; |
29
|
|
|
|
|
|
|
#my $page_size = $gobj->fish_config('cache_pagesize') || '256k'; |
30
|
|
|
|
|
|
|
#my $expire_time = $gobj->fish_config('cache_expires') || '1h'; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
0
|
|
|
|
my $test_sets = $gobj->fish_config('cache_test_sets') || 0; |
33
|
0
|
|
0
|
|
|
|
my $share_file = $gobj->fish_config('cache_filename') |
34
|
|
|
|
|
|
|
|| '/tmp/gantry.storable.cache'; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$gobj->cache_filename( $share_file ); |
37
|
0
|
|
|
|
|
|
$gobj->cache_inited(1); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# If requested, test cache sets to see if they are successful. |
40
|
0
|
0
|
|
|
|
|
if ($test_sets) { |
41
|
0
|
|
|
|
|
|
eval { |
42
|
|
|
|
|
|
|
# Set test. |
43
|
0
|
|
|
|
|
|
$gobj->cache_set('test-ns:test-var', 1, 120); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Get test. |
46
|
0
|
|
|
|
|
|
my $data = $gobj->cache_get('test-ns:test-var'); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Die if set failed. |
49
|
0
|
0
|
|
|
|
|
unless ($data) { |
50
|
0
|
|
|
|
|
|
die "Test cache set failed. Please check cache configuration parameters.\n"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if ($@) { |
55
|
0
|
|
|
|
|
|
die('Unable to use - Gantry::Cache::Storable ' . $@ ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub cache_inited { |
61
|
0
|
|
|
0
|
1
|
|
my ($gobj, $p) = @_; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
$$gobj{__CACHE_INITED__} = $p if defined $p; |
64
|
0
|
|
|
|
|
|
return($$gobj{__CACHE_INITED__}); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub cache_filename { |
69
|
0
|
|
|
0
|
1
|
|
my ($gobj, $p) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
$$gobj{__CACHE_FILENAME__} = $p if defined $p; |
72
|
0
|
|
|
|
|
|
return($$gobj{__CACHE_FILENAME__}); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub cache_handle { |
77
|
0
|
|
|
0
|
1
|
|
my ($gobj, $p) = @_; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
$$gobj{__CACHE_HANDLE__} = $p if defined $p; |
80
|
0
|
|
|
|
|
|
return($$gobj{__CACHE_HANDLE__}); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub cache_namespace { |
85
|
0
|
|
|
0
|
1
|
|
my ($gobj, $p) = @_; |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
$$gobj{__CACHE_NAMESPACE__} = $p if defined $p; |
88
|
0
|
|
|
|
|
|
return($$gobj{__CACHE_NAMESPACE__}); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub cache_expires { |
93
|
0
|
|
|
0
|
1
|
|
my ($gobj, $p) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
$$gobj{__CACHE_EXPIRES__} = $p if defined $p; |
96
|
0
|
|
|
|
|
|
return($$gobj{__CACHE_EXPIRES__}); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub cache_get { |
101
|
0
|
|
|
0
|
1
|
|
my ($gobj, $key) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
0
|
|
|
|
my $namespace = $gobj->cache_namespace() || ''; |
104
|
0
|
|
|
|
|
|
my $skey = $namespace . ':' . $key; |
105
|
0
|
|
|
|
|
|
my $file = $gobj->cache_filename; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if ( -e $file ) { |
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
|
open( CACHE, $file ) or die "$!"; |
110
|
0
|
0
|
|
|
|
|
my $serialized = fd_retrieve( \*CACHE ) or die "retrieve error $!"; |
111
|
0
|
|
|
|
|
|
close( CACHE ); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $storable = thaw( $$serialized ); |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
if ( defined $storable->{$skey} ) { |
116
|
0
|
|
|
|
|
|
return( $storable->{$skey} ); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
else { |
119
|
0
|
|
|
|
|
|
return; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub cache_set { |
126
|
0
|
|
|
0
|
1
|
|
my ($gobj, $key, $val, $expires) = @_; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
0
|
|
|
|
my $namespace = $gobj->cache_namespace() || ''; |
129
|
0
|
|
|
|
|
|
my $skey = $namespace . ':' . $key; |
130
|
0
|
|
|
|
|
|
my $file = $gobj->cache_filename; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my $serialized; |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if ( ! -e $file ) { |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $storable = { |
137
|
|
|
|
|
|
|
$skey => $val |
138
|
|
|
|
|
|
|
}; |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$serialized = freeze( $storable ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else { |
143
|
0
|
0
|
|
|
|
|
my $tmp_serialized = retrieve( $file ) or die "retrieve error $!"; |
144
|
0
|
|
|
|
|
|
my $storable = thaw( $$tmp_serialized ); |
145
|
0
|
|
|
|
|
|
$storable->{$skey} = $val; |
146
|
0
|
|
|
|
|
|
$serialized = freeze( $storable ); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
0
|
0
|
|
|
|
|
open( CACHE, ">", $file ) or die "$!"; |
150
|
0
|
0
|
|
|
|
|
nstore_fd( \$serialized, \*CACHE ) or die "Can't store $key!\n"; |
151
|
0
|
|
|
|
|
|
close( CACHE ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub cache_clear { |
155
|
0
|
|
|
0
|
1
|
|
my($gobj) = @_; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
my $file = $gobj->cache_filename(); |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
if ( -e $file ) { |
160
|
0
|
|
|
|
|
|
unlink( $file ); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub cache_keys { |
165
|
0
|
|
|
0
|
1
|
|
my($gobj ) = @_; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
my $file = $gobj->cache_filename(); |
168
|
0
|
|
0
|
|
|
|
my $namespace = $gobj->cache_namespace() || ''; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my @keys_new; |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
if ( -e $file ) { |
173
|
|
|
|
|
|
|
|
174
|
0
|
0
|
|
|
|
|
my $tmp_serialized = retrieve( $file ) or die "retrieve error $!"; |
175
|
0
|
|
|
|
|
|
my $storable = thaw( $$tmp_serialized ); |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
foreach my $k ( keys %{ $storable } ) { |
|
0
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
$k =~ s/^$namespace\://; |
179
|
0
|
|
|
|
|
|
push( @keys_new, $k ); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
return \@keys_new; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub cache_del { |
187
|
0
|
|
|
0
|
1
|
|
my ($gobj, $key) = @_; |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
0
|
|
|
|
my $namespace = $gobj->cache_namespace() || ''; |
190
|
0
|
|
|
|
|
|
my $skey = $namespace . ':' . $key; |
191
|
0
|
|
|
|
|
|
my $file = $gobj->cache_filename(); |
192
|
|
|
|
|
|
|
|
193
|
0
|
0
|
|
|
|
|
if ( -e $file ) { |
194
|
0
|
0
|
|
|
|
|
my $tmp_serialized = retrieve( $file ) or die "retrieve error $!"; |
195
|
0
|
|
|
|
|
|
my $storable = thaw( $$tmp_serialized ); |
196
|
0
|
|
|
|
|
|
delete( $storable->{$skey} ); |
197
|
0
|
|
|
|
|
|
my $serialized = freeze( $storable ); |
198
|
|
|
|
|
|
|
|
199
|
0
|
0
|
|
|
|
|
open( CACHE, ">", $file ) or die "$!"; |
200
|
0
|
0
|
|
|
|
|
nstore_fd( \$serialized, \*CACHE ) or die "Can't store $key!\n"; |
201
|
0
|
|
|
|
|
|
close( CACHE ); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub cache_purge { |
206
|
0
|
|
|
0
|
1
|
|
my ($gobj) = @_; |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
return; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
1; |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
__END__ |