From 7773249244e89ec970ada109c1695a9f25f15f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20LEUILLIOT?= Date: Fri, 14 Jul 2023 11:55:29 +0200 Subject: [PATCH] feat(cache): add missing ttl from expiration date given from esi using redis (#84) Co-authored-by: Rakdos8 --- src/Cache/RedisCache.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Cache/RedisCache.php b/src/Cache/RedisCache.php index 7ce8442..af2017b 100644 --- a/src/Cache/RedisCache.php +++ b/src/Cache/RedisCache.php @@ -22,6 +22,7 @@ namespace Seat\Eseye\Cache; +use Carbon\Carbon; use DateInterval; use InvalidArgumentException; use Predis\Client; @@ -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; }