Skip to content

Commit

Permalink
Merge pull request #46 from hove-io/task-bo-3164-add-ttl
Browse files Browse the repository at this point in the history
[BO-3164] - Add cache with ttl
  • Loading branch information
pthegner authored Mar 22, 2024
2 parents c9fb2d7 + 7733d2c commit 6b566a4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cache/Navitia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}

Expand Down
28 changes: 28 additions & 0 deletions Configuration/NavitiaConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class NavitiaConfiguration implements NavitiaConfigurationInterface
*/
private $response_error;

/**
*
* @var ?string
*/
private $cacheTtl = null;

/**
* Returns the Url of navitia configuration.
*
Expand Down Expand Up @@ -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}
*/
Expand Down
7 changes: 5 additions & 2 deletions Service/NavitiaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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'];
Expand Down

0 comments on commit 6b566a4

Please sign in to comment.