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\Provider\Provider; |
||
17 | use Geocoder\Query\GeocodeQuery; |
||
18 | use Geocoder\Query\ReverseQuery; |
||
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 long a result is going to be cached. |
||
38 | * |
||
39 | * @var int|null |
||
40 | */ |
||
41 | protected $lifetime; |
||
42 | |||
43 | /** |
||
44 | * If true, include the real provider name into the cache key. |
||
45 | */ |
||
46 | private bool $separateCache; |
||
47 | |||
48 | /** |
||
49 | * @param int $lifetime |
||
50 | */ |
||
51 | 6 | final public function __construct(Provider $realProvider, CacheInterface $cache, int $lifetime = null, bool $separateCache = false) |
|
52 | { |
||
53 | 6 | $this->realProvider = $realProvider; |
|
54 | 6 | $this->cache = $cache; |
|
55 | 6 | $this->lifetime = $lifetime; |
|
56 | 6 | $this->separateCache = $separateCache; |
|
57 | } |
||
58 | |||
59 | 2 | final public function geocodeQuery(GeocodeQuery $query): Collection |
|
60 | { |
||
61 | 2 | $cacheKey = $this->getCacheKey($query); |
|
62 | 2 | if (null !== $result = $this->cache->get($cacheKey)) { |
|
63 | 1 | return $result; |
|
64 | } |
||
65 | |||
66 | 1 | $result = $this->realProvider->geocodeQuery($query); |
|
67 | 1 | $this->cache->set($cacheKey, $result, $this->lifetime); |
|
68 | |||
69 | 1 | return $result; |
|
70 | } |
||
71 | |||
72 | 2 | final public function reverseQuery(ReverseQuery $query): Collection |
|
73 | { |
||
74 | 2 | $cacheKey = $this->getCacheKey($query); |
|
75 | 2 | if (null !== $result = $this->cache->get($cacheKey)) { |
|
76 | 1 | return $result; |
|
77 | } |
||
78 | |||
79 | 1 | $result = $this->realProvider->reverseQuery($query); |
|
80 | 1 | $this->cache->set($cacheKey, $result, $this->lifetime); |
|
81 | |||
82 | 1 | return $result; |
|
83 | } |
||
84 | |||
85 | 1 | public function getName(): string |
|
86 | { |
||
87 | 1 | return sprintf('%s (cache)', $this->realProvider->getName()); |
|
88 | } |
||
89 | |||
90 | final public function __call($method, $args) |
||
91 | { |
||
92 | return call_user_func_array([$this->realProvider, $method], $args); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param GeocodeQuery|ReverseQuery $query |
||
97 | */ |
||
98 | 5 | protected function getCacheKey($query): string |
|
99 | { |
||
100 | // Include the major version number of the geocoder to avoid issues unserializing |
||
101 | // and real provider name if we want to separate cache |
||
102 | 5 | return 'v4'.sha1((string) $query.($this->separateCache ? $this->realProvider->getName() : '')); |
|
103 | } |
||
104 | } |
||
105 |