mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-10 08:52:39 +01:00
Introduce a serializing key-value cache proxy
Summary: Ref T11954. I want to store some lists/arrays in the mutable (database) cache, but it only supports string storage. Provide a serializing wrapper which flattens when values are written and expands them when they're read. Test Plan: Used by D16997. See that revision. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11954 Differential Revision: https://secure.phabricator.com/D16999
This commit is contained in:
parent
f8d6b6181e
commit
125fb332de
3 changed files with 75 additions and 0 deletions
|
@ -2813,6 +2813,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorJiraIssueHasObjectEdgeType' => 'applications/doorkeeper/edge/PhabricatorJiraIssueHasObjectEdgeType.php',
|
||||
'PhabricatorJumpNavHandler' => 'applications/search/engine/PhabricatorJumpNavHandler.php',
|
||||
'PhabricatorKeyValueDatabaseCache' => 'applications/cache/PhabricatorKeyValueDatabaseCache.php',
|
||||
'PhabricatorKeyValueSerializingCacheProxy' => 'applications/cache/PhabricatorKeyValueSerializingCacheProxy.php',
|
||||
'PhabricatorKeyboardRemarkupRule' => 'infrastructure/markup/rule/PhabricatorKeyboardRemarkupRule.php',
|
||||
'PhabricatorKeyring' => 'applications/files/keyring/PhabricatorKeyring.php',
|
||||
'PhabricatorKeyringConfigOptionType' => 'applications/files/keyring/PhabricatorKeyringConfigOptionType.php',
|
||||
|
@ -7798,6 +7799,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorJiraIssueHasObjectEdgeType' => 'PhabricatorEdgeType',
|
||||
'PhabricatorJumpNavHandler' => 'Phobject',
|
||||
'PhabricatorKeyValueDatabaseCache' => 'PhutilKeyValueCache',
|
||||
'PhabricatorKeyValueSerializingCacheProxy' => 'PhutilKeyValueCacheProxy',
|
||||
'PhabricatorKeyboardRemarkupRule' => 'PhutilRemarkupRule',
|
||||
'PhabricatorKeyring' => 'Phobject',
|
||||
'PhabricatorKeyringConfigOptionType' => 'PhabricatorConfigJSONOptionType',
|
||||
|
|
18
src/applications/cache/PhabricatorCaches.php
vendored
18
src/applications/cache/PhabricatorCaches.php
vendored
|
@ -116,6 +116,24 @@ final class PhabricatorCaches extends Phobject {
|
|||
return $caches;
|
||||
}
|
||||
|
||||
public static function getMutableStructureCache() {
|
||||
static $cache;
|
||||
if (!$cache) {
|
||||
$caches = self::buildMutableStructureCaches();
|
||||
$cache = self::newStackFromCaches($caches);
|
||||
}
|
||||
return $cache;
|
||||
}
|
||||
|
||||
private static function buildMutableStructureCaches() {
|
||||
$caches = array();
|
||||
|
||||
$cache = new PhabricatorKeyValueDatabaseCache();
|
||||
$cache = new PhabricatorKeyValueSerializingCacheProxy($cache);
|
||||
$caches[] = $cache;
|
||||
|
||||
return $caches;
|
||||
}
|
||||
|
||||
/* -( Runtime Cache )------------------------------------------------------ */
|
||||
|
||||
|
|
55
src/applications/cache/PhabricatorKeyValueSerializingCacheProxy.php
vendored
Normal file
55
src/applications/cache/PhabricatorKeyValueSerializingCacheProxy.php
vendored
Normal file
|
@ -0,0 +1,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);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue