From 8eb279fd288d1345bf966e9cdc5bdad9fca23109 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 9 Aug 2016 13:32:33 +0200 Subject: [PATCH] Updated UrlShortener to namespace the cache entries --- module/Core/src/Service/UrlShortener.php | 7 ++++--- module/Core/test/Service/UrlShortenerTest.php | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index 30b700cd..ed87b604 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -148,9 +148,10 @@ class UrlShortener implements UrlShortenerInterface */ public function shortCodeToUrl($shortCode) { + $cacheKey = sprintf('%s_longUrl', $shortCode); // Check if the short code => URL map is already cached - if ($this->cache->contains($shortCode)) { - return $this->cache->fetch($shortCode); + if ($this->cache->contains($cacheKey)) { + return $this->cache->fetch($cacheKey); } // Validate short code format @@ -165,7 +166,7 @@ class UrlShortener implements UrlShortenerInterface // Cache the shortcode if (isset($shortUrl)) { $url = $shortUrl->getOriginalUrl(); - $this->cache->save($shortCode, $url); + $this->cache->save($cacheKey, $url); return $url; } diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 90886016..4cf7da1d 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -129,10 +129,10 @@ class UrlShortenerTest extends TestCase $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); - $this->assertFalse($this->cache->contains($shortCode)); + $this->assertFalse($this->cache->contains($shortCode . '_longUrl')); $url = $this->urlShortener->shortCodeToUrl($shortCode); $this->assertEquals($shortUrl->getOriginalUrl(), $url); - $this->assertTrue($this->cache->contains($shortCode)); + $this->assertTrue($this->cache->contains($shortCode . '_longUrl')); } /** @@ -151,7 +151,7 @@ class UrlShortenerTest extends TestCase { $shortCode = '12C1c'; $expectedUrl = 'expected_url'; - $this->cache->save($shortCode, $expectedUrl); + $this->cache->save($shortCode . '_longUrl', $expectedUrl); $this->em->getRepository(ShortUrl::class)->willReturn(null)->shouldBeCalledTimes(0); $url = $this->urlShortener->shortCodeToUrl($shortCode);