From 7733d2cb29f3f3ed652f7a21a6dbcbfd1144bb58 Mon Sep 17 00:00:00 2001 From: pthegner Date: Tue, 19 Mar 2024 09:42:49 +0100 Subject: [PATCH] Add cache with ttl set --- Cache/Navitia.php | 10 +++++++++ Configuration/NavitiaConfiguration.php | 28 ++++++++++++++++++++++++++ Service/NavitiaService.php | 7 +++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Cache/Navitia.php b/Cache/Navitia.php index e5ad07e..1a38f34 100644 --- a/Cache/Navitia.php +++ b/Cache/Navitia.php @@ -15,6 +15,7 @@ class Navitia extends Cache private string $urlApi = ''; private string $token = ''; private ?LoggerInterface $logger = null; + private ?string $cacheTtl = null; private function needToCheckPublicationDate(): bool { @@ -56,6 +57,12 @@ public function setLogger(LoggerInterface $logger): self return $this; } + public function setCacheTtl(?string $cacheTtl = null): self + { + $this->cacheTtl = $cacheTtl; + return $this; + } + public function generateCacheKey(?array $specificArgs): string { $backtrace = debug_backtrace()[1]; @@ -114,6 +121,9 @@ public function setCacheItem(string $cacheKey, $data): void $cacheItem->tag([$this->getCacheTag()]); $cacheItem->set($data); + if ($this->cacheTtl) { + $cacheItem->expiresAfter(intval($this->cacheTtl, 10)); + } $this->cache->save($cacheItem); } diff --git a/Configuration/NavitiaConfiguration.php b/Configuration/NavitiaConfiguration.php index 4ffe608..6ad9186 100644 --- a/Configuration/NavitiaConfiguration.php +++ b/Configuration/NavitiaConfiguration.php @@ -50,6 +50,12 @@ class NavitiaConfiguration implements NavitiaConfigurationInterface */ private $response_error; + /** + * + * @var ?string + */ + private $cacheTtl = null; + /** * Returns the Url of navitia configuration. * @@ -177,6 +183,28 @@ public function setResponseError($response_error) return $this; } + /** + * Returns the Token of navitia configuration. + * + * @return ?string + */ + public function getCacheTtl() + { + return $this->cacheTtl; + } + + /** + * Set the cacheTtl of navitia configuration + * + * @param ?string $cacheTtl + * @return \Navitia\Component\Configuration\NavitiaConfiguration + */ + public function setCacheTtl($cacheTtl = null) + { + $this->cacheTtl = $cacheTtl; + return $this; + } + /** * {@inheritDoc} */ diff --git a/Service/NavitiaService.php b/Service/NavitiaService.php index 7794108..c0c9a66 100644 --- a/Service/NavitiaService.php +++ b/Service/NavitiaService.php @@ -137,12 +137,14 @@ public function generateRequest($api): NavitiaRequestInterface private function setCacheProperties( string $coverage, string $urlApi, - string $token + string $token, + ?string $cacheTtl = null ): void { if ($this->hasCache()) { $this->cache->setCoverage($coverage); $this->cache->setUrlApi($urlApi); $this->cache->setToken($token); + $this->cache->setCacheTtl($cacheTtl); } } @@ -157,12 +159,13 @@ public function callApi( $baseUrl = $this->config->getUrl().'/'.$this->config->getVersion().'/'; $url = $request->buildUrl($baseUrl); $token = $this->config->getToken(); + $cacheTtl = $this->config->getCacheTtl(); $this->log( $url, $request->getApiName(), array_merge($request->getParams(), array('token' => $token)) ); - $this->setCacheProperties($request->getRegion(), $baseUrl, $token); + $this->setCacheProperties($request->getRegion(), $baseUrl, $token, $cacheTtl); $curlResponse = $this->getApiResponse($url, $token, $enableCache); $response = $curlResponse['response'];