1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cache\Handlers; |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Moteur de stockage Wincache pour le cache |
19
|
|
|
* |
20
|
|
|
* Prend en charge Wincache 1.1.0 et supérieur. |
21
|
|
|
*/ |
22
|
|
|
class Wincache extends BaseHandler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Contient les noms de groupe compilés |
26
|
|
|
* (préfixé par le préfixe de configuration global) |
27
|
|
|
*/ |
28
|
|
|
protected array $_compiledGroupNames = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public function init(array $config = []): bool |
34
|
|
|
{ |
35
|
|
|
if (! extension_loaded('wincache')) { |
36
|
|
|
throw new RuntimeException('L\'extension `wincache` doit être activée pour utiliser WincacheHandler.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
parent::init($config); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
|
|
public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool |
48
|
|
|
{ |
49
|
|
|
$key = $this->_key($key); |
50
|
|
|
$duration = $this->duration($ttl); |
51
|
|
|
|
52
|
|
|
return wincache_ucache_set($key, $value, $duration); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function get(string $key, mixed $default = null): mixed |
59
|
|
|
{ |
60
|
|
|
$value = wincache_ucache_get($this->_key($key), $success); |
61
|
|
|
if ($success === false) { |
62
|
|
|
return $default; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function increment(string $key, int $offset = 1) |
72
|
|
|
{ |
73
|
|
|
$key = $this->_key($key); |
74
|
|
|
|
75
|
|
|
return wincache_ucache_inc($key, $offset); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function decrement(string $key, int $offset = 1) |
82
|
|
|
{ |
83
|
|
|
$key = $this->_key($key); |
84
|
|
|
|
85
|
|
|
return wincache_ucache_dec($key, $offset); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function delete(string $key): bool |
92
|
|
|
{ |
93
|
|
|
$key = $this->_key($key); |
94
|
|
|
|
95
|
|
|
return wincache_ucache_delete($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function clear(): bool |
102
|
|
|
{ |
103
|
|
|
$info = wincache_ucache_info(); |
104
|
|
|
$cacheKeys = $info['ucache_entries']; |
105
|
|
|
unset($info); |
106
|
|
|
|
107
|
|
|
foreach ($cacheKeys as $key) { |
108
|
|
|
if (strpos($key['key_name'], $this->_config['prefix']) === 0) { |
109
|
|
|
wincache_ucache_delete($key['key_name']); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function info() |
120
|
|
|
{ |
121
|
|
|
return wincache_ucache_info(true); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function groups(): array |
128
|
|
|
{ |
129
|
|
|
if (empty($this->_compiledGroupNames)) { |
130
|
|
|
foreach ($this->_config['groups'] as $group) { |
131
|
|
|
$this->_compiledGroupNames[] = $this->_config['prefix'] . $group; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$groups = wincache_ucache_get($this->_compiledGroupNames); |
136
|
|
|
if (count($groups) !== count($this->_config['groups'])) { |
137
|
|
|
foreach ($this->_compiledGroupNames as $group) { |
138
|
|
|
if (! isset($groups[$group])) { |
139
|
|
|
wincache_ucache_set($group, 1); |
140
|
|
|
$groups[$group] = 1; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
ksort($groups); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$result = []; |
147
|
|
|
$groups = array_values($groups); |
148
|
|
|
|
149
|
|
|
foreach ($this->_config['groups'] as $i => $group) { |
150
|
|
|
$result[] = $group . $groups[$i]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
*/ |
159
|
|
|
public function clearGroup(string $group): bool |
160
|
|
|
{ |
161
|
|
|
$success = false; |
162
|
|
|
wincache_ucache_inc($this->_config['prefix'] . $group, 1, $success); |
163
|
|
|
|
164
|
|
|
return $success; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|