Skip to content

Commit dc55201

Browse files
committed
Merge pull request #79 from giosh94mhz/master
CacheProvider: better hashing function and namespace
2 parents bfb752c + f12f008 commit dc55201

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Provider/CacheProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(Cache $cache, ProviderInterface $provider, $lifetime
6565
*/
6666
public function getGeocodedData($address)
6767
{
68-
$key = crc32($this->locale.$address);
68+
$key = 'geocoder_' . sha1($this->locale . $address);
6969

7070
if (false !== $data = $this->cache->fetch($key)) {
7171
return unserialize($data);
@@ -82,7 +82,7 @@ public function getGeocodedData($address)
8282
*/
8383
public function getReversedData(array $coordinates)
8484
{
85-
$key = crc32(serialize($this->locale . json_encode($coordinates)));
85+
$key = 'geocoder_' . sha1($this->locale . json_encode($coordinates));
8686

8787
if (false !== $data = $this->cache->fetch($key)) {
8888
return unserialize($data);

Tests/Provider/CacheProviderTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function testGetGeocodedData()
1414
{
1515
$address = 'Paris, France';
1616
$coordinates = array('lat' => 48.857049,'lng' => 2.35223);
17+
$cacheKey = 'geocoder_' . sha1($address);
1718

1819
$delegate = $this->getMock('Geocoder\\Provider\\ProviderInterface');
1920
$delegate->expects($this->once())
@@ -24,12 +25,12 @@ public function testGetGeocodedData()
2425
$cache = $this->getMock('Doctrine\\Common\\Cache\\Cache');
2526
$cache->expects($this->once())
2627
->method('fetch')
27-
->with(crc32($address))
28+
->with($cacheKey)
2829
->will($this->returnValue(false));
2930

3031
$cache->expects($this->once())
3132
->method('save')
32-
->with(crc32($address), serialize($coordinates), 0);
33+
->with($cacheKey, serialize($coordinates), 0);
3334

3435
$provider = new CacheProvider($cache, $delegate);
3536
$this->assertEquals($coordinates, $provider->getGeocodedData($address));
@@ -39,6 +40,7 @@ public function testGetCachedGeocodedData()
3940
{
4041
$address = 'Paris, France';
4142
$coordinates = array('lat' => 48.857049,'lng' => 2.35223);
43+
$cacheKey = 'geocoder_' . sha1($address);
4244

4345
$delegate = $this->getMock('Geocoder\\Provider\\ProviderInterface');
4446
$delegate->expects($this->once())
@@ -51,7 +53,7 @@ public function testGetCachedGeocodedData()
5153
$provider->getGeocodedData($address);
5254
$provider->getGeocodedData($address);
5355

54-
$this->assertTrue($cache->contains(crc32($address)));
56+
$this->assertTrue($cache->contains($cacheKey));
5557
}
5658

5759
public function testGetReversedData()

0 commit comments

Comments
 (0)