Skip to content

Commit

Permalink
Add cache with ttl set
Browse files Browse the repository at this point in the history
  • Loading branch information
pthegner committed Mar 19, 2024
1 parent c9fb2d7 commit 7733d2c
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 7733d2c

Please sign in to comment.