Skip to content

Commit

Permalink
Replacing crc32 with md5 for less collisions in method caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelle Henkens committed Jun 1, 2012
1 parent 07d9a75 commit 18b335a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -53,9 +53,8 @@ class DboSource extends DataSource {

/**
* Caches result from query parsing operations. Cached results for both DboSource::name() and
* DboSource::conditions() will be stored here. Method caching uses `crc32()` which is
* fast but can collisions more easily than other hashing algorithms. If you have problems
* with collisions, set DboSource::$cacheMethods to false.
* DboSource::conditions() will be stored here. Method caching uses `md5()`. If you have
* problems with collisions, set DboSource::$cacheMethods to false.
*
* @var array
*/
Expand Down Expand Up @@ -767,7 +766,7 @@ public function cacheMethod($method, $key, $value = null) {
* Strips fields out of SQL functions before quoting.
*
* Results of this method are stored in a memory cache. This improves performance, but
* because the method uses a simple hashing algorithm it can infrequently have collisions.
* because the method uses a hashing algorithm it can have collisions.
* Setting DboSource::$cacheMethods to false will disable the memory cache.
*
* @param mixed $data Either a string with a column to quote. An array of columns to quote or an
Expand All @@ -787,7 +786,7 @@ public function name($data) {
}
return $data;
}
$cacheKey = crc32($this->startQuote . $data . $this->endQuote);
$cacheKey = md5($this->startQuote . $data . $this->endQuote);
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
return $return;
}
Expand Down Expand Up @@ -2273,7 +2272,7 @@ public function fields(Model $model, $alias = null, $fields = array(), $quote =
* is given it will be integer cast as condition. Null will return 1 = 1.
*
* Results of this method are stored in a memory cache. This improves performance, but
* because the method uses a simple hashing algorithm it can infrequently have collisions.
* because the method uses a hashing algorithm it can have collisions.
* Setting DboSource::$cacheMethods to false will disable the memory cache.
*
* @param mixed $conditions Array or string of conditions, or any value.
Expand Down
15 changes: 15 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -599,6 +599,21 @@ public function testCacheMethod() {
$this->assertNull($result);
}


/**
* Test that rare collisions do not happen with method caching
*
* @return void
*/
public function testNameMethodCacheCollisions() {
$this->testDb->cacheMethods = true;
$this->testDb->flushMethodCache();
$this->testDb->name('Model.fieldlbqndkezcoapfgirmjsh');
$result = $this->testDb->name('Model.fieldkhdfjmelarbqnzsogcpi');
$expected = '`Model`.`fieldkhdfjmelarbqnzsogcpi`';
$this->assertEquals($expected, $result);
}

/**
* testLog method
*
Expand Down

0 comments on commit 18b335a

Please sign in to comment.