36 lines
945 B
PHP
36 lines
945 B
PHP
<?php
|
|
|
|
namespace App\Support\Cache;
|
|
|
|
final class CacheKey
|
|
{
|
|
/**
|
|
* Build a canonical cache key.
|
|
*
|
|
* Format: module:type[:param:value[:param:value...]]
|
|
*
|
|
* Examples:
|
|
* CacheKey::build('students', 'list', ['page' => 1])
|
|
* => "students:list:page:1"
|
|
*
|
|
* CacheKey::build('students', 'list', ['status' => 'active', 'page' => 2])
|
|
* => "students:list:page:2:status:active" (sorted by key)
|
|
*
|
|
* CacheKey::build('students', 'detail', ['id' => 15])
|
|
* => "students:detail:id:15"
|
|
*/
|
|
public static function build(string $module, string $type, array $params = []): string
|
|
{
|
|
ksort($params);
|
|
|
|
$parts = [$module, $type];
|
|
|
|
foreach ($params as $param => $value) {
|
|
$parts[] = (string) $param;
|
|
$parts[] = is_array($value) ? implode(',', $value) : (string) $value;
|
|
}
|
|
|
|
return implode(':', $parts);
|
|
}
|
|
}
|