Skip to content

Commit

Permalink
MDL-36322 cache: cache stores now test connections during construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Jan 31, 2013
1 parent f4cec2e commit 65b3edc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
14 changes: 9 additions & 5 deletions cache/stores/memcache/lib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
*/ */
protected $isready = false; 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. * The cache definition this store was initialised for.
* @var cache_definition * @var cache_definition
Expand Down Expand Up @@ -110,10 +116,7 @@ public function __construct($name, array $configuration = array()) {
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
$this->connection->addServer($server[0], $server[1], true, $server[2]); $this->connection->addServer($server[0], $server[1], true, $server[2]);
// Test the connection to this server. // Test the connection to this server.
if (@$this->connection->set("$server[0]:$server[1]:$server[2]", 'ping', MEMCACHE_COMPRESSED, 1)) { $this->isready = @$this->connection->set("ping", 'ping', MEMCACHE_COMPRESSED, 1);
// We can connect at least to this server.
$this->isready = true;
}
} }
} }


Expand All @@ -129,6 +132,7 @@ public function initialise(cache_definition $definition) {
throw new coding_exception('This memcache instance has already been initialised.'); throw new coding_exception('This memcache instance has already been initialised.');
} }
$this->definition = $definition; $this->definition = $definition;
$this->isinitialised = true;
} }


/** /**
Expand All @@ -137,7 +141,7 @@ public function initialise(cache_definition $definition) {
* @return bool * @return bool
*/ */
public function is_initialised() { public function is_initialised() {
return ($this->connection !== null); return ($this->isinitialised);
} }


/** /**
Expand Down
17 changes: 9 additions & 8 deletions cache/stores/memcached/lib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
*/ */
protected $isready = false; 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. * The cache definition this store was initialised with.
* @var cache_definition * @var cache_definition
Expand Down Expand Up @@ -134,13 +140,7 @@ public function __construct($name, array $configuration = array()) {
$this->connection->setOption($key, $value); $this->connection->setOption($key, $value);
} }
$this->connection->addServers($this->servers); $this->connection->addServers($this->servers);
foreach ($this->servers as $server) { $this->isready = @$this->connection->set("ping", 'ping', 1);
// 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;
}
}
} }
} }


Expand All @@ -156,6 +156,7 @@ public function initialise(cache_definition $definition) {
throw new coding_exception('This memcached instance has already been initialised.'); throw new coding_exception('This memcached instance has already been initialised.');
} }
$this->definition = $definition; $this->definition = $definition;
$this->isinitialised = true;
} }


/** /**
Expand All @@ -164,7 +165,7 @@ public function initialise(cache_definition $definition) {
* @return bool * @return bool
*/ */
public function is_initialised() { public function is_initialised() {
return ($this->connection !== null); return ($this->isinitialised);
} }


/** /**
Expand Down
6 changes: 3 additions & 3 deletions cache/stores/mongodb/lib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ public function __construct($name, array $configuration = array()) {


try { try {
$this->connection = new Mongo($this->server, $this->options); $this->connection = new Mongo($this->server, $this->options);
$this->database = $this->connection->selectDB($this->databasename);
$this->isready = true; $this->isready = true;
} catch (Exception $e) { } catch (MongoConnectionException $e) {
// Tipically, a MongoConnectionException. // We only want to catch MongoConnectionExceptions here.
} }
} }


Expand Down Expand Up @@ -181,6 +180,7 @@ public function initialise(cache_definition $definition) {
if ($this->is_initialised()) { if ($this->is_initialised()) {
throw new coding_exception('This mongodb instance has already been 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->definitionhash = $definition->generate_definition_hash();
$this->collection = $this->database->selectCollection($this->definitionhash); $this->collection = $this->database->selectCollection($this->definitionhash);
$this->collection->ensureIndex(array('key' => 1), array( $this->collection->ensureIndex(array('key' => 1), array(
Expand Down
1 change: 0 additions & 1 deletion cache/stores/session/lib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ public function delete_many(array $keys) {
*/ */
public function purge() { public function purge() {
$this->store = array(); $this->store = array();

return true; return true;
} }


Expand Down

0 comments on commit 65b3edc

Please sign in to comment.