mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Added cache adapter to the UrlShortener service to cache shortcode-url maps
This commit is contained in:
@@ -18,14 +18,14 @@ class RedirectAction implements MiddlewareInterface
|
||||
*/
|
||||
private $urlShortener;
|
||||
/**
|
||||
* @var VisitsTracker|VisitsTrackerInterface
|
||||
* @var VisitsTrackerInterface
|
||||
*/
|
||||
private $visitTracker;
|
||||
|
||||
/**
|
||||
* RedirectMiddleware constructor.
|
||||
* @param UrlShortenerInterface|UrlShortener $urlShortener
|
||||
* @param VisitsTrackerInterface|VisitsTracker $visitTracker
|
||||
* @param UrlShortenerInterface $urlShortener
|
||||
* @param VisitsTrackerInterface $visitTracker
|
||||
*
|
||||
* @Inject({UrlShortener::class, VisitsTracker::class})
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\ORMException;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
@@ -28,23 +29,30 @@ class UrlShortener implements UrlShortenerInterface
|
||||
* @var string
|
||||
*/
|
||||
private $chars;
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* UrlShortener constructor.
|
||||
* @param ClientInterface $httpClient
|
||||
* @param EntityManagerInterface $em
|
||||
* @param Cache $cache
|
||||
* @param string $chars
|
||||
*
|
||||
* @Inject({"httpClient", "em", "config.url_shortener.shortcode_chars"})
|
||||
* @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars"})
|
||||
*/
|
||||
public function __construct(
|
||||
ClientInterface $httpClient,
|
||||
EntityManagerInterface $em,
|
||||
Cache $cache,
|
||||
$chars = self::DEFAULT_CHARS
|
||||
) {
|
||||
$this->httpClient = $httpClient;
|
||||
$this->em = $em;
|
||||
$this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,6 +148,11 @@ class UrlShortener implements UrlShortenerInterface
|
||||
*/
|
||||
public function shortCodeToUrl($shortCode)
|
||||
{
|
||||
// Check if the short code => URL map is already cached
|
||||
if ($this->cache->contains($shortCode)) {
|
||||
return $this->cache->fetch($shortCode);
|
||||
}
|
||||
|
||||
// Validate short code format
|
||||
if (! preg_match('|[' . $this->chars . "]+|", $shortCode)) {
|
||||
throw InvalidShortCodeException::fromShortCode($shortCode, $this->chars);
|
||||
@@ -149,6 +162,13 @@ class UrlShortener implements UrlShortenerInterface
|
||||
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
||||
'shortCode' => $shortCode,
|
||||
]);
|
||||
return isset($shortUrl) ? $shortUrl->getOriginalUrl() : null;
|
||||
// Cache the shortcode
|
||||
if (isset($shortUrl)) {
|
||||
$url = $shortUrl->getOriginalUrl();
|
||||
$this->cache->save($shortCode, $url);
|
||||
return $url;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user