1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use function apcu_cache_info; |
7
|
|
|
use function apcu_clear_cache; |
8
|
|
|
use function apcu_delete; |
9
|
|
|
use function apcu_entry; |
10
|
|
|
use function apcu_exists; |
11
|
|
|
use function apcu_fetch; |
12
|
|
|
use function apcu_sma_info; |
13
|
|
|
use function apcu_store; |
14
|
|
|
use function count; |
15
|
|
|
use function function_exists; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* APCu cache provider. |
19
|
|
|
* |
20
|
|
|
* @link www.doctrine-project.org |
21
|
|
|
*/ |
22
|
|
|
class ApcuCache extends CacheProvider |
23
|
|
|
{ |
24
|
75 |
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
75 |
|
*/ |
27
|
|
|
protected function doFetch($id) |
28
|
|
|
{ |
29
|
|
|
return apcu_fetch($id); |
30
|
|
|
} |
31
|
|
|
|
32
|
70 |
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
70 |
|
*/ |
35
|
|
|
protected function doContains($id) |
36
|
|
|
{ |
37
|
|
|
return apcu_exists($id); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
73 |
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
73 |
|
*/ |
43
|
|
|
protected function doSave($id, $data, $lifeTime = 0) |
44
|
|
|
{ |
45
|
|
|
return apcu_store($id, $data, $lifeTime); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
44 |
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
44 |
|
protected function doDelete($id) |
52
|
|
|
{ |
53
|
|
|
// apcu_delete returns false if the id does not exist |
54
|
|
|
return apcu_delete($id) || ! apcu_exists($id); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
1 |
|
*/ |
60
|
|
|
protected function doDeleteMultiple(array $keys) |
61
|
1 |
|
{ |
62
|
|
|
$result = apcu_delete($keys); |
63
|
|
|
|
64
|
|
|
return $result !== false && count($result) !== count($keys); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
2 |
|
*/ |
70
|
|
|
protected function doFlush() |
71
|
|
|
{ |
72
|
|
|
return apcu_clear_cache(); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
2 |
|
*/ |
78
|
|
|
protected function doFetchAtomic(string $id, callable $generator, int $ttl) |
79
|
|
|
{ |
80
|
|
|
if (! function_exists('apcu_entry')) { |
81
|
|
|
throw new RuntimeException('Atomic fetch (apcu_entry) is not supported by this version of apcu'); |
82
|
|
|
} |
83
|
1 |
|
|
84
|
|
|
return apcu_entry($id, $generator, $ttl); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
protected function doFetchMultiple(array $keys) |
91
|
|
|
{ |
92
|
|
|
return apcu_fetch($keys) ?: []; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
/** |
96
|
1 |
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
99
|
1 |
|
{ |
100
|
1 |
|
$result = apcu_store($keysAndValues, null, $lifetime); |
101
|
1 |
|
|
102
|
1 |
|
return empty($result); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
protected function doGetStats() |
109
|
|
|
{ |
110
|
|
|
$info = apcu_cache_info(true); |
111
|
|
|
$sma = apcu_sma_info(); |
112
|
|
|
|
113
|
|
|
return [ |
114
|
|
|
Cache::STATS_HITS => $info['num_hits'], |
115
|
|
|
Cache::STATS_MISSES => $info['num_misses'], |
116
|
|
|
Cache::STATS_UPTIME => $info['start_time'], |
117
|
|
|
Cache::STATS_MEMORY_USAGE => $info['mem_size'], |
118
|
|
|
Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|