Replacing crc32 with md5 for less collisions in method caching

This commit is contained in:
Jelle Henkens 2012-05-31 18:38:40 +01:00
parent 07d9a75fcb
commit 18b335a605
2 changed files with 20 additions and 6 deletions

View file

@ -53,9 +53,8 @@ class DboSource extends DataSource {
/** /**
* Caches result from query parsing operations. Cached results for both DboSource::name() and * 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 * DboSource::conditions() will be stored here. Method caching uses `md5()`. If you have
* fast but can collisions more easily than other hashing algorithms. If you have problems * problems with collisions, set DboSource::$cacheMethods to false.
* with collisions, set DboSource::$cacheMethods to false.
* *
* @var array * @var array
*/ */
@ -767,7 +766,7 @@ class DboSource extends DataSource {
* Strips fields out of SQL functions before quoting. * Strips fields out of SQL functions before quoting.
* *
* Results of this method are stored in a memory cache. This improves performance, but * 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. * 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 * @param mixed $data Either a string with a column to quote. An array of columns to quote or an
@ -787,7 +786,7 @@ class DboSource extends DataSource {
} }
return $data; return $data;
} }
$cacheKey = crc32($this->startQuote . $data . $this->endQuote); $cacheKey = md5($this->startQuote . $data . $this->endQuote);
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) { if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
return $return; return $return;
} }
@ -2273,7 +2272,7 @@ class DboSource extends DataSource {
* is given it will be integer cast as condition. Null will return 1 = 1. * 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 * 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. * Setting DboSource::$cacheMethods to false will disable the memory cache.
* *
* @param mixed $conditions Array or string of conditions, or any value. * @param mixed $conditions Array or string of conditions, or any value.

View file

@ -599,6 +599,21 @@ class DboSourceTest extends CakeTestCase {
$this->assertNull($result); $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 * testLog method
* *