From 463ef1ec543674c354713eed60c7ef24c8745006 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Wed, 5 Jul 2023 19:09:04 +0500 Subject: [PATCH 1/9] adding symfony cache --- composer.json | 2 +- src/cache/DefaultCache.php | 33 +++++++++++++++++++++++---------- tests/DefaultCacheTest.php | 11 ++++++----- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 3db17d3..c5a1fbf 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "php": ">=8.0", "ext-json": "*", "guzzlehttp/guzzle": "^6.3||^7.0", - "sabre/cache": "^1.0", + "symfony/cache": "^6.3", "symfony/http-foundation": "^6.2" }, "require-dev": { diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index 3dd53b0..899c699 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -2,6 +2,10 @@ namespace ipinfo\ipinfo\cache; +use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; + /** * Default implementation of the CacheInterface. Provides in-memory caching. */ @@ -14,9 +18,9 @@ class DefaultCache implements CacheInterface private $cache; private $element_queue; - public function __construct(int $maxsize, int $ttl) + public function __construct(FilesystemAdapter $cache, int $maxsize, int $ttl) { - $this->cache = new \Sabre\Cache\Memory(); + $this->cache = $cache; $this->element_queue = array(); $this->maxsize = $maxsize; $this->ttl = $ttl; @@ -29,9 +33,15 @@ public function __construct(int $maxsize, int $ttl) */ public function has(string $name) { - return $this->cache->has($name); + return $this->cache->get($name); + } + + public function delete(string $name): bool + { + return $this->$cache->delete($name); } + /** * Set the IP address key to the specified value. * @param string $ip_address IP address to cache data for. @@ -39,11 +49,14 @@ public function has(string $name) */ public function set(string $name, $value) { - if (!$this->cache->has($name)) { - $this->element_queue[] = $name; - } - - $this->cache->set($name, $value, $this->ttl); + // The callable will only be executed on a cache miss. + $this->$cache->get($name, function (ItemInterface $item): string { + $item->expiresAfter($this->ttl); + // On chache miss update queue. + $this->$element_queue[] = $name; + + return $value; + }); $this->manageSize(); } @@ -53,7 +66,7 @@ public function set(string $name, $value) * @param string $ip_address IP address to lookup in cache. * @return mixed IP address data. */ - public function get(string $name) + public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed { return $this->cache->get($name); } @@ -66,7 +79,7 @@ private function manageSize() $overflow = count($this->element_queue) - $this->maxsize; if ($overflow > 0) { foreach (array_slice($this->element_queue, 0, $overflow) as $name) { - if ($this->cache->has($name)) { + if ($this->cache->get($name)) { $this->cache->delete($name); } } diff --git a/tests/DefaultCacheTest.php b/tests/DefaultCacheTest.php index 1e45563..ddd120f 100644 --- a/tests/DefaultCacheTest.php +++ b/tests/DefaultCacheTest.php @@ -3,13 +3,14 @@ namespace ipinfo\ipinfo\tests; use ipinfo\ipinfo\cache\DefaultCache; +use Symfony\Component\Cache\Adapter\FilesystemAdapter; use PHPUnit\Framework\TestCase; class DefaultCacheTest extends TestCase { public function testHasValue() { - $cache = new DefaultCache($maxsize = 4, $ttl = 2); + $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2); $key1 = 'test'; $value1 = 'obama'; $cache->set($key1, $value1); @@ -24,7 +25,7 @@ public function testHasValue() public function testDoesNotHaveValue() { - $cache = new DefaultCache($maxsize = 4, $ttl = 2); + $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2); $key = 'test'; $this->assertFalse($cache->has($key)); @@ -32,7 +33,7 @@ public function testDoesNotHaveValue() public function testGetValue() { - $cache = new DefaultCache($maxsize = 4, $ttl = 2); + $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2); $key1 = 'test'; $value1 = 'obama'; $cache->set($key1, $value1); @@ -47,7 +48,7 @@ public function testGetValue() public function testMaxSizeExceeded() { - $cache = new DefaultCache($maxsize = 2, $ttl = 2); + $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 2, $ttl = 2); $key1 = 'test'; $value1 = 'obama'; @@ -70,7 +71,7 @@ public function testMaxSizeExceeded() public function testTimeToLiveExceeded() { - $cache = new DefaultCache($maxsize = 2, $ttl = 1); + $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 2, $ttl = 1); $key = 'test'; $value = 'obama'; From 69c3e50e06316e70de1492c89fd83ea0d322383a Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Thu, 6 Jul 2023 09:50:00 +0500 Subject: [PATCH 2/9] has func fix and cache initialization --- src/cache/DefaultCache.php | 11 +++++------ tests/DefaultCacheTest.php | 13 ++++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index 899c699..9b8bccb 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -18,9 +18,9 @@ class DefaultCache implements CacheInterface private $cache; private $element_queue; - public function __construct(FilesystemAdapter $cache, int $maxsize, int $ttl) + public function __construct(int $maxsize, int $ttl) { - $this->cache = $cache; + $this->cache = new FilesystemAdapter(); $this->element_queue = array(); $this->maxsize = $maxsize; $this->ttl = $ttl; @@ -31,9 +31,9 @@ public function __construct(FilesystemAdapter $cache, int $maxsize, int $ttl) * @param string $ip_address IP address to lookup. * @return boolean Is the IP address data in the cache. */ - public function has(string $name) + public function has(string $name): bool { - return $this->cache->get($name); + return $this->cache->hasItem($name); } public function delete(string $name): bool @@ -41,7 +41,6 @@ public function delete(string $name): bool return $this->$cache->delete($name); } - /** * Set the IP address key to the specified value. * @param string $ip_address IP address to cache data for. @@ -52,7 +51,7 @@ public function set(string $name, $value) // The callable will only be executed on a cache miss. $this->$cache->get($name, function (ItemInterface $item): string { $item->expiresAfter($this->ttl); - // On chache miss update queue. + // On cache miss update queue. $this->$element_queue[] = $name; return $value; diff --git a/tests/DefaultCacheTest.php b/tests/DefaultCacheTest.php index ddd120f..edd1168 100644 --- a/tests/DefaultCacheTest.php +++ b/tests/DefaultCacheTest.php @@ -10,7 +10,7 @@ class DefaultCacheTest extends TestCase { public function testHasValue() { - $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2); + $cache = new DefaultCache($maxsize = 4, $ttl = 2); $key1 = 'test'; $value1 = 'obama'; $cache->set($key1, $value1); @@ -19,13 +19,16 @@ public function testHasValue() $value2 = 'mccain'; $cache->set($key2, $value2); + var_dump($cache->get($key1)); + var_dump($cache->get($key2)); + $this->assertTrue($cache->has($key1)); $this->assertTrue($cache->has($key2)); } public function testDoesNotHaveValue() { - $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2); + $cache = new DefaultCache($maxsize = 4, $ttl = 2); $key = 'test'; $this->assertFalse($cache->has($key)); @@ -33,7 +36,7 @@ public function testDoesNotHaveValue() public function testGetValue() { - $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 4, $ttl = 2); + $cache = new DefaultCache($maxsize = 4, $ttl = 2); $key1 = 'test'; $value1 = 'obama'; $cache->set($key1, $value1); @@ -48,7 +51,7 @@ public function testGetValue() public function testMaxSizeExceeded() { - $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 2, $ttl = 2); + $cache = new DefaultCache($maxsize = 2, $ttl = 2); $key1 = 'test'; $value1 = 'obama'; @@ -71,7 +74,7 @@ public function testMaxSizeExceeded() public function testTimeToLiveExceeded() { - $cache = new DefaultCache(new FilesystemAdapter(), $maxsize = 2, $ttl = 1); + $cache = new DefaultCache($maxsize = 2, $ttl = 1); $key = 'test'; $value = 'obama'; From 6f586388f21f82f0d8214d75fc2218eafdc45677 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Thu, 6 Jul 2023 13:44:37 +0500 Subject: [PATCH 3/9] updating interface --- src/cache/DefaultCache.php | 34 ++++++++++++++++------------------ tests/DefaultCacheTest.php | 14 ++++++++++---- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index 9b8bccb..d17c590 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -3,7 +3,6 @@ namespace ipinfo\ipinfo\cache; use Symfony\Component\Cache\Adapter\FilesystemAdapter; -use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; /** @@ -36,11 +35,6 @@ public function has(string $name): bool return $this->cache->hasItem($name); } - public function delete(string $name): bool - { - return $this->$cache->delete($name); - } - /** * Set the IP address key to the specified value. * @param string $ip_address IP address to cache data for. @@ -48,16 +42,12 @@ public function delete(string $name): bool */ public function set(string $name, $value) { - // The callable will only be executed on a cache miss. - $this->$cache->get($name, function (ItemInterface $item): string { - $item->expiresAfter($this->ttl); - // On cache miss update queue. - $this->$element_queue[] = $name; - - return $value; + $this->cache->get($name, function (ItemInterface $item) use ($name, $value) { + $item->set($value)->expiresAfter($this->ttl); + $this->element_queue[] = $name; + $this->manageSize(); + return $item->get(); }); - - $this->manageSize(); } /** @@ -65,9 +55,17 @@ public function set(string $name, $value) * @param string $ip_address IP address to lookup in cache. * @return mixed IP address data. */ - public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed + public function get(string $name) { - return $this->cache->get($name); + // return $this->cache->get($name); + // return $this->cache->get($name, function (ItemInterface $item) { + // throw new \Exception('Cache item not found'); + // }); + if ($this->cache->hasItem($name)) { + return $this->cache->getItem($name)->get(); + } + + throw new \Exception("Cache item not found for key: $name"); } /** @@ -78,7 +76,7 @@ private function manageSize() $overflow = count($this->element_queue) - $this->maxsize; if ($overflow > 0) { foreach (array_slice($this->element_queue, 0, $overflow) as $name) { - if ($this->cache->get($name)) { + if ($this->cache->hasItem($name)) { $this->cache->delete($name); } } diff --git a/tests/DefaultCacheTest.php b/tests/DefaultCacheTest.php index edd1168..0674f69 100644 --- a/tests/DefaultCacheTest.php +++ b/tests/DefaultCacheTest.php @@ -19,9 +19,6 @@ public function testHasValue() $value2 = 'mccain'; $cache->set($key2, $value2); - var_dump($cache->get($key1)); - var_dump($cache->get($key2)); - $this->assertTrue($cache->has($key1)); $this->assertTrue($cache->has($key2)); } @@ -29,7 +26,7 @@ public function testHasValue() public function testDoesNotHaveValue() { $cache = new DefaultCache($maxsize = 4, $ttl = 2); - $key = 'test'; + $key = 'ink'; $this->assertFalse($cache->has($key)); } @@ -45,6 +42,15 @@ public function testGetValue() $value2 = 'mccain'; $cache->set($key2, $value2); + // $retrievedValue1 = $cache->get($key1); + // $retrievedValue2 = $cache->get($key2); + + // var_dump($retrievedValue1->get()); + // var_dump($retrievedValue2->get()); + + // $this->assertEquals($value1, $retrievedValue1); + // $this->assertEquals($value2, $retrievedValue2); + $this->assertEquals($value1, $cache->get($key1)); $this->assertEquals($value2, $cache->get($key2)); } From 7ca74c9e8f987ad1d4372083db7295e3392d05b1 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Thu, 6 Jul 2023 14:44:44 +0500 Subject: [PATCH 4/9] fixed issues --- src/cache/DefaultCache.php | 20 ++++++++++---------- tests/DefaultCacheTest.php | 9 --------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index d17c590..210a903 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -42,12 +42,16 @@ public function has(string $name): bool */ public function set(string $name, $value) { - $this->cache->get($name, function (ItemInterface $item) use ($name, $value) { - $item->set($value)->expiresAfter($this->ttl); + if (!in_array($name, $this->element_queue)) { $this->element_queue[] = $name; - $this->manageSize(); + } + + $this->cache->get($name, function (ItemInterface $item) use ($value) { + $item->set($value)->expiresAfter($this->ttl); return $item->get(); }); + + $this->manageSize(); } /** @@ -57,15 +61,11 @@ public function set(string $name, $value) */ public function get(string $name) { - // return $this->cache->get($name); - // return $this->cache->get($name, function (ItemInterface $item) { - // throw new \Exception('Cache item not found'); - // }); if ($this->cache->hasItem($name)) { return $this->cache->getItem($name)->get(); } - - throw new \Exception("Cache item not found for key: $name"); + + return null; } /** @@ -76,7 +76,7 @@ private function manageSize() $overflow = count($this->element_queue) - $this->maxsize; if ($overflow > 0) { foreach (array_slice($this->element_queue, 0, $overflow) as $name) { - if ($this->cache->hasItem($name)) { + if ($this->has($name)) { $this->cache->delete($name); } } diff --git a/tests/DefaultCacheTest.php b/tests/DefaultCacheTest.php index 0674f69..8d6a977 100644 --- a/tests/DefaultCacheTest.php +++ b/tests/DefaultCacheTest.php @@ -42,15 +42,6 @@ public function testGetValue() $value2 = 'mccain'; $cache->set($key2, $value2); - // $retrievedValue1 = $cache->get($key1); - // $retrievedValue2 = $cache->get($key2); - - // var_dump($retrievedValue1->get()); - // var_dump($retrievedValue2->get()); - - // $this->assertEquals($value1, $retrievedValue1); - // $this->assertEquals($value2, $retrievedValue2); - $this->assertEquals($value1, $cache->get($key1)); $this->assertEquals($value2, $cache->get($key2)); } From b0293aeb735e068d104dacb001f599c6fc97f415 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Thu, 6 Jul 2023 15:15:18 +0500 Subject: [PATCH 5/9] updating to in-memory cache --- src/cache/DefaultCache.php | 4 ++-- tests/DefaultCacheTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index 210a903..9047071 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -2,7 +2,7 @@ namespace ipinfo\ipinfo\cache; -use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Contracts\Cache\ItemInterface; /** @@ -19,7 +19,7 @@ class DefaultCache implements CacheInterface public function __construct(int $maxsize, int $ttl) { - $this->cache = new FilesystemAdapter(); + $this->cache = new ArrayAdapter(); $this->element_queue = array(); $this->maxsize = $maxsize; $this->ttl = $ttl; diff --git a/tests/DefaultCacheTest.php b/tests/DefaultCacheTest.php index 8d6a977..81b170d 100644 --- a/tests/DefaultCacheTest.php +++ b/tests/DefaultCacheTest.php @@ -26,7 +26,7 @@ public function testHasValue() public function testDoesNotHaveValue() { $cache = new DefaultCache($maxsize = 4, $ttl = 2); - $key = 'ink'; + $key = 'test'; $this->assertFalse($cache->has($key)); } From cd40a7bdad0ca036fc5081f63bf652fa6642a391 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Thu, 6 Jul 2023 15:16:41 +0500 Subject: [PATCH 6/9] updating get func --- src/cache/DefaultCache.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index 9047071..22ba77f 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -61,11 +61,7 @@ public function set(string $name, $value) */ public function get(string $name) { - if ($this->cache->hasItem($name)) { - return $this->cache->getItem($name)->get(); - } - - return null; + return $this->cache->getItem($name)->get(); } /** From 150eca28c2902f2809ad72abd75eeb233e10f5fc Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Thu, 6 Jul 2023 17:42:13 +0500 Subject: [PATCH 7/9] updated check --- src/cache/DefaultCache.php | 2 +- tests/DefaultCacheTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cache/DefaultCache.php b/src/cache/DefaultCache.php index 22ba77f..fe89d49 100644 --- a/src/cache/DefaultCache.php +++ b/src/cache/DefaultCache.php @@ -42,7 +42,7 @@ public function has(string $name): bool */ public function set(string $name, $value) { - if (!in_array($name, $this->element_queue)) { + if (!$this->cache->hasItem($name)) { $this->element_queue[] = $name; } diff --git a/tests/DefaultCacheTest.php b/tests/DefaultCacheTest.php index 81b170d..1e45563 100644 --- a/tests/DefaultCacheTest.php +++ b/tests/DefaultCacheTest.php @@ -3,7 +3,6 @@ namespace ipinfo\ipinfo\tests; use ipinfo\ipinfo\cache\DefaultCache; -use Symfony\Component\Cache\Adapter\FilesystemAdapter; use PHPUnit\Framework\TestCase; class DefaultCacheTest extends TestCase From 40ba9884b3216c254769c1c168c34d6bc817eb96 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Mon, 10 Jul 2023 11:06:49 +0500 Subject: [PATCH 8/9] updating phpunit version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c5a1fbf..cc556a8 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "symfony/http-foundation": "^6.2" }, "require-dev": { - "phpunit/phpunit": ">=8 <10", + "phpunit/phpunit": "^9.5.20", "squizlabs/php_codesniffer": "^3.5.8" }, "autoload": { From 06afa8a157a4969409699b5cfdd35ef9000129f0 Mon Sep 17 00:00:00 2001 From: Samuel Gill Date: Mon, 10 Jul 2023 11:20:54 +0500 Subject: [PATCH 9/9] updating test array for batch test --- tests/IPinfoTest.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/IPinfoTest.php b/tests/IPinfoTest.php index 15c9366..d263576 100644 --- a/tests/IPinfoTest.php +++ b/tests/IPinfoTest.php @@ -194,11 +194,12 @@ public function testGetBatchDetails() $this->assertEquals($res['8.8.8.8/hostname'], 'dns.google'); $this->assertEquals($res['4.4.4.4'], [ 'ip' => "4.4.4.4", - 'city' => "Taipei", - 'region' => "Taiwan", - 'country' => "TW", - 'loc' => "25.0478,121.5319", - 'timezone' => "Asia/Taipei", + 'city' => "Paris", + 'region' => "Île-de-France", + 'country' => "FR", + 'loc' => "48.8534,2.3488", + 'postal' => "75000", + 'timezone' => "Europe/Paris", 'asn' => [ 'asn' => "AS3356", 'name' => "Level 3 Parent, LLC", @@ -229,13 +230,13 @@ public function testGetBatchDetails() ], 'domains' => [ 'ip' => "4.4.4.4", - 'total' => 124, + 'total' => 126, 'domains' => [ 'ncrsaas.com', - 'snowdongliders.com', - 'codecrunch.se', - 'tetrauni.com', - 'reidaidns.com' + 'datacenter-team.com', + 'ddufjfjantlpok.tk', + 'salvafabregas.com', + 'bfgroup.kz' ] ], 'org' => 'AS3356 Level 3 Parent, LLC',