Browse files

MDL-38247 cache: fixed cache stores so that they pass unit tests

  • Loading branch information...
1 parent ac6754a commit 32c981e67acbb162e828d415c9c950318db7e501 Sam Hemelryk committed Feb 28, 2013
Showing with 43 additions and 21 deletions.
  1. +33 −17 cache/stores/mongodb/lib.php
  2. +5 −2 cache/stores/session/lib.php
  3. +5 −2 cache/stores/static/lib.php
View
50 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;
}
View
7 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;
}
View
7 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;
}

0 comments on commit 32c981e

Please sign in to comment.