27 |
|
|
28 |
|
=head1 DESCRIPTION |
29 |
|
|
30 |
< |
Functions for reading and writing of cache files. This uses Data::Dumper to |
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 and stored in a variable which is then evalled to |
33 |
< |
restore the original object. |
32 |
> |
is read from the cache file using Storable::retrieve() which returns a variable |
33 |
> |
containing the original object. |
34 |
|
|
35 |
|
=head1 METHODS |
36 |
|
|
45 |
|
use English; |
46 |
|
use Exporter; |
47 |
|
|
48 |
+ |
use Storable; |
49 |
+ |
|
50 |
|
@ISA=qw(Exporter); |
51 |
|
|
52 |
|
# |
62 |
|
sub read() |
63 |
|
{ |
64 |
|
my ($cachefilename) = @_; |
65 |
< |
my $cachefh = IO::File->new($cachefilename, O_RDONLY) |
66 |
< |
|| 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"; |
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 |
|
} |
77 |
|
sub write() |
78 |
|
{ |
79 |
|
my ($cacheobject,$cachefilename) = @_; |
83 |
– |
|
84 |
– |
use Data::Dumper; |
80 |
|
use File::Copy; |
86 |
– |
|
81 |
|
print "[ CacheUtilities::write() ] Writing cache ",$cachefilename,"\n", if ($ENV{SCRAM_DEBUG}); |
88 |
– |
|
82 |
|
# Move the cache file to make a backup: |
83 |
|
move($cachefilename,$cachefilename.".bak") if ( -r $cachefilename); |
84 |
< |
# Dump the cache to file: |
85 |
< |
my $cachefh = IO::File->new($cachefilename, O_WRONLY|O_CREAT) |
86 |
< |
or die "Couldn't write to $cachefilename: ",$ERRNO,"\n"; |
87 |
< |
|
88 |
< |
# Name that should replace "VAR1" in the dumped |
89 |
< |
# representation of the cache object: |
90 |
< |
$Data::Dumper::Varname='cache'; |
98 |
< |
$Data::Dumper::Purity = 1; |
99 |
< |
print $cachefh Dumper($cacheobject); |
100 |
< |
close $cachefh; |
101 |
< |
|
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; |