From 04e96e0e0344a5a27cff69f8590b6e8d74f91963 Mon Sep 17 00:00:00 2001 From: Oliver Lumby Date: Wed, 29 Nov 2023 00:44:17 +0100 Subject: [PATCH 1/3] use simple key value store --- src/Agent.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Agent.php b/src/Agent.php index 19b409582..8420de5c5 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -50,6 +50,13 @@ class Agent extends MobileDetect 'WeChat' => 'MicroMessenger', ]; + /** + * Key value store for resolved strings + * + * @var array + */ + protected $store = []; + /** * Get the platform name from the User Agent. * @@ -134,12 +141,12 @@ protected function retrieveUsingCacheOrResolve(string $key, Closure $callback) try { $cacheKey = $this->createCacheKey($key); - if (! is_null($cacheItem = $this->cache->get($cacheKey))) { - return $cacheItem->get(); + if (! is_null($cacheItem = $this->store[$cacheKey] ?? null)) { + return $cacheItem; } return tap(call_user_func($callback), function ($result) use ($cacheKey) { - $this->cache->set($cacheKey, $result); + $this->store[$cacheKey] = $result; }); } catch (CacheException $e) { throw new MobileDetectException("Cache problem in for {$key}: {$e->getMessage()}"); From 4e6af88aa7f678a23699104bc951e859ee4360d2 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 29 Nov 2023 11:44:01 +0800 Subject: [PATCH 2/3] Add failing tests for PR #1412 Signed-off-by: Mior Muhammad Zaki --- tests/AgentTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/AgentTest.php b/tests/AgentTest.php index 0cdea53d3..61f24b12a 100644 --- a/tests/AgentTest.php +++ b/tests/AgentTest.php @@ -22,7 +22,10 @@ public function testOperatingSystems($userAgent, $platform) $agent = new Agent(); $agent->setUserAgent($userAgent); - $this->assertEquals($platform, $agent->platform()); + $this->assertSame($platform, $agent->platform()); + + // Test cached value return the same output. + $this->assertSame($platform, $agent->platform()); } public static function operatingSystemsDataProvider() @@ -48,7 +51,10 @@ public function testBrowsers($userAgent, $browser) $agent = new Agent(); $agent->setUserAgent($userAgent); - $this->assertEquals($browser, $agent->browser()); + $this->assertSame($browser, $agent->browser()); + + // Test cached value return the same output. + $this->assertSame($browser, $agent->browser()); } public static function browsersDataProvider() From df89ff0436f5067d743327f81d2858095d385746 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 29 Nov 2023 12:38:39 +0800 Subject: [PATCH 3/3] wip Signed-off-by: Mior Muhammad Zaki --- src/Agent.php | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/Agent.php b/src/Agent.php index 8420de5c5..97c7642ad 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -3,8 +3,6 @@ namespace Laravel\Jetstream; use Closure; -use Detection\Cache\CacheException; -use Detection\Exception\MobileDetectException; use Detection\MobileDetect; /** @@ -51,9 +49,9 @@ class Agent extends MobileDetect ]; /** - * Key value store for resolved strings + * Key value store for resolved strings. * - * @var array + * @var array */ protected $store = []; @@ -133,24 +131,18 @@ protected function findDetectionRulesAgainstUserAgent(array $rules) * @param string $key * @param \Closure():mixed $callback * @return mixed - * - * @throws \Detection\Exception\MobileDetectException */ protected function retrieveUsingCacheOrResolve(string $key, Closure $callback) { - try { - $cacheKey = $this->createCacheKey($key); - - if (! is_null($cacheItem = $this->store[$cacheKey] ?? null)) { - return $cacheItem; - } + $cacheKey = $this->createCacheKey($key); - return tap(call_user_func($callback), function ($result) use ($cacheKey) { - $this->store[$cacheKey] = $result; - }); - } catch (CacheException $e) { - throw new MobileDetectException("Cache problem in for {$key}: {$e->getMessage()}"); + if (! is_null($cacheItem = $this->store[$cacheKey] ?? null)) { + return $cacheItem; } + + return tap(call_user_func($callback), function ($result) use ($cacheKey) { + $this->store[$cacheKey] = $result; + }); } /**