FIxing query caching to take in account bound parameters

This commit is contained in:
José Lorenzo Rodríguez 2010-10-17 10:51:54 -04:30
parent 097191213b
commit 7e2fe43ee3
2 changed files with 15 additions and 25 deletions

View file

@ -427,7 +427,7 @@ class DboSource extends DataSource {
$defaults = array('cache' => true);
$options = $options + $defaults;
$cache = $options['cache'];
if ($cache && ($cached = $this->getQueryCache($sql)) !== false) {
if ($cache && ($cached = $this->getQueryCache($sql, $params)) !== false) {
return $cached;
}
if ($this->execute($sql, array(), $params)) {
@ -443,7 +443,7 @@ class DboSource extends DataSource {
}
if ($cache) {
$this->_writeQueryCache($sql, $out);
$this->_writeQueryCache($sql, $out, $params);
}
if (empty($out) && is_bool($this->_result)) {
return $this->_result;
@ -2902,11 +2902,12 @@ class DboSource extends DataSource {
*
* @param string $sql SQL query
* @param mixed $data result of $sql query
* @param array $params query params bound as values
* @return void
*/
protected function _writeQueryCache($sql, $data) {
if (strpos(trim(strtolower($sql)), 'select') !== false) {
$this->_queryCache[$sql] = $data;
protected function _writeQueryCache($sql, $data, $params = array()) {
if (preg_match('/^\s*select/i', $sql)) {
$this->_queryCache[$sql][serialize($params)] = $data;
}
}
@ -2914,11 +2915,15 @@ class DboSource extends DataSource {
* Returns the result for a sql query if it is already cached
*
* @param string $sql SQL query
* @param array $params query params bound as values
* @return mixed results for query if it is cached, false otherwise
*/
public function getQueryCache($sql = null) {
public function getQueryCache($sql, $params = array()) {
if (isset($this->_queryCache[$sql]) && preg_match('/^\s*select/i', $sql)) {
return $this->_queryCache[$sql];
$serialized = serialize($params);
if (isset($this->_queryCache[$sql][$serialized])) {
return $this->_queryCache[$sql][$serialized];
}
}
return false;
}

View file

@ -279,13 +279,6 @@ class ModelReadTest extends BaseModelTest {
$this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
$Article = new Article();
$finalQuery = 'SELECT title, published FROM ';
$finalQuery .= $this->db->fullTableName('articles');
$finalQuery .= ' WHERE ' . $this->db->fullTableName('articles');
$finalQuery .= '.id = ' . $this->db->value(1);
$finalQuery .= ' AND ' . $this->db->fullTableName('articles');
$finalQuery .= '.published = ' . $this->db->value('Y');
$query = 'SELECT title, published FROM ';
$query .= $this->db->fullTableName('articles');
$query .= ' WHERE ' . $this->db->fullTableName('articles');
@ -305,14 +298,9 @@ class ModelReadTest extends BaseModelTest {
}
$this->assertEqual($result, $expected);
$result = $this->db->getQueryCache($finalQuery);
$result = $this->db->getQueryCache($query, $params);
$this->assertFalse(empty($result));
$finalQuery = 'SELECT id, created FROM ';
$finalQuery .= $this->db->fullTableName('articles');
$finalQuery .= ' WHERE ' . $this->db->fullTableName('articles');
$finalQuery .= '.title = ' . $this->db->value('First Article');
$query = 'SELECT id, created FROM ';
$query .= $this->db->fullTableName('articles');
$query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title = ?';
@ -324,7 +312,7 @@ class ModelReadTest extends BaseModelTest {
isset($result[0][$this->db->fullTableName('articles', false)])
|| isset($result[0][0])
);
$result = $this->db->getQueryCache($finalQuery);
$result = $this->db->getQueryCache($query, $params);
$this->assertTrue(empty($result));
$query = 'SELECT title FROM ';
@ -345,10 +333,7 @@ class ModelReadTest extends BaseModelTest {
$params = array('First? Article', 'Y');
$Article->query($query, $params);
$expected = 'SELECT title FROM ';
$expected .= $this->db->fullTableName('articles');
$expected .= " WHERE title = 'First? Article' AND published = 'Y'";
$result = $this->db->getQueryCache($expected);
$result = $this->db->getQueryCache($query, $params);
$this->assertFalse($result === false);
}