XPP/Cache/Store/File.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 188 CC 21 SCAR 30.4
CC
21
Cov
0%
new
30.0
location
30.0
cachedir
30.0
CRAP 20.8
1 # Apache::XPP::Cache::Store::File
2 # ---------------------------------
3 # $Revision: 1.9 $
4 # $Date: 2002/01/16 21:06:01 $
5 # -----------------------------------------------------
6  
7 =head1 NAME
8  
9 Apache::XPP::Cache::Store::File - flatfile cache store
10  
11 =cut
12  
13 package Apache::XPP::Cache::Store::File;
14  
15 =head1 SYNOPSIS
16  
17 ...
18  
19 =head1 REQUIRES
20  
21 Apache::XPP::Cache::Store
22 FileHandle
23 File::stat
24  
25 =cut
26  
27 use Carp;
28 use strict;
29 use File::stat;
30 use FileHandle;
31 use Data::Dumper;
32 use Apache::XPP::Cache::Store;
33 use vars qw( @ISA $debug $debuglines );
34  
35 BEGIN {
36 @ISA = qw( Apache::XPP::Cache::Store );
37 $Apache::XPP::Cache::Store::File::REVISION = (qw$Revision: 1.9 $)[-1];
38 $Apache::XPP::Cache::Store::File::VERSION = '2.01';
39 $debug = undef;
40 $debuglines = 1;
41 }
42  
43 =head1 EXPORTS
44  
45 Nothing
46  
47 =head1 DESCRIPTION
48  
49 Apache::XPP::Cache::Store::File handles the storing of data in flat file form on behalf
50 of Apache::XPP::Cache.
51  
52 =head1 METHODS
53  
54 =over
55  
56 =item C<new> ( $name, $group, \%instance_data, \$content )
57  
58 Creates a new File store object. The contents of %instance_data will be placed in the object
59 as instance data (for Apache request object, etc.).
60  
61 =cut
62 sub new {
63 my $proto = shift;
64 my $class = ref($proto) || $proto;
65 my $name = shift;
66 my $group = shift;
67 my $instance = shift;
68
69 my $self = bless( { %{ ref($instance) ? $instance : {} } }, $class );
70 my $filename = $self->location( $name, $group );
71
72 $self->name( $name );
73 $self->group( $group );
74 $self->filename( $filename );
75
76 if (my $content = shift) {
77 warn "file: setting content ($content) in cache object" . ($debuglines ? '' : "\n") if ($debug >= 2);
78 $self->content( $content );
79 }
80
81 return $self;
82 } # END constructor new
83  
84  
85 =item C<location> ( $name, $group )
86  
87 Returns the fully qualified filename to the store file for the specified name/group pair.
88 Files are stored by their $name in the directory $group. If the directory $group does
89 not exist, it will be created with permissions of 0777 (use the C<umask> function to
90 change these permissions to more desirable ones).
91  
92 =cut
93 sub location {
94 my $proto = shift;
95 my $class = ref($proto) || $proto;
96 my $name = shift;
97 my $group = shift;
98
99 $name =~ s#/#_#g;
100 $group =~ s#/#_#g;
101
102 my $directory = join('/', $proto->cachedir, $group);
103 unless (-d $directory) {
104 warn "file: creating directory '$directory'" . ($debuglines ? '' : "\n") if ($debug);
105 if (!mkdir( $directory, 0777 )) {
106 warn "Failed to create directory '$directory'! $!";
107 return undef;
108 }
109 }
110
111 return $directory . '/' . $name;
112 } # END method location
113  
114  
115 =item C<cachedir> ( )
116  
117 Returns the directory in which file caches are stored.
118  
119 =cut
120 sub cachedir {
121 my $self = shift;
122 warn "file: cachedir called" . ($debuglines ? '' : "\n") if ($debug >= 2);
123 my $cachedir = ref($self->r) ? $self->r->dir_config('XPPFileCacheDir') : '/tmp/cache';
124 $cachedir = $self->r->server_root_relative($cachedir)
125 unless( $cachedir =~ /^\// );
126 $cachedir =~ m#^([/.\-\w]*)$#;
127 return $1;
128 } # END method cachedir
129  
130  
131 =item C<content> ( [ \$content ] )
132  
133 Sets the store object's content to $content and returns TRUE.
134 If $content is omitted, returns the content of the store object.
135  
136 =cut
137 sub content {
138 my $self = shift;
139 my $class = ref($self) || return undef;
140 my $filename = $self->filename;
141 if (my $content = shift) {
142 my $fh = new FileHandle ("> $filename");
143 return undef unless ((defined $fh) && (ref($content)));
144 print $fh ${ $content };
145 $fh->close;
146 return 1;
147 } else {
148 my $fh = new FileHandle ($filename);
149 return undef unless (defined $fh);
150 local($/) = undef;
151 return <$fh>;
152 }
153 } # END method content
154  
155  
156 =item C<is_expired> ( )
157  
158 Removes the store file.
159  
160 =cut
161 sub is_expired {
162 my $self = shift;
163 my $class = ref($self) || return undef;
164 my $filename = $self->filename;
165 unlink $filename || warn "file\t: cannot delete file ($filename): $!" . ($debuglines ? '' : "\n");
166 return 1;
167 } # END method is_expired
168  
169  
170 =item C<mtime> ( )
171  
172 =item C<mtime> ( $name, $group )
173  
174 Returns the modification time of the specified store.
175  
176 =cut
177 sub mtime {
178 my $proto = shift;
179 my $filename;
180 if (my $class = ref($proto)) {
181 $filename = $proto->filename;
182 } else {
183 my $name = shift;
184 my $group = shift;
185 $filename = $proto->location( $name, $group );
186 }
187
188 my $st = stat($filename); # using File::stat
189 unless (ref($st) && $st->can('mtime')) {
190 warn "file:\tcannot stat file ($filename): $!" . ($debuglines ? '' : "\n") if ($debug);
191 return undef;
192 }
193 return $st->mtime;
194 } # END method mtime
195  
196  
197 1;
198  
199 __END__