| 1 | package Weather::OpenWeatherMap::Cache; | ||||||||||||||||||||||||||||
| 2 | 1 | $Weather::OpenWeatherMap::Cache::VERSION = '0.003001'; | |||||||||||||||||||||||||||
| 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 make_path { | ||||||||||||||||||||||||||||
| 43 | 7 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_path: called CC=10 SCAR=32.4
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 7 | ▶ | $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")
| |||||||||||||||||||||||||||||
| 46 | if blessed($obj) | ||||||||||||||||||||||||||||
| 47 | and $obj->isa('Weather::OpenWeatherMap::Result'); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 7 | ▶ | 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")
| |||||||||||||||||||||||||||||
| 50 | unless blessed($obj) and $obj->isa('Weather::OpenWeatherMap::Request'); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7 | my $fname = 'W'; | |||||||||||||||||||||||||||
| 53 | TYPE: { | ||||||||||||||||||||||||||||
| 54 | 7 | ▶ | if ($obj->isa('Weather::OpenWeatherMap::Request::Current')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 7 | $fname .= 'C'; | |||||||||||||||||||||||||||
| 56 | last TYPE | ||||||||||||||||||||||||||||
| 57 | 7 | } | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($obj->isa('Weather::OpenWeatherMap::Request::Forecast')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $fname .= $obj->hourly ? 'H' : 'F'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | last TYPE | ||||||||||||||||||||||||||||
| 61 | 0 | } | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($obj->isa('Weather::OpenWeatherMap::Request::Find')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $fname .= 'S'; | |||||||||||||||||||||||||||
| 64 | last TYPE | ||||||||||||||||||||||||||||
| 65 | 0 | } | |||||||||||||||||||||||||||
| 66 | 0 | confess "Fell through; no clue what to do with $obj" | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | my $location = lc $obj->location; | |||||||||||||||||||||||||||
| 70 | 7 | ▶ | my $digest = $^O eq 'Win32' ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | substr sha1_hex($location), 0, 25 | ||||||||||||||||||||||||||||
| 72 | : sha1_hex($location); | ||||||||||||||||||||||||||||
| 73 | # If you happen to alter the extension, check ->clear() too: | ||||||||||||||||||||||||||||
| 74 | 7 | $fname .= $digest . '.wx'; | |||||||||||||||||||||||||||
| 75 | 7 | path( join '/', $self->dir->absolute, $fname ) | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub serialize { | ||||||||||||||||||||||||||||
| 80 | 2 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 2 | Storable::freeze( | |||||||||||||||||||||||||||
| 82 | [ Time::HiRes::time, $obj ] | ||||||||||||||||||||||||||||
| 83 | ) | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub cache { | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | my ($self, @results) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 2 | for my $result (@results) { | |||||||||||||||||||||||||||
| 89 | 2 | ▶ | 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")
| |||||||||||||||||||||||||||||
| 90 | unless blessed($result) | ||||||||||||||||||||||||||||
| 91 | and $result->isa('Weather::OpenWeatherMap::Result'); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | my $request = $result->request; | |||||||||||||||||||||||||||
| 94 | 2 | my $path = $self->make_path($request); | |||||||||||||||||||||||||||
| 95 | 2 | my $frozen = $self->serialize($result); | |||||||||||||||||||||||||||
| 96 | 2 | $path->spew_raw($frozen); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 1 | ||||||||||||||||||||||||||||
| 99 | 2 | } | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub is_cached { | ||||||||||||||||||||||||||||
| 102 | 3 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_cached: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 3 | my $path = $self->make_path($obj); | |||||||||||||||||||||||||||
| 104 | 3 | ▶ | return unless $path->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | return if $self->expire($obj); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 2 | $path | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub deserialize { | ||||||||||||||||||||||||||||
| 110 | 3 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 3 | Storable::thaw($data) | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub retrieve { | ||||||||||||||||||||||||||||
| 115 | 2 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 2 | my $path = $self->is_cached($obj); | |||||||||||||||||||||||||||
| 117 | 2 | ▶ | return unless $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 1 | my $data = $path->slurp_raw; | |||||||||||||||||||||||||||
| 119 | 1 | my $ref = $self->deserialize($data); | |||||||||||||||||||||||||||
| 120 | 1 | my ($ts, $result) = @$ref; | |||||||||||||||||||||||||||
| 121 | 1 | hash( | |||||||||||||||||||||||||||
| 122 | cached_at => $ts, | ||||||||||||||||||||||||||||
| 123 | object => $result | ||||||||||||||||||||||||||||
| 124 | )->inflate | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub expire { | ||||||||||||||||||||||||||||
| 128 | 2 | ▶ | my ($self, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expire: called CC=5 SCAR=17.4
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | return $self->expire_all unless defined $obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | my $path = is_Path($obj) ? $obj : $self->make_path($obj); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | return unless $path->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | my $data = $path->slurp_raw; | |||||||||||||||||||||||||||
| 134 | my $ref = | ||||||||||||||||||||||||||||
| 135 | 2 | ▶ | try { $self->deserialize($data) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 136 | catch { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | warn "Attempting to remove possibly corrupt cachefile: $path"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | 0 | $path->remove | |||||||||||||||||||||||||||
| 139 | 2 | }; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 2 | my ($ts) = @$ref; | |||||||||||||||||||||||||||
| 142 | 2 | ▶ | return $path->remove if Time::HiRes::time - $ts > $self->expiry; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | () | ||||||||||||||||||||||||||||
| 144 | 2 | } | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub cache_paths { | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_paths: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 0 | $self->dir->children( qr/^W(?:C|F).+\.wx/ ) | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub expire_all { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expire_all: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 0 | my @expired; | |||||||||||||||||||||||||||
| 154 | 0 | POSSIBLE: for my $maybe ($self->cache_paths) { | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | push @expired, "$maybe" if $self->expire($maybe) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | @expired | ||||||||||||||||||||||||||||
| 158 | 0 | } | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub clear { | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 0 | my @removed; | |||||||||||||||||||||||||||
| 163 | 0 | POSSIBLE: for my $maybe ($self->cache_paths) { | |||||||||||||||||||||||||||
| 164 | try { | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my $data = $maybe->slurp_raw; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 166 | 0 | my $ref = $self->deserialize($data); | |||||||||||||||||||||||||||
| 167 | 0 | my ($ts, $result) = @$ref; | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | die | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/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 table0%0/3
A and B
A= &is_StrictNum($ts)B= $result->isa("Weather::OpenWeatherMap::Result")
| |||||||||||||||||||||||||||||
| 169 | unless is_StrictNum($ts) | ||||||||||||||||||||||||||||
| 170 | and $result->isa('Weather::OpenWeatherMap::Result') | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | } or next POSSIBLE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | push @removed, "$maybe"; | |||||||||||||||||||||||||||
| 173 | # Looks like ours; remove() rather than unlink() | ||||||||||||||||||||||||||||
| 174 | # (we don't care if it exists or not, at this point) | ||||||||||||||||||||||||||||
| 175 | 0 | $maybe->remove | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | @removed | ||||||||||||||||||||||||||||
| 178 | 0 | } | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | 1; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =pod | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 NAME | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Weather::OpenWeatherMap::Cache - Cache manager for OpenWeatherMap results | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Usually used via Weather::OpenWeatherMap | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | A simple cache manager for L<Weather::OpenWeatherMap> results. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 ATTRIBUTES | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head3 dir | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | The directory cache files are saved in. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Defaults to using a temporary directory that is cleaned up during object | ||||||||||||||||||||||||||||
| 204 | destruction (via L<Path::Tiny> / L<File::Temp>). | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | If you specify a directory, no automated cleanup is done other than normal | ||||||||||||||||||||||||||||
| 207 | object expiry checks during calls to L</retrieve>. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head3 expiry | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | The duration (in seconds) cache files are considered valid; defaults to | ||||||||||||||||||||||||||||
| 212 | C<1200>. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head2 METHODS | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head3 High-level methods | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head4 cache | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Takes a list of L<Weather::OpenWeatherMap::Result> objects and caches to | ||||||||||||||||||||||||||||
| 221 | L</dir>. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head4 retrieve | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Takes a L<Weather::OpenWeatherMap::Request> and attempts to retrieve a | ||||||||||||||||||||||||||||
| 226 | (non-expired) cached result. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Returns false if no item was found. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | If successful, the return value is a simple struct-like object with two | ||||||||||||||||||||||||||||
| 231 | attributes, B<cached_at> (the C<time()> that the cached item was saved) and | ||||||||||||||||||||||||||||
| 232 | B<object> (the relevant L<Weather::OpenWeatherMap::Result> object): | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | my $result; | ||||||||||||||||||||||||||||
| 235 | if (my $cached = $cache->retrieve($request)) { | ||||||||||||||||||||||||||||
| 236 | $result = $cached->object | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head3 Low-level methods | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Subclasses can override the following methods to alter cache behavior. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =head4 cache_paths | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | Returns a list of L<Path::Tiny> objects representing (what appear to be) | ||||||||||||||||||||||||||||
| 246 | L<Weather::OpenWeatherMap> cache files. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head4 clear | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Walk our L</dir>, removing any items that appear to belong to the cache. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Returns the list of removed paths (as strings). | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head4 deserialize | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Takes a scalar containing serialized cache data and returns a Perl object or | ||||||||||||||||||||||||||||
| 257 | data structure. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Uses L<Storable> by default. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =head4 expire | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Given a L<Weather::OpenWeatherMap::Request> or | ||||||||||||||||||||||||||||
| 264 | L<Weather::OpenWeatherMap::Result>, removes relevant stale cache data. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | If passed no arguments, calls L</expire_all>. | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | Called by L</retrieve> before object retrieval. | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Returns true if a cached object was expired. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =head4 expire_all | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | Expires any stale cache files found in L</dir>. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =head4 is_cached | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Takes a L<Weather::OpenWeatherMap::Request> or | ||||||||||||||||||||||||||||
| 279 | L<Weather::OpenWeatherMap::Result> and returns boolean true if the object is | ||||||||||||||||||||||||||||
| 280 | cached. | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head4 make_path | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Takes a L<Weather::OpenWeatherMap::Request> or | ||||||||||||||||||||||||||||
| 285 | L<Weather::OpenWeatherMap::Result> and returns an appropriate L<Path::Tiny> | ||||||||||||||||||||||||||||
| 286 | object representing the path that would be used to cache or retrieve the | ||||||||||||||||||||||||||||
| 287 | object. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head4 serialize | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | Takes a Perl object or data structure and returns serialized cache data | ||||||||||||||||||||||||||||
| 292 | suitable for writing to disk. | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Uses L<Storable> by default. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =cut | ||||||||||||||||||||||||||||