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.6.2.3 2006/09/11 11:19:58 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 Data::Dumper to
|
31 |
write out Perl data structures to files. For reading, the complete data structure
|
32 |
is read from the cache file and stored in a variable which is then evalled to
|
33 |
restore 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 |
@ISA=qw(Exporter);
|
49 |
|
50 |
#
|
51 |
# Common functions for interacting with caches:
|
52 |
#
|
53 |
|
54 |
=item C<read($cachefilename)>
|
55 |
|
56 |
Read the cache file $cachefilename and return a Perl object.
|
57 |
|
58 |
=cut
|
59 |
|
60 |
sub read()
|
61 |
{
|
62 |
my ($cachefilename) = @_;
|
63 |
my $cachefh = IO::File->new($cachefilename, O_RDONLY)
|
64 |
|| die "Unable to read cached data file $cachefilename: ",$ERRNO,"\n";
|
65 |
my @cacheitems = <$cachefh>;
|
66 |
close $cachefh;
|
67 |
|
68 |
# Copy the new cache object to self and return:
|
69 |
$cache = eval "@cacheitems";
|
70 |
die "Cache load error: ",$EVAL_ERROR,"\n", if ($EVAL_ERROR);
|
71 |
return $cache;
|
72 |
}
|
73 |
|
74 |
=item C<write($cacheobject,$cachefilename)>
|
75 |
|
76 |
Dump the Perl object $cacheobject to a file $cachefilename.
|
77 |
|
78 |
=cut
|
79 |
|
80 |
sub write()
|
81 |
{
|
82 |
my ($cacheobject,$cachefilename) = @_;
|
83 |
|
84 |
use Data::Dumper;
|
85 |
use File::Copy;
|
86 |
|
87 |
print "[ CacheUtilities::write() ] Writing cache ",$cachefilename,"\n", if ($ENV{SCRAM_DEBUG});
|
88 |
|
89 |
# Move the cache file to make a backup:
|
90 |
move($cachefilename,$cachefilename.".bak") if ( -r $cachefilename);
|
91 |
# Dump the cache to file:
|
92 |
my $cachefh = IO::File->new($cachefilename, O_WRONLY|O_CREAT)
|
93 |
or die "Couldn't write to $cachefilename: ",$ERRNO,"\n";
|
94 |
|
95 |
# Name that should replace "VAR1" in the dumped
|
96 |
# representation of the cache object:
|
97 |
$Data::Dumper::Varname='cache';
|
98 |
$Data::Dumper::Purity = 1;
|
99 |
print $cachefh Dumper($cacheobject);
|
100 |
close $cachefh;
|
101 |
|
102 |
# Change the permissions to -rw-r--r--:
|
103 |
my $filemode = 0644;
|
104 |
chmod $filemode, $cachefilename;
|
105 |
|
106 |
return;
|
107 |
}
|
108 |
|
109 |
1;
|
110 |
|
111 |
|
112 |
=back
|
113 |
|
114 |
=head1 AUTHOR/MAINTAINER
|
115 |
|
116 |
Shaun Ashby
|
117 |
|
118 |
=cut
|