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

This commit is contained in:
Sam Hemelryk 2013-01-24 15:41:06 +13:00
parent f4cec2ec87
commit 65b3edc450
4 changed files with 21 additions and 17 deletions

View File

@ -69,6 +69,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 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
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 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
throw new coding_exception('This memcache instance has already been initialised.');
}
$this->definition = $definition;
$this->isinitialised = true;
}
/**
@ -137,7 +141,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
* @return bool
*/
public function is_initialised() {
return ($this->connection !== null);
return ($this->isinitialised);
}
/**

View File

@ -74,6 +74,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 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
$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 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
throw new coding_exception('This memcached instance has already been initialised.');
}
$this->definition = $definition;
$this->isinitialised = true;
}
/**
@ -164,7 +165,7 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
* @return bool
*/
public function is_initialised() {
return ($this->connection !== null);
return ($this->isinitialised);
}
/**

View File

@ -132,10 +132,9 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable {
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 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable {
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(

View File

@ -361,7 +361,6 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
*/
public function purge() {
$this->store = array();
return true;
}