diff --git a/cache/stores/mongodb/lib.php b/cache/stores/mongodb/lib.php index 7a5e6ec6a5ca3..a6fd6a26c0e5a 100644 --- a/cache/stores/mongodb/lib.php +++ b/cache/stores/mongodb/lib.php @@ -274,11 +274,7 @@ public function get_many($keys) { $cursor = $this->collection->find($query); $results = array(); foreach ($cursor as $result) { - if (array_key_exists('key', $result)) { - $id = $result[$key]; - } else { - $id = (string)$result['key']; - } + $id = (string)$result['key']; $results[$id] = unserialize($result['data']); } foreach ($keys as $key) { @@ -307,11 +303,22 @@ public function set($key, $data) { $record['data'] = serialize($data); $options = array( 'upsert' => true, - 'safe' => $this->usesafe + 'safe' => $this->usesafe, + 'w' => $this->usesafe ? 1 : 0 ); $this->delete($key); $result = $this->collection->insert($record, $options); - return $result; + if ($result === true) { + // Safe mode is off. + return true; + } else if (is_array($result)) { + if (empty($result['ok']) || isset($result['err'])) { + return false; + } + return true; + } + // Who knows? + return false; } /** @@ -326,11 +333,11 @@ public function set_many(array $keyvaluearray) { $count = 0; foreach ($keyvaluearray as $pair) { $result = $this->set($pair['key'], $pair['value']); - if ($result === true || (is_array($result)) && !empty($result['ok'])) { + if ($result === true) { $count++; } } - return; + return $count; } /** @@ -349,13 +356,25 @@ public function delete($key) { } $options = array( 'justOne' => false, - 'safe' => $this->usesafe + 'safe' => $this->usesafe, + 'w' => $this->usesafe ? 1 : 0 ); $result = $this->collection->remove($criteria, $options); - if ($result === false || (is_array($result) && !array_key_exists('ok', $result)) || $result === 0) { - return false; + + if ($result === true) { + // Safe mode. + return true; + } else if (is_array($result)) { + if (empty($result['ok']) || isset($result['err'])) { + return false; + } else if (empty($result['n'])) { + // Nothing was removed. + return false; + } + return true; } - return !empty($result['ok']); + // Who knows? + return false; } /** @@ -496,7 +515,6 @@ public static function initialise_test_instance(cache_definition $definition) { if (empty($config->testserver)) { return false; } - $configuration = array(); $configuration['server'] = $config->testserver; if (!empty($config->testreplicaset)) { @@ -511,9 +529,7 @@ public static function initialise_test_instance(cache_definition $definition) { if (!empty($config->testdatabase)) { $configuration['database'] = $config->testdatabase; } - if (!empty($config->testusesafe)) { - $configuration['usesafe'] = $config->testusesafe; - } + $configuration['usesafe'] = 1; if (!empty($config->testextendedmode)) { $configuration['extendedmode'] = (bool)$config->testextendedmode; } diff --git a/cache/stores/session/lib.php b/cache/stores/session/lib.php index 8cb42f0d0e069..441bd70cc2e92 100644 --- a/cache/stores/session/lib.php +++ b/cache/stores/session/lib.php @@ -335,8 +335,9 @@ public function has_any(array $keys) { * @return bool Returns true if the operation was a success, false otherwise. */ public function delete($key) { + $result = isset($this->store[$key]); unset($this->store[$key]); - return true; + return $result; } /** @@ -348,8 +349,10 @@ public function delete($key) { public function delete_many(array $keys) { $count = 0; foreach ($keys as $key) { + if (isset($this->store[$key])) { + $count++; + } unset($this->store[$key]); - $count++; } return $count; } diff --git a/cache/stores/static/lib.php b/cache/stores/static/lib.php index 4ac32de7448e7..cc74f7409ba5b 100644 --- a/cache/stores/static/lib.php +++ b/cache/stores/static/lib.php @@ -331,8 +331,9 @@ public function has_any(array $keys) { * @return bool Returns true if the operation was a success, false otherwise. */ public function delete($key) { + $result = isset($this->store[$key]); unset($this->store[$key]); - return true; + return $result; } /** @@ -344,8 +345,10 @@ public function delete($key) { public function delete_many(array $keys) { $count = 0; foreach ($keys as $key) { + if (isset($this->store[$key])) { + $count++; + } unset($this->store[$key]); - $count++; } return $count; }