From: Evan Prodromou Date: Thu, 14 Jul 2011 18:41:30 +0000 (-0400) Subject: explicitly skip nulls in Memcached_DataObject::multiGet() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ae340ec34586e2a9083f7c8e1cef12224e08381e;p=quix0rs-gnu-social.git explicitly skip nulls in Memcached_DataObject::multiGet() --- diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php index f1336e582f..0eae9fb42a 100644 --- a/classes/Memcached_DataObject.php +++ b/classes/Memcached_DataObject.php @@ -64,7 +64,17 @@ class Memcached_DataObject extends Safe_DataObject return $i; } - function multiGet($cls, $keyCol, $keyVals) + /** + * 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 $skipNulls return only non-null results? + * + * @return array Array of objects, in order + */ + function multiGet($cls, $keyCol, $keyVals, $skipNulls=true) { $result = array_fill_keys($keyVals, null); @@ -92,9 +102,34 @@ class Memcached_DataObject extends Safe_DataObject $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); + } + } + } + } + + $values = array_values($result); + + if ($skipNulls) { + $tmp = array(); + foreach ($values as $value) { + if (!empty($value)) { + $tmp[] = $value; + } + } + $values = $tmp; } - return new ArrayWrapper(array_values($result)); + return new ArrayWrapper($values); } function columnType($columnName) diff --git a/classes/Notice.php b/classes/Notice.php index 4476798661..e82b567e9c 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -84,9 +84,9 @@ class Notice extends Memcached_DataObject /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE - function multiGet($kc, $kvs) + function multiGet($kc, $kvs, $skipNulls=true) { - return Memcached_DataObject::multiGet('Notice', $kc, $kvs); + return Memcached_DataObject::multiGet('Notice', $kc, $kvs, $skipNulls); } /* Notice types */