forked from GNUsocial/gnu-social
Stop caching unfindable keys
There were some problems with the automated cache/uncache system for data objects that made us cache unfindable keys (with null attributes and sometimes null names). Fixed those problems and refactored the encache() and decache() methods so they use a helper to find the cache keys to use.
This commit is contained in:
parent
8f362e9956
commit
5a1ea0b9b2
@ -152,52 +152,69 @@ class Memcached_DataObject extends DB_DataObject
|
|||||||
function encache()
|
function encache()
|
||||||
{
|
{
|
||||||
$c = $this->memcache();
|
$c = $this->memcache();
|
||||||
|
|
||||||
if (!$c) {
|
if (!$c) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
}
|
||||||
$pkey = array();
|
|
||||||
$pval = array();
|
$keys = $this->_allCacheKeys();
|
||||||
$types = $this->keyTypes();
|
|
||||||
ksort($types);
|
foreach ($keys as $key) {
|
||||||
foreach ($types as $key => $type) {
|
$c->set($key, $this);
|
||||||
if ($type == 'K') {
|
|
||||||
$pkey[] = $key;
|
|
||||||
$pval[] = $this->$key;
|
|
||||||
} else {
|
|
||||||
$c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# XXX: should work for both compound and scalar pkeys
|
|
||||||
$pvals = implode(',', $pval);
|
|
||||||
$pkeys = implode(',', $pkey);
|
|
||||||
$c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function decache()
|
function decache()
|
||||||
{
|
{
|
||||||
$c = $this->memcache();
|
$c = $this->memcache();
|
||||||
|
|
||||||
if (!$c) {
|
if (!$c) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
$pkey = array();
|
|
||||||
$pval = array();
|
|
||||||
$types = $this->keyTypes();
|
|
||||||
ksort($types);
|
|
||||||
foreach ($types as $key => $type) {
|
|
||||||
if ($type == 'K') {
|
|
||||||
$pkey[] = $key;
|
|
||||||
$pval[] = $this->$key;
|
|
||||||
} else {
|
|
||||||
$c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# should work for both compound and scalar pkeys
|
|
||||||
# XXX: comma works for now but may not be safe separator for future keys
|
|
||||||
$pvals = implode(',', $pval);
|
|
||||||
$pkeys = implode(',', $pkey);
|
|
||||||
$c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$keys = $this->_allCacheKeys();
|
||||||
|
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$c->delete($key, $this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _allCacheKeys()
|
||||||
|
{
|
||||||
|
$ckeys = array();
|
||||||
|
|
||||||
|
$types = $this->keyTypes();
|
||||||
|
ksort($types);
|
||||||
|
|
||||||
|
$pkey = array();
|
||||||
|
$pval = array();
|
||||||
|
|
||||||
|
foreach ($types as $key => $type) {
|
||||||
|
|
||||||
|
assert(!empty($key));
|
||||||
|
|
||||||
|
if ($type == 'U') {
|
||||||
|
if (empty($this->$key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$ckeys[] = $this->cacheKey($this->tableName(), $key, $this->$key);
|
||||||
|
} else if ($type == 'K' || $type == 'N') {
|
||||||
|
$pkey[] = $key;
|
||||||
|
$pval[] = $this->$key;
|
||||||
|
} else {
|
||||||
|
throw new Exception("Unknown key type $key => $type for " . $this->tableName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(count($pkey) > 0);
|
||||||
|
|
||||||
|
// XXX: should work for both compound and scalar pkeys
|
||||||
|
$pvals = implode(',', $pval);
|
||||||
|
$pkeys = implode(',', $pkey);
|
||||||
|
|
||||||
|
$ckeys[] = $this->cacheKey($this->tableName(), $pkeys, $pvals);
|
||||||
|
|
||||||
|
return $ckeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
function multicache($cls, $kv)
|
function multicache($cls, $kv)
|
||||||
|
Loading…
Reference in New Issue
Block a user