Skip to content

Commit

Permalink
MDL-38247 cache: fixed cache stores so that they pass unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Feb 28, 2013
1 parent ac6754a commit 32c981e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
50 changes: 33 additions & 17 deletions cache/stores/mongodb/lib.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions cache/stores/session/lib.php
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions cache/stores/static/lib.php
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand Down

0 comments on commit 32c981e

Please sign in to comment.