Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 637979c

Browse files
samybassetNyholm
authored andcommitted
Add precision argument in CachePlugin (#883)
1 parent 711264a commit 637979c

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

Plugin/CachePlugin.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
namespace Geocoder\Plugin\Plugin;
1414

15+
use Geocoder\Model\Coordinates;
1516
use Geocoder\Plugin\Plugin;
1617
use Geocoder\Query\Query;
18+
use Geocoder\Query\ReverseQuery;
1719
use Psr\SimpleCache\CacheInterface;
1820

1921
/**
@@ -35,14 +37,21 @@ class CachePlugin implements Plugin
3537
*/
3638
private $lifetime;
3739

40+
/**
41+
* @var int|null
42+
*/
43+
private $precision;
44+
3845
/**
3946
* @param CacheInterface $cache
40-
* @param int $lifetime
47+
* @param int|null $lifetime
48+
* @param int|null $precision
4149
*/
42-
public function __construct(CacheInterface $cache, int $lifetime = null)
50+
public function __construct(CacheInterface $cache, int $lifetime = null, int $precision = null)
4351
{
4452
$this->cache = $cache;
4553
$this->lifetime = $lifetime;
54+
$this->precision = $precision;
4655
}
4756

4857
/**
@@ -68,6 +77,13 @@ public function handleQuery(Query $query, callable $next, callable $first)
6877
*/
6978
private function getCacheKey(Query $query): string
7079
{
80+
if (null !== $this->precision && $query instanceof ReverseQuery) {
81+
$query = $query->withCoordinates(new Coordinates(
82+
number_format($query->getCoordinates()->getLatitude(), $this->precision),
83+
number_format($query->getCoordinates()->getLongitude(), $this->precision)
84+
));
85+
}
86+
7187
// Include the major version number of the geocoder to avoid issues unserializing.
7288
return 'v4'.sha1((string) $query);
7389
}

Tests/Plugin/CachePluginTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
namespace Geocoder\Plugin\Tests\Plugin;
1414

1515
use Cache\Adapter\Void\VoidCachePool;
16+
use Generator;
17+
use Geocoder\Model\Coordinates;
1618
use Geocoder\Plugin\Plugin\CachePlugin;
1719
use Geocoder\Query\GeocodeQuery;
1820
use Geocoder\Query\Query;
21+
use Geocoder\Query\ReverseQuery;
1922
use PHPUnit\Framework\TestCase;
2023

2124
class CachePluginTest extends TestCase
@@ -50,18 +53,31 @@ public function testPluginMiss()
5053
$this->assertEquals('result', $plugin->handleQuery($query, $next, $first));
5154
}
5255

53-
public function testPluginHit()
56+
public function getQueryProvider(): Generator
5457
{
5558
$query = GeocodeQuery::create('foo');
56-
$queryString = sha1($query->__toString());
59+
$key = sha1($query->__toString());
60+
yield [$query, $key];
61+
62+
$query = ReverseQuery::create(new Coordinates(12.123456, 12.123456));
63+
$lessPreciseQuery = $query->withCoordinates(new Coordinates(12.1235, 12.1235));
64+
$key = sha1((string) $lessPreciseQuery);
65+
yield [$query, $key];
66+
}
67+
68+
/**
69+
* @dataProvider getQueryProvider
70+
*/
71+
public function testPluginHit(Query $query, string $key)
72+
{
5773
$cache = $this->getMockBuilder(VoidCachePool::class)
5874
->disableOriginalConstructor()
5975
->setMethods(['get', 'set'])
6076
->getMock();
6177

6278
$cache->expects($this->once())
6379
->method('get')
64-
->with('v4'.$queryString)
80+
->with('v4'.$key)
6581
->willReturn('result');
6682
$cache->expects($this->never())->method('set');
6783

@@ -72,7 +88,7 @@ public function testPluginHit()
7288
$this->fail('Plugin not call $next on cache hit');
7389
};
7490

75-
$plugin = new CachePlugin($cache);
91+
$plugin = new CachePlugin($cache, 0, 4);
7692
$this->assertEquals('result', $plugin->handleQuery($query, $next, $first));
7793
}
7894
}

0 commit comments

Comments
 (0)