Created
May 14, 2015 13:10
-
-
Save gherkins/032cec370deac576210a to your computer and use it in GitHub Desktop.
CachedMySQLDatabase - stores results of SELECT queries as arrays and reuses those... not sure if this is a good idea, though
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class CachedMySQLDatabase | |
* @inheritdoc | |
*/ | |
class CachedMySQLDatabase extends MySQLDatabase | |
{ | |
protected $cache = array(); | |
public function query($sql, $errorLevel = E_USER_ERROR) | |
{ | |
$method = strtolower(substr($sql, 0, strpos($sql, ' '))); | |
$cacheKey = crc32($sql); | |
//skip anything but select queries | |
if ($method !== "select") { | |
return parent::query($sql, $errorLevel = E_USER_ERROR); | |
} | |
if (!isset($this->cache[$cacheKey])) { | |
//create array data set from result | |
$handle = $this->dbConn->query($sql); | |
if (!$handle && $errorLevel) { | |
$this->databaseError("Couldn't run query: $sql | " . $this->dbConn->error, $errorLevel); | |
} | |
$dataset = array(); | |
while ($data = $handle->fetch_assoc()) { | |
$dataset[] = $data; | |
} | |
$handle->free(); | |
$this->cache[$cacheKey] = $dataset; | |
} | |
if (!isset($dataset)) { | |
$dataset = $this->cache[$cacheKey]; | |
} | |
return new CachedMySQLQuery($this, $dataset); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A result-set from a MySQL database. | |
* | |
* @package framework | |
* @subpackage model | |
*/ | |
class CachedMySQLQuery extends SS_Query | |
{ | |
/** | |
* The MySQLDatabase object that created this result set. | |
* @var MySQLDatabase | |
*/ | |
protected $database; | |
/** | |
* The internal MySQL handle that points to the result set. | |
* @var resource | |
*/ | |
protected $arrayObject; | |
/** | |
* @var int | |
*/ | |
protected $iterator = 0; | |
/** | |
* Hook the result-set given into a Query class, suitable for use by | |
* SilverStripe. | |
* | |
* @param SS_Database $database The database object that created this query. | |
* @param array $arrayObject the internal mysql handle that is points to the resultset. | |
*/ | |
public function __construct(MySQLDatabase $database, array $arrayObject) | |
{ | |
$this->database = $database; | |
$this->arrayObject = $arrayObject; | |
} | |
public function __destruct() | |
{ | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function seek($row) | |
{ | |
$this->iterator = $row; | |
return isset($this->arrayObject[$this->iterator]) ? $this->arrayObject[$this->iterator] : false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function numRecords() | |
{ | |
return count($this->arrayObject); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function nextRecord() | |
{ | |
$row = isset($this->arrayObject[$this->iterator]) ? $this->arrayObject[$this->iterator] : false; | |
$this->iterator++; | |
return $row; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment