Skip to content

Commit

Permalink
fix key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Jan 9, 2025
1 parent 4f8c124 commit f1aa9d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"homepage": "https://github.com/kolirt/laravel-cacheable",
"license": "MIT",
"version": "1.1.0",
"version": "1.1.1",
"authors": [
{
"name": "kolirt"
Expand All @@ -34,4 +34,4 @@
]
}
}
}
}
50 changes: 28 additions & 22 deletions src/Traits/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit f1aa9d0

Please sign in to comment.