forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorKeyValueSerializingCacheProxy.php
55 lines (45 loc) · 1.38 KB
/
PhabricatorKeyValueSerializingCacheProxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Proxies another cache and serializes values.
*
* This allows more complex data to be stored in a cache which can only store
* strings.
*/
final class PhabricatorKeyValueSerializingCacheProxy
extends PhutilKeyValueCacheProxy {
public function getKeys(array $keys) {
$results = parent::getKeys($keys);
$reads = array();
foreach ($results as $key => $result) {
$structure = @unserialize($result);
// The unserialize() function returns false when unserializing a
// literal `false`, and also when it fails. If we get a literal
// `false`, test if the serialized form is the same as the
// serialization of `false` and miss the cache otherwise.
if ($structure === false) {
static $serialized_false;
if ($serialized_false === null) {
$serialized_false = serialize(false);
}
if ($result !== $serialized_false) {
continue;
}
}
$reads[$key] = $structure;
}
return $reads;
}
public function setKeys(array $keys, $ttl = null) {
$writes = array();
foreach ($keys as $key => $value) {
if (is_object($value)) {
throw new Exception(
pht(
'Serializing cache can not write objects (for key "%s")!',
$key));
}
$writes[$key] = serialize($value);
}
return parent::setKeys($writes, $ttl);
}
}