Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replacing crc32 with md5 for less collisions in method caching

  • Loading branch information...
commit 18b335a605356e11c1fec8341782e0b298125e2d 1 parent 07d9a75
@jellehenkens jellehenkens authored
View
11 lib/Cake/Model/Datasource/DboSource.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.
View
15 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -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
*
Please sign in to comment.
Something went wrong with that request. Please try again.