Skip to content

Commit d560be7

Browse files
authored
Merge pull request #22 from InteractionDesignFoundation/cache-prefix
Allow specifying cache prefix
2 parents 94f00a4 + e43eaa6 commit d560be7

File tree

8 files changed

+54
-41
lines changed

8 files changed

+54
-41
lines changed

config/geoip.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@
137137

138138
'cache_expires' => 30,
139139

140+
/*
141+
|--------------------------------------------------------------------------
142+
| Cache Prefix
143+
|--------------------------------------------------------------------------
144+
|
145+
| Prefix used for cache keys (in addition to globally configured prefix).
146+
|
147+
*/
148+
149+
'cache_prefix' => 'geoip:',
150+
140151
/*
141152
|--------------------------------------------------------------------------
142153
| Default Location

psalm-baseline.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
</PropertyNotSetInConstructor>
6666
</file>
6767
<file src="src/GeoIP.php">
68+
<DeprecatedMethod>
69+
<code><![CDATA[setPrefix]]></code>
70+
</DeprecatedMethod>
6871
<DocblockTypeContradiction>
6972
<code><![CDATA[$this->currencies === null]]></code>
7073
<code><![CDATA[$this->service === null]]></code>

src/Cache.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class Cache
2020
*/
2121
protected $expires;
2222

23+
/** Cache prefix */
24+
protected string $prefix = '';
25+
2326
/**
2427
* Create a new cache instance.
2528
*
@@ -33,6 +36,15 @@ public function __construct(CacheManager $cache, $tags, $expires = 30)
3336
$this->expires = $expires;
3437
}
3538

39+
/**
40+
* @internal A hack to support prefixes. Added as a setter to avoid BC breaks.
41+
* @deprecated Will be removed in v2.0
42+
*/
43+
public function setPrefix(?string $prefix = null): void
44+
{
45+
$this->prefix = (string) $prefix;
46+
}
47+
3648
/**
3749
* Get an item from the cache.
3850
*
@@ -42,7 +54,7 @@ public function __construct(CacheManager $cache, $tags, $expires = 30)
4254
*/
4355
public function get($name)
4456
{
45-
$value = $this->cache->get($name);
57+
$value = $this->cache->get($this->prefix.$name);
4658

4759
return is_array($value)
4860
? new Location($value)
@@ -59,7 +71,7 @@ public function get($name)
5971
*/
6072
public function set($name, Location $location)
6173
{
62-
return $this->cache->put($name, $location->toArray(), $this->expires);
74+
return $this->cache->put($this->prefix.$name, $location->toArray(), $this->expires);
6375
}
6476

6577
/**

src/GeoIP.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function __construct(array $config, CacheManager $cache)
9090
$this->config('cache_tags'),
9191
$this->config('cache_expires', 30)
9292
);
93+
$this->cache->setPrefix((string) $this->config('cache_prefix'));
9394

9495
// Set custom default location
9596
$this->default_location = array_merge(

tests/CacheTest.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace InteractionDesignFoundation\GeoIP\Tests;
44

5+
use Illuminate\Cache\CacheManager;
56
use Mockery;
67

78
class CacheTest extends TestCase
89
{
9-
/**
10-
* @test
11-
*/
10+
/** @test */
1211
public function shouldReturnValidLocation()
1312
{
1413
$data = [
@@ -18,14 +17,15 @@ public function shouldReturnValidLocation()
1817
'lon' => -72.92,
1918
];
2019

21-
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager')
20+
$cacheMock = Mockery::mock(CacheManager::class)
2221
->shouldAllowMockingProtectedMethods();
2322

2423
$cacheMock->shouldReceive('get')
2524
->with($data['ip'])
2625
->andReturn($data);
2726

2827
$geo_ip = $this->makeGeoIP([], $cacheMock);
28+
$geo_ip->getCache()->setPrefix('');
2929

3030
$location = $geo_ip->getCache()->get($data['ip']);
3131

@@ -34,15 +34,14 @@ public function shouldReturnValidLocation()
3434
$this->assertEquals($location->default, false);
3535
}
3636

37-
/**
38-
* @test
39-
*/
37+
/** @test */
4038
public function shouldReturnInvalidLocation()
4139
{
42-
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager')
40+
$cacheMock = Mockery::mock(CacheManager::class)
4341
->shouldAllowMockingProtectedMethods();
4442

4543
$geo_ip = $this->makeGeoIP([], $cacheMock);
44+
$geo_ip->getCache()->setPrefix('');
4645

4746
$cacheMock->shouldReceive('get')
4847
->with('81.2.69.142')
@@ -55,9 +54,7 @@ public function shouldReturnInvalidLocation()
5554
$this->assertEquals($geo_ip->getCache()->get('81.2.69.142'), null);
5655
}
5756

58-
/**
59-
* @test
60-
*/
57+
/** @test */
6158
public function shouldSetLocation()
6259
{
6360
$location = new \InteractionDesignFoundation\GeoIP\Location([
@@ -67,10 +64,11 @@ public function shouldSetLocation()
6764
'lon' => -72.92,
6865
]);
6966

70-
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager')
67+
$cacheMock = Mockery::mock(CacheManager::class)
7168
->shouldAllowMockingProtectedMethods();
7269

7370
$geo_ip = $this->makeGeoIP([], $cacheMock);
71+
$geo_ip->getCache()->setPrefix('');
7472

7573
$cacheMock->shouldReceive('put')
7674
->withArgs(['81.2.69.142', $location->toArray(), $geo_ip->config('cache_expires')])
@@ -83,12 +81,10 @@ public function shouldSetLocation()
8381
$this->assertEquals($geo_ip->getCache()->set('81.2.69.142', $location), null);
8482
}
8583

86-
/**
87-
* @test
88-
*/
84+
/** @test */
8985
public function shouldFlushLocations()
9086
{
91-
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager')
87+
$cacheMock = Mockery::mock(CacheManager::class)
9288
->shouldAllowMockingProtectedMethods();
9389

9490
$geo_ip = $this->makeGeoIP([], $cacheMock);

tests/GeoIPTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66

77
class GeoIPTest extends TestCase
88
{
9-
/**
10-
* @test
11-
*/
9+
/** @test */
1210
public function shouldGetUSDCurrency()
1311
{
1412
$geo_ip = $this->makeGeoIP();
1513

1614
$this->assertEquals($geo_ip->getCurrency('US'), 'USD');
1715
}
1816

19-
/**
20-
* @test
21-
*/
17+
/** @test */
2218
public function testGetService()
2319
{
2420
$geo_ip = $this->makeGeoIP([
@@ -32,9 +28,7 @@ public function testGetService()
3228
$this->assertInstanceOf(\InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface::class, $geo_ip->getService());
3329
}
3430

35-
/**
36-
* @test
37-
*/
31+
/** @test */
3832
public function testGetCache()
3933
{
4034
$geo_ip = $this->makeGeoIP();

tests/Services/MaxMindDatabaseTest.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@
66

77
class MaxMindDatabaseTest extends TestCase
88
{
9-
/**
10-
* @test
11-
*/
9+
/** @test */
1210
public function shouldReturnConfigValue()
1311
{
1412
list($service, $config) = $this->getService();
1513

1614
$this->assertEquals($service->config('database_path'), $config['database_path']);
1715
}
1816

19-
/**
20-
* @test
21-
*/
17+
/** @test */
2218
public function shouldReturnValidLocation()
2319
{
24-
list($service, $config) = $this->getService();
20+
[$service] = $this->getService();
2521

2622
$location = $service->locate('81.2.69.142');
2723

@@ -30,12 +26,10 @@ public function shouldReturnValidLocation()
3026
$this->assertEquals($location->default, false);
3127
}
3228

33-
/**
34-
* @test
35-
*/
36-
public function shouldReturnInvalidLocation()
29+
/** @test */
30+
public function shouldReturnInvalidLocationForSpecialAddresses()
3731
{
38-
list($service, $config) = $this->getService();
32+
[$service] = $this->getService();
3933

4034
try {
4135
$location = $service->locate('1.1.1.1');
@@ -46,7 +40,8 @@ public function shouldReturnInvalidLocation()
4640
}
4741
}
4842

49-
protected function getService()
43+
/** @return list{\InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface, array<string, mixed>} */
44+
protected function getService(): array
5045
{
5146
$config = $this->getConfig()['services']['maxmind_database'];
5247

tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace InteractionDesignFoundation\GeoIP\Tests;
44

5+
use Illuminate\Cache\CacheManager;
56
use Mockery;
67
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
78

@@ -21,7 +22,7 @@ public function tearDown(): void
2122

2223
protected function makeGeoIP(array $config = [], $cacheMock = null)
2324
{
24-
$cacheMock = $cacheMock ?: Mockery::mock('Illuminate\Cache\CacheManager');
25+
$cacheMock = $cacheMock ?: Mockery::mock(CacheManager::class);
2526

2627
$config = array_merge($this->getConfig(), $config);
2728

0 commit comments

Comments
 (0)