New method Memcached_DataObject::pivotGet()
This method lets you get all the objects with a given variable key and another set of "fixed" keys. A good example is getting all the avatars for a notice list; the avatar size stays the same, but the IDs change. Since it's very similar to multiGet(), I refactored that function to use pivotGet(). And, yes, I realize these are kind of hard to follow.
This commit is contained in:
parent
200e18cd71
commit
72ed297214
@ -76,46 +76,13 @@ class Memcached_DataObject extends Safe_DataObject
|
||||
*/
|
||||
function multiGet($cls, $keyCol, $keyVals, $skipNulls=true)
|
||||
{
|
||||
$result = array_fill_keys($keyVals, null);
|
||||
$result = self::pivotGet($cls, $keyCol, $keyVals);
|
||||
|
||||
$toFetch = array();
|
||||
|
||||
foreach ($keyVals as $keyVal) {
|
||||
$i = self::getcached($cls, $keyCol, $keyVal);
|
||||
if ($i !== false) {
|
||||
$result[$keyVal] = $i;
|
||||
} else if (!empty($keyVal)) {
|
||||
$toFetch[] = $keyVal;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($toFetch) > 0) {
|
||||
$i = DB_DataObject::factory($cls);
|
||||
if (empty($i)) {
|
||||
throw new Exception(_('Cannot instantiate class ' . $cls));
|
||||
}
|
||||
$i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol));
|
||||
if ($i->find()) {
|
||||
while ($i->fetch()) {
|
||||
$copy = clone($i);
|
||||
$copy->encache();
|
||||
$result[$i->$keyCol] = $copy;
|
||||
}
|
||||
}
|
||||
|
||||
// Save state of DB misses
|
||||
|
||||
foreach ($toFetch as $keyVal) {
|
||||
if (empty($result[$keyVal])) {
|
||||
// save the fact that no such row exists
|
||||
$c = self::memcache();
|
||||
if (!empty($c)) {
|
||||
$ck = self::cachekey($cls, $keyCol, $keyVal);
|
||||
$c->set($ck, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
common_log(LOG_INFO, sprintf("Got %d results for class %s with %d keys on column %s",
|
||||
count($result),
|
||||
$cls,
|
||||
count($keyVals),
|
||||
$keyCol));
|
||||
|
||||
$values = array_values($result);
|
||||
|
||||
@ -132,6 +99,70 @@ class Memcached_DataObject extends Safe_DataObject
|
||||
return new ArrayWrapper($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get multiple items from the database by key
|
||||
*
|
||||
* @param string $cls Class to fetch
|
||||
* @param string $keyCol name of column for key
|
||||
* @param array $keyVals key values to fetch
|
||||
* @param boolean $otherCols Other columns to hold fixed
|
||||
*
|
||||
* @return array Array mapping $keyVals to objects, or null if not found
|
||||
*/
|
||||
static function pivotGet($cls, $keyCol, $keyVals, $otherCols = array())
|
||||
{
|
||||
$result = array_fill_keys($keyVals, null);
|
||||
|
||||
$toFetch = array();
|
||||
|
||||
foreach ($keyVals as $keyVal) {
|
||||
|
||||
$kv = array_merge($otherCols, array($keyCol => $keyVal));
|
||||
|
||||
$i = self::multicache($cls, $kv);
|
||||
|
||||
if ($i !== false) {
|
||||
$result[$keyVal] = $i;
|
||||
} else if (!empty($keyVal)) {
|
||||
$toFetch[] = $keyVal;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($toFetch) > 0) {
|
||||
$i = DB_DataObject::factory($cls);
|
||||
if (empty($i)) {
|
||||
throw new Exception(_('Cannot instantiate class ' . $cls));
|
||||
}
|
||||
foreach ($otherCols as $otherKeyCol => $otherKeyVal) {
|
||||
$i->$otherKeyCol = $otherKeyVal;
|
||||
}
|
||||
$i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol));
|
||||
if ($i->find()) {
|
||||
while ($i->fetch()) {
|
||||
$copy = clone($i);
|
||||
$copy->encache();
|
||||
$result[$i->$keyCol] = $copy;
|
||||
}
|
||||
}
|
||||
|
||||
// Save state of DB misses
|
||||
|
||||
foreach ($toFetch as $keyVal) {
|
||||
if (empty($result[$keyVal])) {
|
||||
$kv = array_merge($otherCols, array($keyCol => $keyVal));
|
||||
// save the fact that no such row exists
|
||||
$c = self::memcache();
|
||||
if (!empty($c)) {
|
||||
$ck = self::multicacheKey($cls, $keyCol, $keyVal);
|
||||
$c->set($ck, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function columnType($columnName)
|
||||
{
|
||||
$keys = $this->table();
|
||||
|
Loading…
Reference in New Issue
Block a user