1 |
sashby |
1.2 |
package Cache::CacheUtilities;
|
2 |
|
|
require 5.004;
|
3 |
muzaffar |
1.9.2.2.2.1 |
use Storable qw(nfreeze thaw retrieve nstore);
|
4 |
|
|
BEGIN {
|
5 |
|
|
eval "use Compress::Zlib qw(gzopen);";
|
6 |
|
|
}
|
7 |
sashby |
1.5 |
|
8 |
sashby |
1.2 |
sub read()
|
9 |
|
|
{
|
10 |
muzaffar |
1.9.2.2.2.1 |
my $file = shift;
|
11 |
|
|
my $cache=undef;
|
12 |
|
|
my $gz = gzopen($file, "rb");
|
13 |
|
|
if ($gz)
|
14 |
|
|
{
|
15 |
|
|
my $buf;my $data;
|
16 |
|
|
while ($gz->gzread($buf,1024*1024) > 0){$data.=$buf;}
|
17 |
|
|
$gz->gzclose();
|
18 |
|
|
$cache=eval {thaw($data);};
|
19 |
|
|
if ($EVAL_ERROR){die "Cache loading error: ",$EVAL_ERROR,"\n";}
|
20 |
|
|
}
|
21 |
|
|
else{die "Can not open file for reading: $file";}
|
22 |
sashby |
1.2 |
return $cache;
|
23 |
|
|
}
|
24 |
muzaffar |
1.9.2.2.2.1 |
|
25 |
sashby |
1.2 |
sub write()
|
26 |
|
|
{
|
27 |
muzaffar |
1.9.2.2.2.1 |
my ($cache,$file) = @_;
|
28 |
sashby |
1.2 |
use File::Copy;
|
29 |
muzaffar |
1.9.2.2.2.1 |
if (-r $file){move($file,"${file}.bak");}
|
30 |
|
|
my $ok=1;
|
31 |
|
|
my $gz = gzopen($file, "wb");
|
32 |
|
|
if ($gz)
|
33 |
|
|
{
|
34 |
|
|
eval {$gz->gzwrite(nfreeze($cache));};
|
35 |
|
|
if ($EVAL_ERROR){$ok=0;}
|
36 |
|
|
$gz->gzclose();
|
37 |
|
|
}
|
38 |
|
|
else{$ok=0;}
|
39 |
|
|
if ($ok)
|
40 |
|
|
{
|
41 |
|
|
unlink ("${file}.bak");
|
42 |
|
|
my $mode=0644;
|
43 |
|
|
chmod $mode,$file;
|
44 |
|
|
}
|
45 |
|
|
else
|
46 |
|
|
{
|
47 |
|
|
if (-r "${file}.bak"){move("${file}.bak",$file);}
|
48 |
|
|
die "ERROR: Writing Cache file: $file";
|
49 |
|
|
}
|
50 |
sashby |
1.2 |
return;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
1;
|