| 1 | package Cake::Plugins::Simple::Cache; | ||
| 2 | use Cake 'Plugin'; | ||
| 3 | use Carp; | ||
| 4 | |||
| 5 | my $settings; | ||
| 6 | my $cacheinstance; | ||
| 7 | |||
| 8 | sub _cache { | ||
| 9 | $settings = settings if !$settings; #memoize | ||
| 10 | $cacheinstance = Cake::Plugins::Simple::Cache::Base->new( | ||
| 11 | cache_root => $settings->{cache_root}, | ||
| 12 | expire_time => $settings->{expire_time}, | ||
| 13 | depth => $settings->{depth} | ||
| 14 | ); | ||
| 15 | return $cacheinstance; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub cache { | ||
| 19 | my $self = shift; | ||
| 20 | if (@_ > 1 && ref $_[1]){ | ||
| 21 | return _cache->set(@_); | ||
| 22 | } | ||
| 23 | return _cache->get(@_); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub get_cache_location { | ||
| 27 | shift; | ||
| 28 | my ($path,$file) = _cache->_get_path_and_file(@_); | ||
| 29 | |||
| 30 | wantarray ? return ($path,$file) : | ||
| 31 | return $path.'/'.$file; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub newCache { | ||
| 35 | my $self = shift; | ||
| 36 | my %settings = @_; | ||
| 37 | return Cake::Plugins::Simple::Cache::Base->new(%settings); | ||
| 38 | } | ||
| 39 | |||
| 40 | register(); | ||
| 41 | |||
| 42 | package #hide | ||
| 43 | Cake::Plugins::Simple::Cache::Base; | ||
| 44 | use Digest::MD5 qw(md5_hex); | ||
| 45 | use File::Spec; | ||
| 46 | use File::Path qw(make_path); | ||
| 47 | use Storable qw( retrieve nstore ); | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | my %options = @_; | ||
| 52 | my $self = { | ||
| 53 | expire_time => $options{expire_time} || 3600, | ||
| 54 | cache_root => $options{cache_root} || '/', | ||
| 55 | depth => $options{depth} || 3 | ||
| 56 | }; | ||
| 57 | |||
| 58 | $self = bless ($self,$class); | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub cache { | ||
| 63 | my $self = shift; | ||
| 64 | if (@_ > 1 && ref $_[1]){ | ||
| 65 | return $self->set(@_); | ||
| 66 | } | ||
| 67 | return $self->get(@_); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub set { | ||
| 71 | my $self = shift; | ||
| 72 | my ($name,$data,$expire,$subfolder) = @_; | ||
| 73 | |||
| 74 | ###get expire time | ||
| 75 | my $expire_time = $self->_get_expire_time($expire || $self->{expire_time}); | ||
| 76 | |||
| 77 | ###get file and path | ||
| 78 | my ($path,$file) = $self->_get_path_and_file($name,$subfolder); | ||
| 79 | my $fullfile = $path.'/'.$file; | ||
| 80 | |||
| 81 | make_path( $path, { | ||
| 82 | mode => 0755 | ||
| 83 | }); #if !-d $path; | ||
| 84 | #mkpath( $path, 0, 775 ) if !-d $path; | ||
| 85 | |||
| 86 | my $ca = { | ||
| 87 | expire_time => $expire_time, | ||
| 88 | date_created => time(), | ||
| 89 | data => $data | ||
| 90 | }; | ||
| 91 | |||
| 92 | nstore ($ca, $fullfile) or die "can't write to $fullfile\n"; | ||
| 93 | return $data; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub get { | ||
| 97 | my ($self,$name,$subfolder) = @_; | ||
| 98 | if ($subfolder && $subfolder =~ m/^\//){ | ||
| 99 | $self->{cache_root} = $subfolder; | ||
| 100 | $subfolder = ''; | ||
| 101 | } | ||
| 102 | |||
| 103 | my ($path,$file) = $self->_get_path_and_file($name,$subfolder); | ||
| 104 | $file = $path.'/'.$file; | ||
| 105 | |||
| 106 | local $SIG{__DIE__} = 'IGNORE'; | ||
| 107 | my $data; | ||
| 108 | eval { | ||
| 109 | $data = retrieve($file); | ||
| 110 | }; | ||
| 111 | |||
| 112 | if ($@){ | ||
| 113 | #die $@; | ||
| 114 | undef $@; | ||
| 115 | return undef; | ||
| 116 | } elsif (time() > $data->{expire_time}){ | ||
| 117 | return undef; | ||
| 118 | } | ||
| 119 | |||
| 120 | return $data->{data}; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _get_expire_time { | ||
| 124 | my $self = shift; | ||
| 125 | my $length = shift; | ||
| 126 | return Cake::Utils::to_epoch($length); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | sub _get_path_and_file { | ||
| 131 | my ($self,$name,$subfolder) = @_; | ||
| 132 | my $digest = md5_hex($name); | ||
| 133 | |||
| 134 | ###get folder map | ||
| 135 | my @deep; | ||
| 136 | my $file = $digest; | ||
| 137 | |||
| 138 | push( @deep, | ||
| 139 | map { substr( $digest, $_, 1 ) } ( 0 .. $self->{depth} - 1 ) ); | ||
| 140 | |||
| 141 | |||
| 142 | my $cache_root = $self->{cache_root}; | ||
| 143 | $cache_root .= '/' . $subfolder if $subfolder; | ||
| 144 | #my $x = File::Spec->catfile(@deep, $file); | ||
| 145 | my $path = join('/',$cache_root,@deep); | ||
| 146 | return ($path,$file); | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ |