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