Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/cache/PhutilAPCKeyValueCache.php
12241 views
1
<?php
2
3
/**
4
* Interface to the APC key-value cache. This is a very high-performance cache
5
* which is local to the current machine.
6
*/
7
final class PhutilAPCKeyValueCache extends PhutilKeyValueCache {
8
9
10
/* -( Key-Value Cache Implementation )------------------------------------- */
11
12
13
public function isAvailable() {
14
return (function_exists('apc_fetch') || function_exists('apcu_fetch')) &&
15
ini_get('apc.enabled') &&
16
(ini_get('apc.enable_cli') || php_sapi_name() != 'cli');
17
}
18
19
public function getKeys(array $keys, $ttl = null) {
20
static $is_apcu;
21
if ($is_apcu === null) {
22
$is_apcu = self::isAPCu();
23
}
24
25
$results = array();
26
$fetched = false;
27
foreach ($keys as $key) {
28
if ($is_apcu) {
29
$result = apcu_fetch($key, $fetched);
30
} else {
31
$result = apc_fetch($key, $fetched);
32
}
33
34
if ($fetched) {
35
$results[$key] = $result;
36
}
37
}
38
return $results;
39
}
40
41
public function setKeys(array $keys, $ttl = null) {
42
static $is_apcu;
43
if ($is_apcu === null) {
44
$is_apcu = self::isAPCu();
45
}
46
47
// NOTE: Although modern APC supports passing an array to `apc_store()`,
48
// it is not supported by older version of APC or by HPHP.
49
50
// See T13525 for discussion of use of "@" to silence this warning:
51
// > GC cache entry "<some-key-name>" was on gc-list for <X> seconds
52
53
foreach ($keys as $key => $value) {
54
if ($is_apcu) {
55
@apcu_store($key, $value, $ttl);
56
} else {
57
@apc_store($key, $value, $ttl);
58
}
59
}
60
61
return $this;
62
}
63
64
public function deleteKeys(array $keys) {
65
static $is_apcu;
66
if ($is_apcu === null) {
67
$is_apcu = self::isAPCu();
68
}
69
70
foreach ($keys as $key) {
71
if ($is_apcu) {
72
apcu_delete($key);
73
} else {
74
apc_delete($key);
75
}
76
}
77
78
return $this;
79
}
80
81
public function destroyCache() {
82
static $is_apcu;
83
if ($is_apcu === null) {
84
$is_apcu = self::isAPCu();
85
}
86
87
if ($is_apcu) {
88
apcu_clear_cache();
89
} else {
90
apc_clear_cache('user');
91
}
92
93
return $this;
94
}
95
96
private static function isAPCu() {
97
return function_exists('apcu_fetch');
98
}
99
100
}
101
102