Skip to content

Commit

Permalink
Preventing cache collisions by adding the the datasource key
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelle Henkens committed Jun 1, 2012
1 parent 18b335a commit e5eb7b4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -2172,7 +2172,8 @@ public function fields(Model $model, $alias = null, $fields = array(), $quote =
$model->alias,
$virtualFields,
$fields,
$quote
$quote,
ConnectionManager::getSourceName($this)
);
$cacheKey = md5(serialize($cacheKey));
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
Expand Down
55 changes: 55 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -53,6 +53,30 @@ public function setConnection($conn) {

}

class DboSecondTestSource extends DboSource {

public $startQuote = '_';

public $endQuote = '_';

public function connect($config = array()) {
$this->connected = true;
}

public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
}

public function setConfig($config = array()) {
$this->config = $config;
}

public function setConnection($conn) {
$this->_connection = $conn;
}

}

/**
* DboSourceTest class
*
Expand Down Expand Up @@ -790,6 +814,37 @@ public function testFieldsUsingMethodCache() {
$this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty');
}


/**
* test that fields() method cache detects datasource changes
*
* @return void
*/
public function testFieldsCacheKeyWithDatasourceChange() {
ConnectionManager::create('firstschema', array(
'datasource' => 'DboTestSource'
));
ConnectionManager::create('secondschema', array(
'datasource' => 'DboSecondTestSource'
));
Cache::delete('method_cache', '_cake_core_');
DboTestSource::$methodCache = array();
$Article = ClassRegistry::init('Article');

$Article->setDataSource('firstschema');
$ds = $Article->getDataSource();
$ds->cacheMethods = true;
$first = $ds->fields($Article, null, array('title', 'body', 'published'));

$Article->setDataSource('secondschema');
$ds = $Article->getDataSource();
$ds->cacheMethods = true;
$second = $ds->fields($Article, null, array('title', 'body', 'published'));

$this->assertNotEquals($first, $second);
$this->assertEquals(2, count(DboTestSource::$methodCache['fields']));
}

/**
* Test that group works without a model
*
Expand Down

0 comments on commit e5eb7b4

Please sign in to comment.