| 1 | package Weather::OpenWeatherMap::Cache; | ||||||||||||||||||||||||||||
| 2 | 1 | $Weather::OpenWeatherMap::Cache::VERSION = '0.005001'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Storable (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Digest::SHA 'sha1_hex'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use List::Objects::WithUtils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Types::Standard -all; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use List::Objects::Types -all; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Types::Path::Tiny -all; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has dir => ( | ||||||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | isa => AbsDir, | ||||||||||||||||||||||||||||
| 30 | coerce => 1, | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | builder => sub { Path::Tiny->tempdir(CLEANUP => 1) }, | ||||||||||||||||||||||||||
|
Subroutine
_build_dir: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has expiry => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => StrictNum, | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | builder => sub { 1200 }, | ||||||||||||||||||||||||||
|
Subroutine
_build_expiry: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | ## FIXME max_entries ? or a size limit ? | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub serialize { | ||||||||||||||||||||||||||||
| 43 | 8 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 8 | Storable::freeze( | |||||||||||||||||||||||||||
| 45 | [ Time::HiRes::time, $obj ] | ||||||||||||||||||||||||||||
| 46 | ) | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub deserialize { | ||||||||||||||||||||||||||||
| 50 | 12 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 12 | Storable::thaw($data) | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub make_path { | ||||||||||||||||||||||||||||
| 55 | 16 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_path: called CC=10 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 16 | ▶ | $obj = $obj->request | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Weather::OpenWeatherMap::Result")
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Weather::OpenWeatherMap::Result")
builtin::blessed($obj)
$obj->isa("Weather::OpenWeatherMap::Result")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Weather::OpenWeatherMap::Result")
| |||||||||||||||||||||||||||||
| 58 | if blessed($obj) | ||||||||||||||||||||||||||||
| 59 | and $obj->isa('Weather::OpenWeatherMap::Result'); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 16 | ▶ | confess "Expected a Weather::OpenWeatherMap::Request but got $obj" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Weather::OpenWeatherMap::Request")
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Weather::OpenWeatherMap::Request")
builtin::blessed($obj)
$obj->isa("Weather::OpenWeatherMap::Request")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Weather::OpenWeatherMap::Request")
| |||||||||||||||||||||||||||||
| 62 | unless blessed($obj) and $obj->isa('Weather::OpenWeatherMap::Request'); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 16 | my $fname = 'W'; | |||||||||||||||||||||||||||
| 65 | # also see cache_paths; new cache prefixes need added there also | ||||||||||||||||||||||||||||
| 66 | TYPE: { | ||||||||||||||||||||||||||||
| 67 | 16 | ▶ | if ($obj->isa('Weather::OpenWeatherMap::Request::Current')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 4 | $fname .= 'C'; | |||||||||||||||||||||||||||
| 69 | last TYPE | ||||||||||||||||||||||||||||
| 70 | 4 | } | |||||||||||||||||||||||||||
| 71 | 12 | ▶ | if ($obj->isa('Weather::OpenWeatherMap::Request::Forecast')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 8 | ▶ | $fname .= $obj->hourly ? 'H' : 'F'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | last TYPE | ||||||||||||||||||||||||||||
| 74 | 8 | } | |||||||||||||||||||||||||||
| 75 | 4 | ▶ | if ($obj->isa('Weather::OpenWeatherMap::Request::Find')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 4 | $fname .= 'S'; | |||||||||||||||||||||||||||
| 77 | last TYPE | ||||||||||||||||||||||||||||
| 78 | 4 | } | |||||||||||||||||||||||||||
| 79 | 0 | confess "Fell through; no clue what to do with $obj" | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 16 | my $location = lc $obj->location; | |||||||||||||||||||||||||||
| 83 | 16 | ▶ | my $digest = $^O eq 'Win32' ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | substr sha1_hex($location), 0, 25 | ||||||||||||||||||||||||||||
| 85 | : sha1_hex($location); | ||||||||||||||||||||||||||||
| 86 | # If you happen to alter the extension, check ->clear() too: | ||||||||||||||||||||||||||||
| 87 | 16 | $fname .= $digest . '.wx'; | |||||||||||||||||||||||||||
| 88 | 16 | path( join '/', $self->dir->absolute, $fname ) | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub cache { | ||||||||||||||||||||||||||||
| 92 | 3 | ▶ | my ($self, @results) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 3 | my $count = 0; | |||||||||||||||||||||||||||
| 94 | 3 | for my $result (@results) { | |||||||||||||||||||||||||||
| 95 | 8 | ▶ | confess "Expected a Weather::OpenWeatherMap::Result but got $result" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($result) and $result->isa("Weather::OpenWeatherMap::Result")
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Weather::OpenWeatherMap::Result")
builtin::blessed($result)
$result->isa("Weather::OpenWeatherMap::Result")
Truth table33%1/3
A and B
A= builtin::blessed($result)B= $result->isa("Weather::OpenWeatherMap::Result")
| |||||||||||||||||||||||||||||
| 96 | unless blessed($result) | ||||||||||||||||||||||||||||
| 97 | and $result->isa('Weather::OpenWeatherMap::Result'); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 8 | my $request = $result->request; | |||||||||||||||||||||||||||
| 100 | 8 | my $path = $self->make_path($request); | |||||||||||||||||||||||||||
| 101 | 8 | my $frozen = $self->serialize($result); | |||||||||||||||||||||||||||
| 102 | 7 | $path->spew_raw($frozen); | |||||||||||||||||||||||||||
| 103 | 7 | ++$count; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | $count | ||||||||||||||||||||||||||||
| 106 | 2 | } | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub is_cached { | ||||||||||||||||||||||||||||
| 109 | 4 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_cached: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 4 | my $path = $self->make_path($request); | |||||||||||||||||||||||||||
| 111 | 4 | ▶ | return unless $path->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 4 | ▶ | return if $self->expire($request); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 4 | $path | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub retrieve { | ||||||||||||||||||||||||||||
| 117 | 4 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=5 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | my $path = $self->is_cached($request); | |||||||||||||||||||||||||||
| 120 | 4 | ▶ | return unless $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | my $data = $path->slurp_raw; | |||||||||||||||||||||||||||
| 123 | my $ref = | ||||||||||||||||||||||||||||
| 124 | 4 | ▶ | try { $self->deserialize($data) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | catch { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | warn "deserialize died on retrieve: $_\n"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
| |||||||||||||||||||||||||||||
| 127 | 0 | warn "Attempting to remove possibly corrupt cachefile: $path"; | |||||||||||||||||||||||||||
| 128 | 0 | $path->remove; | |||||||||||||||||||||||||||
| 129 | undef | ||||||||||||||||||||||||||||
| 130 | 4 | }; | |||||||||||||||||||||||||||
| 131 | 4 | ▶ | return unless $ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | my ($ts, $result) = @$ref; | |||||||||||||||||||||||||||
| 134 | 4 | ▶ | unless (defined $ts && defined $result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ts and defined $result
MC/DC0%0/2
defined $ts and defined $result
defined $ts
defined $result
Truth table33%1/3
A and B
A= defined $tsB= defined $result
| |||||||||||||||||||||||||||||
| 135 | 0 | warn "cachefile incomplete, removing: $path"; | |||||||||||||||||||||||||||
| 136 | 0 | $path->remove; | |||||||||||||||||||||||||||
| 137 | return | ||||||||||||||||||||||||||||
| 138 | 0 | } | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # cached Request obj attached to Result may be stale | ||||||||||||||||||||||||||||
| 141 | # (e.g. tag may be different for new Request/Result pair) | ||||||||||||||||||||||||||||
| 142 | # FIXME tests for this are lacking | ||||||||||||||||||||||||||||
| 143 | 4 | $result->set_request($request); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | hash( | |||||||||||||||||||||||||||
| 146 | cached_at => $ts, | ||||||||||||||||||||||||||||
| 147 | object => $result | ||||||||||||||||||||||||||||
| 148 | )->inflate | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub expire { | ||||||||||||||||||||||||||||
| 152 | 4 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expire: called CC=7 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | return $self->expire_all unless defined $obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 4 | ▶ | my $path = is_Path($obj) ? $obj : $self->make_path($obj); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 4 | ▶ | return unless $path->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 4 | my $data = $path->slurp_raw; | |||||||||||||||||||||||||||
| 158 | my $ref = | ||||||||||||||||||||||||||||
| 159 | 4 | ▶ | try { $self->deserialize($data) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | catch { | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | warn "deserialize died on expiry check: $_\n"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
| |||||||||||||||||||||||||||||
| 162 | 0 | warn "Attempting to remove possibly corrupt cachefile: $path"; | |||||||||||||||||||||||||||
| 163 | 0 | $path->remove; | |||||||||||||||||||||||||||
| 164 | undef | ||||||||||||||||||||||||||||
| 165 | 4 | }; | |||||||||||||||||||||||||||
| 166 | 4 | ▶ | return unless $ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 4 | my ($ts) = @$ref; | |||||||||||||||||||||||||||
| 169 | 4 | ▶ | unless ($ts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | warn "expiring possibly corrupt cachefile: $path"; | |||||||||||||||||||||||||||
| 171 | 0 | return $path->remove | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 4 | ▶ | return $path->remove if Time::HiRes::time - $ts > $self->expiry; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | () | ||||||||||||||||||||||||||||
| 175 | 4 | } | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub cache_paths { | ||||||||||||||||||||||||||||
| 178 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_paths: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 1 | $self->dir->children( qr/^W(?:[CHFS]).+\.wx/ ) | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub expire_all { | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expire_all: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | 0 | my @expired; | |||||||||||||||||||||||||||
| 185 | 0 | POSSIBLE: for my $maybe ($self->cache_paths) { | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | push @expired, "$maybe" if $self->expire($maybe) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | @expired | ||||||||||||||||||||||||||||
| 189 | 0 | } | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub clear { | ||||||||||||||||||||||||||||
| 192 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 1 | my @removed; | |||||||||||||||||||||||||||
| 194 | 1 | POSSIBLE: for my $maybe ($self->cache_paths) { | |||||||||||||||||||||||||||
| 195 | try { | ||||||||||||||||||||||||||||
| 196 | 4 | ▶ | my $data = $maybe->slurp_raw; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 197 | 4 | my $ref = $self->deserialize($data); | |||||||||||||||||||||||||||
| 198 | 4 | my ($ts, $result) = @$ref; | |||||||||||||||||||||||||||
| 199 | 4 | ▶ | die | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&is_StrictNum($ts) and $result->isa("Weather::OpenWeatherMap::Result")
MC/DC0%0/2
&is_StrictNum($ts) and $result->isa("Weather::OpenWeatherMap::Result")
&is_StrictNum($ts)
$result->isa("Weather::OpenWeatherMap::Result")
Truth table33%1/3
A and B
A= &is_StrictNum($ts)B= $result->isa("Weather::OpenWeatherMap::Result")
| |||||||||||||||||||||||||||||
| 200 | unless is_StrictNum($ts) | ||||||||||||||||||||||||||||
| 201 | and $result->isa('Weather::OpenWeatherMap::Result') | ||||||||||||||||||||||||||||
| 202 | 4 | ▶ | } or next POSSIBLE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 4 | push @removed, "$maybe"; | |||||||||||||||||||||||||||
| 204 | # Looks like ours; remove() rather than unlink() | ||||||||||||||||||||||||||||
| 205 | # (we don't care if it exists or not, at this point) | ||||||||||||||||||||||||||||
| 206 | 4 | $maybe->remove | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | @removed | ||||||||||||||||||||||||||||
| 209 | 1 | } | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 1 | 1; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =pod | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 NAME | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Weather::OpenWeatherMap::Cache - Cache manager for OpenWeatherMap results | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # Usually used via Weather::OpenWeatherMap | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | A simple cache manager for L<Weather::OpenWeatherMap> results. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 ATTRIBUTES | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head3 dir | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | The directory cache files are saved in. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Defaults to using a temporary directory that is cleaned up during object | ||||||||||||||||||||||||||||
| 235 | destruction (via L<Path::Tiny> / L<File::Temp>). | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | If you specify a directory, no automated cleanup is done other than normal | ||||||||||||||||||||||||||||
| 238 | object expiry checks during calls to L</retrieve>. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head3 expiry | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | The duration (in seconds) cache files are considered valid; defaults to | ||||||||||||||||||||||||||||
| 243 | C<1200>. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 METHODS | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head3 High-level methods | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head4 cache | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Takes a list of L<Weather::OpenWeatherMap::Result> objects and caches to | ||||||||||||||||||||||||||||
| 252 | L</dir>. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Returns the number of items cached. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =head4 retrieve | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | Takes a L<Weather::OpenWeatherMap::Request> and attempts to retrieve a | ||||||||||||||||||||||||||||
| 259 | (non-expired) cached L<Weather::OpenWeatherMap::Result>. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Returns false if no item was found. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | If successful, the return value is a simple struct-like object with two | ||||||||||||||||||||||||||||
| 264 | attributes, B<cached_at> (the C<time()> that the cached item was saved) and | ||||||||||||||||||||||||||||
| 265 | B<object> (the relevant L<Weather::OpenWeatherMap::Result> object): | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | my $result; | ||||||||||||||||||||||||||||
| 268 | if (my $cached = $cache->retrieve($request)) { | ||||||||||||||||||||||||||||
| 269 | $result = $cached->object | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =head3 Low-level methods | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | Subclasses can override the following methods to alter cache behavior. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =head4 cache_paths | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Returns a list of L<Path::Tiny> objects representing (what appear to be) | ||||||||||||||||||||||||||||
| 279 | L<Weather::OpenWeatherMap> cache files. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head4 clear | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Walk our L</dir>, removing any items that appear to belong to the cache. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | Returns the list of removed paths (as strings). | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head4 deserialize | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Takes a scalar containing serialized cache data and returns a Perl object or | ||||||||||||||||||||||||||||
| 290 | data structure. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | Uses L<Storable> by default. | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head4 expire | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | Given a L<Weather::OpenWeatherMap::Request> or | ||||||||||||||||||||||||||||
| 297 | L<Weather::OpenWeatherMap::Result>, removes relevant stale cache data. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | If passed no arguments, calls L</expire_all>. | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Called by L</retrieve> before object retrieval. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | Returns true if a cached object was expired. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head4 expire_all | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Expires any stale cache files found in L</dir>. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head4 is_cached | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Takes a L<Weather::OpenWeatherMap::Request> or | ||||||||||||||||||||||||||||
| 312 | L<Weather::OpenWeatherMap::Result> and returns boolean true if the object is | ||||||||||||||||||||||||||||
| 313 | cached. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =head4 make_path | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | Takes a L<Weather::OpenWeatherMap::Request> or | ||||||||||||||||||||||||||||
| 318 | L<Weather::OpenWeatherMap::Result> and returns an appropriate L<Path::Tiny> | ||||||||||||||||||||||||||||
| 319 | object representing the path that would be used to cache or retrieve the | ||||||||||||||||||||||||||||
| 320 | object. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =head4 serialize | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | Takes a Perl object or data structure and returns serialized cache data | ||||||||||||||||||||||||||||
| 325 | suitable for writing to disk. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Uses L<Storable> by default. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||