Skip to content

Commit 492d50b

Browse files
committed
Merge pull request #7 from php-cache/sha2
Make sure we shorten the cache keys
2 parents faf9a2b + 10c8589 commit 492d50b

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/HierarchicalCachePoolTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function getHierarchyKey($key, &$pathKey = null)
5757
// 1) $keyString = "foo!tagHash"
5858
// 2) $keyString = "foo!tagHash![foo_index]!bar!tagHash"
5959
$keyString .= $name;
60-
$pathKey = 'path'.TaggablePoolInterface::TAG_SEPARATOR.$keyString;
60+
$pathKey = sha1('path'.TaggablePoolInterface::TAG_SEPARATOR.$keyString);
6161

6262
if (isset($this->keyCache[$pathKey])) {
6363
$index = $this->keyCache[$pathKey];
@@ -74,7 +74,8 @@ protected function getHierarchyKey($key, &$pathKey = null)
7474
// Assert: $pathKey = "path!foo!tagHash![foo_index]!bar!tagHash"
7575
// Assert: $keyString = "foo!tagHash![foo_index]!bar!tagHash![bar_index]!"
7676

77-
return $keyString;
77+
// Make sure we do not get awfully long (>250 chars) keys
78+
return sha1($keyString);
7879
}
7980

8081
/**

tests/HierarchicalCachePoolTest.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
use Cache\Hierarchy\Tests\Helper\CachePool;
1515

1616
/**
17-
* We should not use constans on interfaces in the tests. Tests should break if the constant is changed.
17+
* We should not use constants on interfaces in the tests. Tests should break if the constant is changed.
1818
*
1919
* @author Tobias Nyholm <[email protected]>
2020
*/
2121
class HierarchicalCachePoolTest extends \PHPUnit_Framework_TestCase
2222
{
23+
private function assertEqualsSha1($expected, $result, $message = '')
24+
{
25+
$this->assertEquals(sha1($expected), $result, $message);
26+
}
27+
2328
public function testGetHierarchyKey()
2429
{
2530
$path = null;
@@ -31,13 +36,13 @@ public function testGetHierarchyKey()
3136

3237
$pool = new CachePool(['idx_1', 'idx_2', 'idx_3']);
3338
$result = $pool->exposeGetHierarchyKey('|foo|bar', $path);
34-
$this->assertEquals('root!!idx_1!foo!!idx_2!bar!!idx_3!', $result);
35-
$this->assertEquals('path!root!!idx_1!foo!!idx_2!bar!', $path);
39+
$this->assertEqualsSha1('root!!idx_1!foo!!idx_2!bar!!idx_3!', $result);
40+
$this->assertEqualsSha1('path!root!!idx_1!foo!!idx_2!bar!', $path);
3641

3742
$pool = new CachePool(['idx_1', 'idx_2', 'idx_3']);
3843
$result = $pool->exposeGetHierarchyKey('|', $path);
39-
$this->assertEquals('path!root!', $path);
40-
$this->assertEquals('root!!idx_1!', $result);
44+
$this->assertEqualsSha1('path!root!', $path);
45+
$this->assertEqualsSha1('root!!idx_1!', $result);
4146
}
4247

4348
public function testGetHierarchyKeyWithTags()
@@ -51,13 +56,13 @@ public function testGetHierarchyKeyWithTags()
5156

5257
$pool = new CachePool(['idx_1', 'idx_2', 'idx_3']);
5358
$result = $pool->exposeGetHierarchyKey('|foo|bar!tagHash', $path);
54-
$this->assertEquals('root!tagHash!idx_1!foo!tagHash!idx_2!bar!tagHash!idx_3!', $result);
55-
$this->assertEquals('path!root!tagHash!idx_1!foo!tagHash!idx_2!bar!tagHash', $path);
59+
$this->assertEqualsSha1('root!tagHash!idx_1!foo!tagHash!idx_2!bar!tagHash!idx_3!', $result);
60+
$this->assertEqualsSha1('path!root!tagHash!idx_1!foo!tagHash!idx_2!bar!tagHash', $path);
5661

5762
$pool = new CachePool(['idx_1', 'idx_2', 'idx_3']);
5863
$result = $pool->exposeGetHierarchyKey('|!tagHash', $path);
59-
$this->assertEquals('path!root!tagHash', $path);
60-
$this->assertEquals('root!tagHash!idx_1!', $result);
64+
$this->assertEqualsSha1('path!root!tagHash', $path);
65+
$this->assertEqualsSha1('root!tagHash!idx_1!', $result);
6166
}
6267

6368
public function testGetHierarchyKeyEmptyCache()
@@ -70,12 +75,12 @@ public function testGetHierarchyKeyEmptyCache()
7075
$this->assertNull($path);
7176

7277
$result = $pool->exposeGetHierarchyKey('|foo|bar', $path);
73-
$this->assertEquals('root!!!foo!!!bar!!!', $result);
74-
$this->assertEquals('path!root!!!foo!!!bar!', $path);
78+
$this->assertEqualsSha1('root!!!foo!!!bar!!!', $result);
79+
$this->assertEqualsSha1('path!root!!!foo!!!bar!', $path);
7580

7681
$result = $pool->exposeGetHierarchyKey('|', $path);
77-
$this->assertEquals('path!root!', $path);
78-
$this->assertEquals('root!!!', $result);
82+
$this->assertEqualsSha1('path!root!', $path);
83+
$this->assertEqualsSha1('root!!!', $result);
7984
}
8085

8186
public function testKeyCache()
@@ -84,13 +89,13 @@ public function testKeyCache()
8489

8590
$pool = new CachePool(['idx_1', 'idx_2', 'idx_3']);
8691
$result = $pool->exposeGetHierarchyKey('|foo', $path);
87-
$this->assertEquals('root!!idx_1!foo!!idx_2!', $result);
88-
$this->assertEquals('path!root!!idx_1!foo!', $path);
92+
$this->assertEqualsSha1('root!!idx_1!foo!!idx_2!', $result);
93+
$this->assertEqualsSha1('path!root!!idx_1!foo!', $path);
8994

9095
// Make sure re reuse the old index value we already looked up for 'root'.
9196
$result = $pool->exposeGetHierarchyKey('|bar', $path);
92-
$this->assertEquals('root!!idx_1!bar!!idx_3!', $result);
93-
$this->assertEquals('path!root!!idx_1!bar!', $path);
97+
$this->assertEqualsSha1('root!!idx_1!bar!!idx_3!', $result);
98+
$this->assertEqualsSha1('path!root!!idx_1!bar!', $path);
9499
}
95100

96101
public function testClearHierarchyKeyCache()

0 commit comments

Comments
 (0)