From f1aa9d0f44d0dfa4591baa593b5580ab8eb11f33 Mon Sep 17 00:00:00 2001 From: kolirt Date: Thu, 9 Jan 2025 21:55:37 +0200 Subject: [PATCH] fix key generation --- composer.json | 4 ++-- src/Traits/Cacheable.php | 50 ++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index a08843a..5d36d2d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "homepage": "https://github.com/kolirt/laravel-cacheable", "license": "MIT", - "version": "1.1.0", + "version": "1.1.1", "authors": [ { "name": "kolirt" @@ -34,4 +34,4 @@ ] } } -} \ No newline at end of file +} diff --git a/src/Traits/Cacheable.php b/src/Traits/Cacheable.php index 2a738f7..d5d0743 100644 --- a/src/Traits/Cacheable.php +++ b/src/Traits/Cacheable.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use ReflectionClass; @@ -164,28 +165,9 @@ private function getCacheKey($fnc_name = null, ...$args): array $tags = $this->getCacheTags(); $args_prepared = []; foreach ($args as $arg) { - if ($arg instanceof Pivot) { - $args_prepared[] = "{$this->getName($arg)}->{$arg->getAttribute($arg->getForeignKey())}_{$arg->getKey()}"; - } else if ($arg instanceof Model) { - $args_prepared[] = "{$this->getName($arg)}->{$arg->getKey()}"; - } else if ($arg instanceof \UnitEnum) { - $args_prepared[] = "{$this->getName($arg)}->{$arg->name}"; - } else if ($arg instanceof Carbon) { - $args_prepared[] = str_replace(':', '_', $arg->toIso8601String()); - } else { - switch (gettype($arg)) { - case 'boolean': - $args_prepared[] = $arg ? 1 : 0; - break; - case 'integer': - case 'double': - case 'string': - $args_prepared[] = $arg; - break; - case 'NULL': - $args_prepared[] = 'null'; - break; - } + $arg_prepared = $this->prepareArg($arg); + if ($arg_prepared !== null) { + $args_prepared[] = $arg_prepared; } } $args_prepared = implode(', ', $args_prepared); @@ -199,6 +181,30 @@ private function getCacheKey($fnc_name = null, ...$args): array ]; } + private function prepareArg($arg) + { + if ($arg instanceof Pivot) { + return "{$this->getName($arg)}->{$arg->getAttribute($arg->getForeignKey())}_{$arg->getKey()}"; + } else if ($arg instanceof Model) { + return "{$this->getName($arg)}->{$arg->getKey()}"; + } else if ($arg instanceof \UnitEnum) { + return "{$this->getName($arg)}->{$arg->name}"; + } else if ($arg instanceof Carbon) { + return str_replace(':', '_', $arg->toIso8601String()); + } else if ($arg instanceof Collection) { + return $arg->map(fn($v) => $this->prepareArg($v))->toJson(); + } + + return match (gettype($arg)) { + 'boolean' => $arg ? 1 : 0, + 'integer', 'double', 'string' => $arg, + 'array' => json_encode(array_map(fn($v) => $this->prepareArg($v), $arg)), + 'NULL' => 'null', + 'object' => 'object', + default => null + }; + } + private function getName($target = null): string { $reflect = new ReflectionClass(is_null($target) ? $this : $target);