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:
Evan Prodromou 2010-01-04 08:59:19 -10:00
parent 8f362e9956
commit 5a1ea0b9b2

View File

@ -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();
$types = $this->keyTypes();
ksort($types);
foreach ($types as $key => $type) {
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 $keys = $this->_allCacheKeys();
$pvals = implode(',', $pval);
$pkeys = implode(',', $pkey); foreach ($keys as $key) {
$c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this); $c->set($key, $this);
} }
} }
function decache() function decache()
{ {
$c = $this->memcache(); $c = $this->memcache();
if (!$c) { if (!$c) {
return false; return false;
} else { }
$pkey = array();
$pval = array(); $keys = $this->_allCacheKeys();
foreach ($keys as $key) {
$c->delete($key, $this);
}
}
function _allCacheKeys()
{
$ckeys = array();
$types = $this->keyTypes(); $types = $this->keyTypes();
ksort($types); ksort($types);
$pkey = array();
$pval = array();
foreach ($types as $key => $type) { foreach ($types as $key => $type) {
if ($type == 'K') {
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; $pkey[] = $key;
$pval[] = $this->$key; $pval[] = $this->$key;
} else { } else {
$c->delete($this->cacheKey($this->tableName(), $key, $this->$key)); throw new Exception("Unknown key type $key => $type for " . $this->tableName());
} }
} }
# should work for both compound and scalar pkeys
# XXX: comma works for now but may not be safe separator for future keys assert(count($pkey) > 0);
// XXX: should work for both compound and scalar pkeys
$pvals = implode(',', $pval); $pvals = implode(',', $pval);
$pkeys = implode(',', $pkey); $pkeys = implode(',', $pkey);
$c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
} $ckeys[] = $this->cacheKey($this->tableName(), $pkeys, $pvals);
return $ckeys;
} }
function multicache($cls, $kv) function multicache($cls, $kv)