Cache PDO connections and connection failures in a single page load

This commit is contained in:
kaetemi 2014-09-09 02:15:25 +02:00
parent 7ebc9478e6
commit 001f90bb99

View file

@ -29,13 +29,13 @@
* *
*/ */
// $PDOCache = array(); $PDOCache = array();
class DBLayer { class DBLayer {
private $PDO; private $PDO;
// private $host; private $host;
// private $dbname; private $dbname;
/** /**
* The PDO object, instantiated by the constructor * The PDO object, instantiated by the constructor
@ -54,12 +54,15 @@ class DBLayer {
} }
global $cfg; global $cfg;
// $this->host = $cfg['db'][$db]['host']; $this->host = $cfg['db'][$db]['host'];
// $this->dbname = $cfg['db'][$db]['name']; $this->dbname = $cfg['db'][$db]['name'];
/*global $PDOCache; global $PDOCache;
if (isset($PDOCache[$this->host])) { if (isset($PDOCache[$this->host])) {
if (isset($PDOCache[$this->host]['exception'])) {
throw $PDOCache[$this->host]['exception'];
}
$this->PDO = $PDOCache[$this->host]['pdo']; $this->PDO = $PDOCache[$this->host]['pdo'];
} else {*/ } else {
$dsn = "mysql:"; $dsn = "mysql:";
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";"; $dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
$dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";"; // Comment this out when using the cache $dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";"; // Comment this out when using the cache
@ -68,14 +71,21 @@ class DBLayer {
$opt = array( $opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => true PDO::ATTR_PERSISTENT => true,
PDO::ATTR_TIMEOUT => 5
); );
$PDOCache[$this->host] = array();
try {
$this->PDO = new PDO($dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt); $this->PDO = new PDO($dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt);
/* $PDOCache[$this->host] = array(); } catch (Exception $e) {
$PDOCache[$this->host]['exception'] = $e;
throw $e;
return;
}
$PDOCache[$this->host]['pdo'] = $this->PDO; $PDOCache[$this->host]['pdo'] = $this->PDO;
$PDOCache[$this->host]['use'] = $this->dbname; $PDOCache[$this->host]['use'] = $this->dbname;
*/ //$this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety $this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety
/*}*/ }
} }
function __destruct() { function __destruct() {
@ -83,11 +93,11 @@ class DBLayer {
} }
function useDb() { function useDb() {
/*global $PDOCache; global $PDOCache;
if ($PDOCache[$this->host]['use'] != $this->dbname) { if ($PDOCache[$this->host]['use'] != $this->dbname) {
$PDOCache[$this->host]['use'] = $this->dbname; $PDOCache[$this->host]['use'] = $this->dbname;
$this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety $this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety
}*/ }
} }
/** /**