Skip to content
Permalink
Browse files

Replacing crc32 with md5 for less collisions in method caching

  • Loading branch information...
Jelle Henkens
Jelle Henkens committed May 31, 2012
1 parent 07d9a75 commit 18b335a605356e11c1fec8341782e0b298125e2d
Showing with 20 additions and 6 deletions.
  1. +5 −6 lib/Cake/Model/Datasource/DboSource.php
  2. +15 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -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
*/
@@ -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
@@ -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;
}
@@ -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.
@@ -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
*

0 comments on commit 18b335a

Please sign in to comment.
You can’t perform that action at this time.