Permalink
Browse files

MDL-36322 cache: cache stores now test connections during construction

  • Loading branch information...
1 parent 447475e commit 770eed82fd948ec93599ca2f8c6e2afb2c9dcb95 Sam Hemelryk committed Jan 24, 2013
Showing with 21 additions and 17 deletions.
  1. +9 −5 cache/stores/memcache/lib.php
  2. +9 −8 cache/stores/memcached/lib.php
  3. +3 −3 cache/stores/mongodb/lib.php
  4. +0 −1 cache/stores/session/lib.php
@@ -70,6 +70,12 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
protected $isready = false;
/**
+ * Set to true once this store instance has been initialised.
+ * @var bool
+ */
+ protected $isinitialised = false;
+
+ /**
* The cache definition this store was initialised for.
* @var cache_definition
*/
@@ -110,10 +116,7 @@ public function __construct($name, array $configuration = array()) {
foreach ($this->servers as $server) {
$this->connection->addServer($server[0], $server[1], true, $server[2]);
// Test the connection to this server.
- if (@$this->connection->set("$server[0]:$server[1]:$server[2]", 'ping', MEMCACHE_COMPRESSED, 1)) {
- // We can connect at least to this server.
- $this->isready = true;
- }
+ $this->isready = @$this->connection->set("ping", 'ping', MEMCACHE_COMPRESSED, 1);
}
}
@@ -129,6 +132,7 @@ public function initialise(cache_definition $definition) {
throw new coding_exception('This memcache instance has already been initialised.');
}
$this->definition = $definition;
+ $this->isinitialised = true;
}
/**
@@ -137,7 +141,7 @@ public function initialise(cache_definition $definition) {
* @return bool
*/
public function is_initialised() {
- return ($this->connection !== null);
+ return ($this->isinitialised);
}
/**
@@ -75,6 +75,12 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
protected $isready = false;
/**
+ * Set to true when this store instance has been initialised.
+ * @var bool
+ */
+ protected $isinitialised = false;
+
+ /**
* The cache definition this store was initialised with.
* @var cache_definition
*/
@@ -134,13 +140,7 @@ public function __construct($name, array $configuration = array()) {
$this->connection->setOption($key, $value);
}
$this->connection->addServers($this->servers);
- foreach ($this->servers as $server) {
- // Test the connection to this server.
- if (@$this->connection->set("$server[0]:$server[1]:$server[2]", 'ping', MEMCACHE_COMPRESSED, 1)) {
- // We can connect at least to this server.
- $this->isready = true;
- }
- }
+ $this->isready = @$this->connection->set("ping", 'ping', 1);
}
}
@@ -156,6 +156,7 @@ public function initialise(cache_definition $definition) {
throw new coding_exception('This memcached instance has already been initialised.');
}
$this->definition = $definition;
+ $this->isinitialised = true;
}
/**
@@ -164,7 +165,7 @@ public function initialise(cache_definition $definition) {
* @return bool
*/
public function is_initialised() {
- return ($this->connection !== null);
+ return ($this->isinitialised);
}
/**
@@ -132,10 +132,9 @@ public function __construct($name, array $configuration = array()) {
try {
$this->connection = new Mongo($this->server, $this->options);
- $this->database = $this->connection->selectDB($this->databasename);
$this->isready = true;
- } catch (Exception $e) {
- // Tipically, a MongoConnectionException.
+ } catch (MongoConnectionException $e) {
+ // We only want to catch MongoConnectionExceptions here.
}
}
@@ -181,6 +180,7 @@ public function initialise(cache_definition $definition) {
if ($this->is_initialised()) {
throw new coding_exception('This mongodb instance has already been initialised.');
}
+ $this->database = $this->connection->selectDB($this->databasename);
$this->definitionhash = $definition->generate_definition_hash();
$this->collection = $this->database->selectCollection($this->definitionhash);
$this->collection->ensureIndex(array('key' => 1), array(
@@ -361,7 +361,6 @@ public function delete_many(array $keys) {
*/
public function purge() {
$this->store = array();
-
return true;
}

0 comments on commit 770eed8

Please sign in to comment.