|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* webtrees: online genealogy |
|
5
|
|
|
* Copyright (C) 2021 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 <https://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\ItemInterface; |
|
24
|
|
|
use Symfony\Contracts\Cache\TagAwareCacheInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Wrapper around the symfony PSR6 cache library. |
|
28
|
|
|
* Hash the keys to protect against characters that are not allowed in PSR6. |
|
29
|
|
|
*/ |
|
30
|
|
|
class Cache |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var TagAwareCacheInterface */ |
|
33
|
|
|
private $cache; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Cache constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param TagAwareCacheInterface $cache |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(TagAwareCacheInterface $cache) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->cache = $cache; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Generate a key compatible with PSR-6 requirements |
|
47
|
|
|
* @see https://www.php-fig.org/psr/psr-6/ |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function safeKey(string $key): string |
|
53
|
|
|
{ |
|
54
|
|
|
return md5($key); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Fetch an item from the cache - or create it where it does not exist. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $key |
|
61
|
|
|
* @param Closure $closure |
|
62
|
|
|
* @param string[] $tags |
|
63
|
|
|
* @param int|null $ttl |
|
64
|
|
|
* |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
public function remember(string $key, Closure $closure, array $tags = [], int $ttl = null) |
|
68
|
|
|
{ |
|
69
|
|
|
$tags = array_map([$this, 'safeKey'], $tags); |
|
70
|
|
|
return $this->cache->get( |
|
71
|
|
|
$this->safeKey($key), |
|
72
|
|
|
static function (ItemInterface $item) use ($closure, $tags, $ttl) { |
|
73
|
|
|
$item->expiresAfter($ttl); |
|
74
|
|
|
$item->tag($tags); |
|
75
|
|
|
|
|
76
|
|
|
return $closure(); |
|
77
|
|
|
} |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Invalidate cache items based on tags. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string[] $tags |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function invalidateTags(array $tags): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->cache->invalidateTags(array_map([$this, 'safeKey'], $tags)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Remove an item from the cache. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
*/ |
|
97
|
|
|
public function forget(string $key): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->cache->delete($this->safeKey($key)); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|