1 |
#____________________________________________________________________
|
2 |
# File: CacheUtilities.pm
|
3 |
#____________________________________________________________________
|
4 |
#
|
5 |
# Author: Shaun Ashby <Shaun.Ashby@cern.ch>
|
6 |
# Update: 2003-10-30 11:51:58+0100
|
7 |
# Revision: $Id: CacheUtilities.pm,v 1.7.2.1 2007/02/26 18:33:45 sashby Exp $
|
8 |
#
|
9 |
# Copyright: 2003 (C) Shaun Ashby
|
10 |
#
|
11 |
#--------------------------------------------------------------------
|
12 |
|
13 |
=head1 NAME
|
14 |
|
15 |
Cache::CacheUtilities - Utilities for reading and writing of cache files.
|
16 |
|
17 |
=head1 SYNOPSIS
|
18 |
|
19 |
Reading:
|
20 |
|
21 |
print "Reading cached data","\n",if ($ENV{SCRAM_DEBUG});
|
22 |
$cacheobject=&Cache::CacheUtilities::read($cachename);
|
23 |
|
24 |
Writing:
|
25 |
|
26 |
&Cache::CacheUtilities::write($cacheobject,$cachename);
|
27 |
|
28 |
=head1 DESCRIPTION
|
29 |
|
30 |
Functions for reading and writing of cache files. This uses Storable::store() to
|
31 |
write out Perl data structures to files. For reading, the complete data structure
|
32 |
is read from the cache file using Storable::retrieve() which returns a variable
|
33 |
containing the original object.
|
34 |
|
35 |
=head1 METHODS
|
36 |
|
37 |
=over
|
38 |
|
39 |
=cut
|
40 |
|
41 |
package Cache::CacheUtilities;
|
42 |
require 5.004;
|
43 |
|
44 |
use IO::File;
|
45 |
use English;
|
46 |
use Exporter;
|
47 |
|
48 |
use Storable;
|
49 |
|
50 |
@ISA=qw(Exporter);
|
51 |
|
52 |
#
|
53 |
# Common functions for interacting with caches:
|
54 |
#
|
55 |
|
56 |
=item C<read($cachefilename)>
|
57 |
|
58 |
Read the cache file $cachefilename and return a Perl object.
|
59 |
|
60 |
=cut
|
61 |
|
62 |
sub read()
|
63 |
{
|
64 |
my ($cachefilename) = @_;
|
65 |
# Retrieve the cached object from the file:
|
66 |
$cache = eval "retrieve(\"$cachefilename\")";
|
67 |
die "Cache load error: ",$EVAL_ERROR,"\n", if ($EVAL_ERROR);
|
68 |
return $cache;
|
69 |
}
|
70 |
|
71 |
=item C<write($cacheobject,$cachefilename)>
|
72 |
|
73 |
Dump the Perl object $cacheobject to a file $cachefilename.
|
74 |
|
75 |
=cut
|
76 |
|
77 |
sub write()
|
78 |
{
|
79 |
my ($cacheobject,$cachefilename) = @_;
|
80 |
use File::Copy;
|
81 |
print "[ CacheUtilities::write() ] Writing cache ",$cachefilename,"\n", if ($ENV{SCRAM_DEBUG});
|
82 |
# Move the cache file to make a backup:
|
83 |
move($cachefilename,$cachefilename.".bak") if ( -r $cachefilename);
|
84 |
# Use the store method of the Storable package to write out the object to a file:
|
85 |
eval {
|
86 |
store($cacheobject,$cachefilename);
|
87 |
};
|
88 |
|
89 |
die "Cache write error: ",$EVAL_ERROR,"\n", if ($EVAL_ERROR);
|
90 |
|
91 |
# Change the permissions to -rw-r--r--:
|
92 |
my $filemode = 0644;
|
93 |
chmod $filemode, $cachefilename;
|
94 |
|
95 |
return;
|
96 |
}
|
97 |
|
98 |
1;
|
99 |
|
100 |
|
101 |
=back
|
102 |
|
103 |
=head1 AUTHOR/MAINTAINER
|
104 |
|
105 |
Shaun Ashby
|
106 |
|
107 |
=cut
|