Skip to content

Commit

Permalink
feat(cache): add missing ttl from expiration date given from esi usin…
Browse files Browse the repository at this point in the history
…g redis (#84)

Co-authored-by: Rakdos8 <[email protected]>
  • Loading branch information
warlof and Rakdos8 committed Jul 14, 2023
1 parent 3261a4a commit 7773249
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Cache/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Seat\Eseye\Cache;

use Carbon\Carbon;
use DateInterval;
use InvalidArgumentException;
use Predis\Client;
Expand Down Expand Up @@ -82,7 +83,21 @@ public function set(string $key, mixed $value, null|int|DateInterval $ttl = null

$this->validateCacheKey($key, $uri_path, $uri_query);

$this->redis->set($this->buildCacheKey($uri_path, $uri_query), serialize($value));
switch (true) {
case $ttl == null:
$this->redis->set($this->buildCacheKey($uri_path, $uri_query), serialize($value));

break;
case $ttl instanceof DateInterval:
$now = Carbon::now('UTC');
$expires = $now->clone()->add($ttl);

$this->redis->setex($this->buildCacheKey($uri_path, $uri_query), $now->diffInSeconds($expires), serialize($value));

break;
default:
$this->redis->setex($this->buildCacheKey($uri_path, $uri_query), $ttl, serialize($value));
}

return true;
}
Expand Down

0 comments on commit 7773249

Please sign in to comment.