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 |
|
|
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 |
< |
my $cachefh = IO::File->new($cachefilename, O_RDONLY) |
66 |
< |
|| 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"; |
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) = @_; |
42 |
– |
|
43 |
– |
use Data::Dumper; |
80 |
|
use File::Copy; |
81 |
< |
|
82 |
< |
# Rename the cache file to make a backup 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 |
< |
# 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'; |
91 |
< |
$Data::Dumper::Purity = 1; |
92 |
< |
print $cachefh Dumper($cacheobject); |
93 |
< |
close $cachefh; |
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 |