1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2019 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees; |
21
|
|
|
|
22
|
|
|
use Closure; |
23
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
24
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Wrapper around the symfony PSR6 cache library. |
28
|
|
|
*/ |
29
|
|
|
class Cache |
30
|
|
|
{ |
31
|
|
|
/** @var CacheInterface */ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cache constructor. |
36
|
|
|
* |
37
|
|
|
* @param CacheInterface $cache |
38
|
|
|
*/ |
39
|
|
|
public function __construct(CacheInterface $cache) |
40
|
|
|
{ |
41
|
|
|
$this->cache = $cache; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Fetch an item from the cache - or create it where it does not exist. |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* @param Closure $closure |
49
|
|
|
* @param int $ttl |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function remember(string $key, Closure $closure, int $ttl = PHP_INT_MAX) |
54
|
|
|
{ |
55
|
|
|
return $this->cache->get($key, static function (ItemInterface $item) use ($closure, $ttl) { |
56
|
|
|
$item->expiresAfter($ttl); |
57
|
|
|
|
58
|
|
|
return $closure(); |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Remove an item from the cache. |
64
|
|
|
* |
65
|
|
|
* @param string $key |
66
|
|
|
*/ |
67
|
|
|
public function forget(string $key): void |
68
|
|
|
{ |
69
|
|
|
$this->cache->delete($key); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|