Permalink
Browse files

Preventing cache collisions by adding the the datasource key

  • Loading branch information...
1 parent 18b335a commit e5eb7b490e9d0853d61f8ba437bca0756088987d Jelle Henkens committed May 31, 2012
Showing with 57 additions and 1 deletion.
  1. +2 −1 lib/Cake/Model/Datasource/DboSource.php
  2. +55 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
View
3 lib/Cake/Model/Datasource/DboSource.php
@@ -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)) {
View
55 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -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
*
@@ -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
*

0 comments on commit e5eb7b4

Please sign in to comment.