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$ |
8 |
+ |
# |
9 |
+ |
# Copyright: 2003 (C) Shaun Ashby |
10 |
+ |
# |
11 |
+ |
#-------------------------------------------------------------------- |
12 |
+ |
package Cache::CacheUtilities; |
13 |
+ |
require 5.004; |
14 |
+ |
|
15 |
+ |
use IO::File; |
16 |
+ |
use English; |
17 |
+ |
use Exporter; |
18 |
+ |
|
19 |
+ |
@ISA=qw(Exporter); |
20 |
+ |
|
21 |
+ |
# |
22 |
+ |
# Common functions for interacting with caches: |
23 |
+ |
# |
24 |
+ |
|
25 |
+ |
sub read() |
26 |
+ |
{ |
27 |
+ |
my ($cachefilename) = @_; |
28 |
+ |
my $cachefh = IO::File->new($cachefilename, O_RDONLY) |
29 |
+ |
|| die "Unable to read cached data file $cachefilename: ",$ERRNO,"\n"; |
30 |
+ |
my @cacheitems = <$cachefh>; |
31 |
+ |
close $cachefh; |
32 |
+ |
|
33 |
+ |
# Copy the new cache object to self and return: |
34 |
+ |
$cache = eval "@cacheitems"; |
35 |
+ |
die "Cache load error: ",$EVAL_ERROR,"\n", if ($EVAL_ERROR); |
36 |
+ |
return $cache; |
37 |
+ |
} |
38 |
+ |
|
39 |
+ |
sub write() |
40 |
+ |
{ |
41 |
+ |
my ($cacheobject,$cachefilename) = @_; |
42 |
+ |
|
43 |
+ |
use Data::Dumper; |
44 |
+ |
use File::Copy; |
45 |
+ |
|
46 |
+ |
# Rename the cache file to make a backup copy: |
47 |
+ |
move($cachefilename,$cachefilename.".bak") if ( -r $cachefilename); |
48 |
+ |
# Dump the cache to file: |
49 |
+ |
my $cachefh = IO::File->new($cachefilename, O_WRONLY|O_CREAT) |
50 |
+ |
or die "Couldn't write to $cachefilename: ",$ERRNO,"\n"; |
51 |
+ |
|
52 |
+ |
# Name that should replace "VAR1" in the dumped |
53 |
+ |
# representation of the cache object: |
54 |
+ |
$Data::Dumper::Varname='cache'; |
55 |
+ |
$Data::Dumper::Purity = 1; |
56 |
+ |
print $cachefh Dumper($cacheobject); |
57 |
+ |
close $cachefh; |
58 |
+ |
|
59 |
+ |
return; |
60 |
+ |
} |
61 |
+ |
|
62 |
+ |
1; |