|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Geocoder package. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Geocoder\Provider\Cache; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
|
16
|
|
|
use Geocoder\Query\GeocodeQuery; |
|
17
|
|
|
use Geocoder\Query\ReverseQuery; |
|
18
|
|
|
use Geocoder\Provider\Provider; |
|
19
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ProviderCache implements Provider |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Provider |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $realProvider; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CacheInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $cache; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* How log a result is going to be cached. |
|
38
|
|
|
* |
|
39
|
|
|
* @var int|null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $lifetime; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Provider $realProvider |
|
45
|
|
|
* @param CacheInterface $cache |
|
46
|
|
|
* @param int $lifetime |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
final public function __construct(Provider $realProvider, CacheInterface $cache, int $lifetime = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->realProvider = $realProvider; |
|
51
|
|
|
$this->cache = $cache; |
|
52
|
|
|
$this->lifetime = $lifetime; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
final public function geocodeQuery(GeocodeQuery $query): Collection |
|
59
|
|
|
{ |
|
60
|
|
|
$cacheKey = $this->getCacheKey($query); |
|
61
|
|
|
if (null !== $result = $this->cache->get($cacheKey)) { |
|
62
|
|
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$result = $this->realProvider->geocodeQuery($query); |
|
66
|
|
|
$this->cache->set($cacheKey, $result, $this->lifetime); |
|
67
|
|
|
|
|
68
|
|
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
final public function reverseQuery(ReverseQuery $query): Collection |
|
75
|
|
|
{ |
|
76
|
|
|
$cacheKey = $this->getCacheKey($query); |
|
77
|
|
|
if (null !== $result = $this->cache->get($cacheKey)) { |
|
78
|
|
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$result = $this->realProvider->reverseQuery($query); |
|
82
|
|
|
$this->cache->set($cacheKey, $result, $this->lifetime); |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getName(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return sprintf('%s (cache)', $this->realProvider->getName()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
final public function __call($method, $args) |
|
96
|
|
|
{ |
|
97
|
|
|
return call_user_func_array([$this->realProvider, $method], $args); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param GeocodeQuery|ReverseQuery $query |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getCacheKey($query): string |
|
106
|
|
|
{ |
|
107
|
|
|
// Include the major version number of the geocoder to avoid issues unserializing. |
|
108
|
|
|
return 'v4'.sha1((string) $query); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.