|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Countries\Package\Services\Cache\Managers; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
8
|
|
|
use Nette\Caching\Cache as NetteCache; |
|
9
|
|
|
use Nette\Caching\Storages\FileStorage; |
|
10
|
|
|
use PragmaRX\Countries\Package\Services\Config; |
|
11
|
|
|
|
|
12
|
|
|
class Nette implements CacheInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Cache. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \Nette\Caching\Cache |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $cache; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Config. |
|
23
|
|
|
* |
|
24
|
|
|
* @var Config |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Cache directory. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $dir; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Cache constructor. |
|
37
|
|
|
* @param object $config |
|
38
|
|
|
* @param null $path |
|
39
|
|
|
*/ |
|
40
|
31 |
|
public function __construct($config = null, $path = null) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
31 |
|
$this->config = is_null($config) ? new Config() : $config; |
|
43
|
|
|
|
|
44
|
31 |
|
$this->cache = new NetteCache( |
|
45
|
31 |
|
$this->getStorage($path = null) |
|
46
|
|
|
); |
|
47
|
31 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Check if cache is enabled. |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function enabled() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->config->get('countries.cache.enabled'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the cache directory. |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed|string|static |
|
63
|
|
|
*/ |
|
64
|
31 |
|
public function getCacheDir() |
|
65
|
|
|
{ |
|
66
|
31 |
|
if (is_null($this->dir)) { |
|
67
|
31 |
|
$this->dir = $this->config->cache->directory ?: sys_get_temp_dir().'/__PRAGMARX_COUNTRIES__/cache'; |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
31 |
|
if (! file_exists($this->dir)) { |
|
70
|
1 |
|
mkdir($this->dir, 0755, true); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
31 |
|
return $this->dir; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the file storage. |
|
79
|
|
|
* |
|
80
|
|
|
* @param null $path |
|
81
|
|
|
* @return FileStorage |
|
82
|
|
|
*/ |
|
83
|
31 |
|
public function getStorage($path = null) |
|
84
|
|
|
{ |
|
85
|
31 |
|
return new FileStorage( |
|
86
|
31 |
|
is_null($path) |
|
87
|
31 |
|
? $this->getCacheDir() |
|
88
|
31 |
|
: $path |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Fetches a value from the cache. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
* @param null $default |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
public function get($key, $default = null) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->enabled()) { |
|
102
|
|
|
return $this->cache->load($key, $default); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param $ttl |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function makeExpiration($ttl) |
|
111
|
|
|
{ |
|
112
|
|
|
$expiration = ($ttl ?: $this->config->get('cache.duration')).' minutes'; |
|
113
|
|
|
|
|
114
|
|
|
return $expiration; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $key |
|
121
|
|
|
* @param mixed $value |
|
122
|
|
|
* @param null $ttl |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public function set($key, $value, $ttl = null) |
|
126
|
|
|
{ |
|
127
|
|
|
if ($this->enabled()) { |
|
128
|
|
|
return $this->cache->save($key, $value, [NetteCache::EXPIRE => $this->makeExpiration($ttl)]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $value; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Delete an item from the cache by its unique key. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $key |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
public function delete($key) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->cache->remove($key); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Wipe clean the entire cache's keys. |
|
147
|
|
|
*/ |
|
148
|
31 |
|
public function clear() |
|
149
|
|
|
{ |
|
150
|
31 |
|
$this->cache->clean([NetteCache::ALL => true]); |
|
151
|
31 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Obtains multiple cache items by their unique keys. |
|
155
|
|
|
* |
|
156
|
|
|
* @param $keys |
|
157
|
|
|
* @param null $default |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getMultiple($keys, $default = null) |
|
161
|
|
|
{ |
|
162
|
|
|
return coollect($keys)->map(function ($key) { |
|
163
|
|
|
return $this->get($key); |
|
164
|
|
|
}); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
|
169
|
|
|
* |
|
170
|
|
|
* @param $values |
|
171
|
|
|
* @param null $ttl |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setMultiple($values, $ttl = null) |
|
175
|
|
|
{ |
|
176
|
|
|
return coollect($values)->map(function ($value, $key) use ($ttl) { |
|
177
|
|
|
return $this->set($key, $value, $ttl); |
|
178
|
|
|
}); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Deletes multiple cache items in a single operation. |
|
183
|
|
|
* |
|
184
|
|
|
* @param $keys |
|
185
|
|
|
* @return bool|void |
|
186
|
|
|
*/ |
|
187
|
|
|
public function deleteMultiple($keys) |
|
188
|
|
|
{ |
|
189
|
|
|
coollect($keys)->map(function ($key) { |
|
190
|
|
|
$this->forget($key); |
|
|
|
|
|
|
191
|
|
|
}); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Determines whether an item is present in the cache. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $key |
|
198
|
|
|
* @return bool |
|
199
|
|
|
*/ |
|
200
|
|
|
public function has($key) |
|
201
|
|
|
{ |
|
202
|
|
|
return ! is_null($this->get($key)); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get an item from the cache, or store the default value. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $key |
|
209
|
|
|
* @param \DateTimeInterface|\DateInterval|float|int $minutes |
|
210
|
|
|
* @param Closure $callback |
|
211
|
|
|
* @return mixed |
|
212
|
|
|
*/ |
|
213
|
|
View Code Duplication |
public function remember($key, $minutes, Closure $callback) |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
$value = $this->get($key); |
|
216
|
|
|
|
|
217
|
|
|
if (! is_null($value)) { |
|
218
|
|
|
return $value; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$this->set($key, $value = $callback(), $minutes); |
|
222
|
|
|
|
|
223
|
|
|
return $value; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.