4 CXGN::Tools::WebImageCache - manages a local cache of images that are
5 generated on the fly from data
9 my $cache = CXGN::Tools::WebImageCache->new();
10 $cache->set_key("abc");
11 $cache->set_expiration_time(86400); # seconds, this would be a day.
12 $cache->set_map_name("map_name"); # what's in the <map name='map_name' tag.
13 $cache->set_temp_dir("/documents/tempfiles/cview");
14 $cache->set_basedir("/data/local/website/"); # would get this from VHost...
15 if (! $cache->is_valid()) {
16 # generate the image and associated image map.
20 $cache->set_image_data($img_data);
21 $cache->set_image_map_data($image_map_data);
23 print $cache->get_image_html();
30 Lukas Mueller <lam87@cornell.edu>
34 This class implements the following methods:
41 package CXGN
::Tools
::WebImageCache
;
43 use base
"CXGN::Debug";
46 use File
::Basename
();
51 Synopsis: $wic = CXGN::Tools::WebImageCache->new(
52 { expiration_time => 80000,
67 my $self = bless {}, $class;
68 $self->set_expiration_time($args->{expiration_time
})
69 if exists($args->{expiration_time
});
71 $self->set_cache_name($args->{filename
})
72 if exists($args->{filename
});
74 $self->set_key($args->{key
})
75 if exists($args->{key
});
77 $self->set_temp_dir($args->{temp_dir
})
78 if exists($args->{temp_dir
});
80 $self->set_basedir($args->{basedir
})
81 if exists($args->{basedir
});
83 $self->set_force($args->{force
})
84 if exists($args->{force
});
86 $self->set_function($args->{function
})
87 if exists($args->{function
});
89 $self->set_image_type($args->{image_type
})
90 if exists($args->{image_type
});
95 =head2 accessors set_temp_dir, get_temp_dir
97 Property: the tempfile directory, excluding the basepath
105 return $self->{temp_dir
};
111 $self->{temp_dir
}=shift;
114 =head2 accessors set_basedir, get_basedir
116 Property: the basepath. Forms the fully qualified path if
117 temp_dir is appended to it.
125 return $self->{basedir
};
130 $self->{basedir
}=shift;
133 =head2 function get_file_url
135 Synopsis: returns the url of the tempfile
143 return File
::Spec
->catfile($self->get_temp_dir(), $self->get_cache_name());
146 =head2 function get_filepath
148 Synopsis: returns the path of the temp file
156 return File
::Spec
->catfile($self->get_basedir(), $self->get_temp_dir(), $self->get_cache_name());
159 =head2 function get_image_path
161 Description: returns the fully qualified path to the image.
167 return $self->get_filepath().".".$self->get_image_type();
170 =head2 function get_image_url
172 Description: returns the url of the cache image
178 $self->get_file_url().".".$self->get_image_type();
181 =head2 function get_image_map_path
183 Description: returns the fully qualified path to the file
187 sub get_image_map_path
{
189 return $self->get_filepath().".map";
192 =head2 accessors set_expiration_time, get_expiration_time
194 Property: the time in seconds after which the cache is considered
196 Args/Rets: the number of seconds that specify the expiration time
197 Side Effects: the cache will be considered invalid after this number
198 of seconds have elapsed, and is_valid() will return
199 false, even if the cache file exists.
200 If no expiration time is set, the cached file will never
206 sub get_expiration_time
{
208 return $self->{expiration_time
};
211 sub set_expiration_time
{
213 my $expiration_time = shift;
214 unless($expiration_time =~ /^\d+$/){
215 $expiration_time = 60 if $expiration_time =~ /minute/i;
216 $expiration_time = 3600 if $expiration_time =~ /hour/i;
217 $expiration_time = 3600*24 if $expiration_time =~ /day/i;
218 $expiration_time = 3600*24*7 if $expiration_time =~ /week/i;
219 $expiration_time = 3600*24*30 if $expiration_time =~ /month/i;
220 $expiration_time = 3600*24*365 if $expiration_time =~ /year/i;
222 $self->{expiration_time
} = $expiration_time;
225 =head2 accessors set_key, get_key
227 Property: the unique key that identifies the web image,
228 such as a concatenation of parameters that are
229 used to generate the image in a webpage.
230 This property needs to be set, otherwise the
239 if (!exists($self->{key
})) { die "use set_key to set a key. cannot proceed.\n"; }
248 =head2 accessors set_cache_name, get_cache_name
250 Property: the name of the cache file. An md5checksum of the
251 key is the default. Should probably not be changed...
259 return $self->{cache_name
};
264 $self->{cache_name
}=shift;
267 =head2 accessors set_image_data, get_image_data
269 Property: image data, as a string
270 Args/Ret: the image data, as a string.
271 Side Effects: writes the string to the cache image file.
278 open (my $FILE, "<", $self->get_image_path()) || die "Can't open ".$self->get_image_path()." for reading";
279 my @contents = <$FILE>;
281 return join "\n", @contents;
288 $self->d("Generating image cache file ".$self->get_image_path()."\n");
290 my $dir = File
::Basename
::dirname
( $self->get_image_path );
291 File
::Path
::mkpath
($dir);
292 open (my $FILE, ">", $self->get_image_path()) || die "Can't open ".$self->get_image_path()." for writing";
293 print $FILE join "\n", @contents;
297 =head2 accessors set_image_map_data, get_image_map_data
299 Property: the image map data as a string
300 Side Effects: writes/retrieves the image map data to/from
306 sub get_image_map_data
{
308 $self->d("Generating image map cache file ".$self->get_image_map_path()."\n");
309 open (my $FILE, "<".($self->get_image_map_path())) || die "Can't open ".$self->get_image_map_path();
310 my @contents = <$FILE>;
312 return join "\n", @contents;
316 sub set_image_map_data
{
319 open (my $FILE, ">".($self->get_image_map_path())) || die "Can't open ".$self->get_image_map_path();
320 print $FILE join "\n", @contents;
325 =head2 function is_valid
327 Synopsis: $wic->is_valid()
329 Returns: true if the cache is valid, false if not
331 Description: the cache may be valid because it was not
332 initialized, or because the time expiration has
333 occurred, or because the cache's force parameter
341 # generate the filename from the key
344 if ($self->get_force() eq "1") {
345 $self->d("Force reloading cache...\n");
349 # is there a cache file?
350 if (-e
$self->get_image_path()) {
352 # has the expiration time already expired?
353 if ($self->get_expiration_time()) {
355 my $mtime = (stat($self->get_image_path()))[9];
356 my $age = time()-$mtime;
357 if ($age > $self->get_expiration_time()) {
358 $self->d("ARGH! Cache has expired!!!!!\n");
363 # the cache file is ok.
364 $self->d("Cache exists...\n");
369 $self->d("Cache DOES NOT exist!\n");
374 =head2 function get_image_tag
376 Synopsis: my $img_tag = $wic -> get_image_tag();
378 Returns: the html for the cached image.
386 my $image_url = $self->get_image_url();
387 my $usemap = $self->get_map_name();
388 no warnings
'uninitialized';
389 return qq{ <img src
="$image_url" border
="0" usemap
="#$usemap" />\n };
392 =head2 accessors set_map_name, get_map_name
394 Property: the name of the image map, to link to the image with usemap.
402 return $self->{map_name
};
407 $self->{map_name
}=shift;
410 =head2 accessors set_force, get_force
412 Property: force - whether to force the generation of
414 Setter Args: true to force generation of new file
415 false to use cache if present
417 Side Effects: true value will cause old file to be deleted
424 if (!exists($self->{force
}) || !defined($self->{force
})) {
427 return $self->{force
};
432 $self->{force
}=shift;
435 =head2 function get_image_html
437 Synopsis: $image->get_image_html()
439 Returns: a string representing the image in html,
440 including the image tag and the image map.
448 my $map_data = undef;
449 eval{$map_data = $self->get_image_map_data()};
450 $map_data = "" if $@
;
451 return $self->get_image_tag() ."<br />". $map_data;
454 =head2 accessors set_function, get_function
456 Property: (Don't know what that is? Who added this function?)
467 return $self->{function
};
472 $self->{function
}=shift;
475 =head2 accessors set_image_type, get_image_type
477 Property: the type of image to be handled.
478 Args/Ret: the image type, such as png, jpg, etc.
479 Side Effects: used to construct the cache image name
480 Description: default returned is "png"
486 if (!exists($self->{image_type
}) || !defined($self->{image_type
})) {
487 $self->{image_type
}="png";
489 return $self->{image_type
};
494 $self->{image_type
}=shift;
497 =head2 function _hash
509 my $filename = Digest
::MD5
->new()->add($self->get_key())->hexdigest();
510 $self->d("Generated filename $filename\n");
511 $self->set_cache_name($filename);
527 return unlink($self->get_image_path(), $self->get_image_map_path());