From 8c8a8921549e9acaa1d0c5c3b326162404191c1a Mon Sep 17 00:00:00 2001 From: Uman Shahzad Date: Thu, 22 Apr 2021 16:45:00 +0500 Subject: [PATCH] impl versioned cache keys. --- src/IPinfo.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/IPinfo.php b/src/IPinfo.php index 6957224..af76b82 100644 --- a/src/IPinfo.php +++ b/src/IPinfo.php @@ -15,6 +15,7 @@ class IPinfo const API_URL = 'https://ipinfo.io'; const CACHE_MAXSIZE = 4096; const CACHE_TTL = 86400; // 24 hours as seconds + const CACHE_KEY_VSN = '1'; // update when cache vals change for same key. const COUNTRIES_FILE_DEFAULT = __DIR__ . '/countries.json'; const REQUEST_TYPE_GET = 'GET'; const STATUS_CODE_QUOTA_EXCEEDED = 429; @@ -97,8 +98,9 @@ public function formatDetailsObject($details = []) */ public function getRequestDetails(string $ip_address) { - if ($this->cache->has($ip_address)) { - return $this->cache->get($ip_address); + $cachedRes = $this->cache->get($this->cacheKey($ip_address)); + if ($cachedRes != null) { + return $cachedRes; } $url = self::API_URL; @@ -127,7 +129,7 @@ public function getRequestDetails(string $ip_address) } $raw_details = json_decode($response->getBody(), true); - $this->cache->set($ip_address, $raw_details); + $this->cache->set($this->cacheKey($ip_address), $raw_details); return $raw_details; } @@ -160,4 +162,14 @@ private function readCountryNames($countries_file) $file_contents = file_get_contents($countries_file); return json_decode($file_contents, true); } + + /** + * Returns a versioned cache key given a user-input key. + * @param string key to transform into a versioned cache key. + * @return string the versioned cache key. + */ + private function cacheKey($k) + { + return sprintf('%s:%s', $k, self::CACHE_KEY_VSN); + } }