From: Evan Prodromou Date: Thu, 14 Jul 2011 16:02:44 +0000 (-0400) Subject: add Memcached_DataObject::multiGet() method X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=75b280b4bbb853eb40ef626af7329af0bcf18924;p=quix0rs-gnu-social.git add Memcached_DataObject::multiGet() method --- diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php index 0e60b7fed5..f1336e582f 100644 --- a/classes/Memcached_DataObject.php +++ b/classes/Memcached_DataObject.php @@ -63,7 +63,56 @@ class Memcached_DataObject extends Safe_DataObject } return $i; } + + function multiGet($cls, $keyCol, $keyVals) + { + $result = array_fill_keys($keyVals, null); + + $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; + } + } + } + + return new ArrayWrapper(array_values($result)); + } + function columnType($columnName) + { + $keys = $this->table(); + if (!array_key_exists($columnName, $keys)) { + throw new Exception('Unknown key column ' . $columnName . ' in ' . join(',', array_keys($keys))); + } + + $def = $keys[$columnName]; + + if ($def & DB_DATAOBJECT_INT) { + return 'integer'; + } else { + return 'string'; + } + } + /** * @fixme Should this return false on lookup fail to match staticGet? */