| 1 | package WebService::Cache::FileCacheManager; | ||
| 2 | use strict; | ||
| 3 | use Digest::MD5 qw(md5_hex); | ||
| 4 | use Storable qw(store retrieve); | ||
| 5 | use base qw(WebService::Cache::AbstractCacheManager); | ||
| 6 | __PACKAGE__->mk_classdata('cache_dir'); | ||
| 7 | |||
| 8 | |||
| 9 | sub init{ | ||
| 10 | my $self = shift; | ||
| 11 | my %args = @_; | ||
| 12 | $self->SUPER::init(@_); | ||
| 13 | if(exists $args{'cache_dir'}){ | ||
| 14 | $self->cache_dir($args{'cache_dir'}); | ||
| 15 | } | ||
| 16 | else{ | ||
| 17 | die "Require cache dirctory path " ; | ||
| 18 | } | ||
| 19 | if(! -f $self->cache_dir){ | ||
| 20 | mkdir $self->cache_dir(); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | sub store_cache{ | ||
| 25 | my $self = shift; | ||
| 26 | my $id = shift; | ||
| 27 | my $result = shift; | ||
| 28 | store \$result ,$self->cache_dir.$id; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub retrieve_cache{ | ||
| 32 | my $self = shift; | ||
| 33 | my $id = shift; | ||
| 34 | return retrieve $self->cache_dir.$id; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub is_cached{ | ||
| 38 | my $self = shift; | ||
| 39 | my $id = shift; | ||
| 40 | return 0 unless(-f $self->cache_dir .$id); | ||
| 41 | my @file_status = stat($self->cache_dir .$id); | ||
| 42 | return 0 if($self->lifetime < (time - $file_status[9])); | ||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | 1; |