Files
postshop-backend/app/Repositories/System/Cache/CacheRepository.php
2026-02-03 15:31:29 +05:00

68 lines
1.3 KiB
PHP

<?php
namespace App\Repositories\System\Cache;
class CacheRepository
{
/**
* Cache name
*/
protected string $name;
/**
* Value
*/
protected mixed $value;
/**
* Time
*/
protected int $time;
/**
* Cache repo
*/
public function __construct(string $name, mixed $value, int $time = 60)
{
$this->name = $name;
$this->value = $value;
$this->time = $time;
}
/**
* Cache Repo
*
* @param string $name key
* @param mixed $value value
* @param int|int $time time in seconds
*/
public static function make(string $name, mixed $value, int $time = 600): mixed
{
$repo = new self($name, $value, $time);
return $repo->handle();
}
/**
* Handle cache
*/
public function handle(): mixed
{
return cache()->has($this->name)
? cache($this->name)
: cache()->remember(
key: $this->name,
ttl: $this->time,
callback: fn () => is_callable($this->value) ? call_user_func($this->value) : $this->value
);
}
/**
* Forget the key from cache
*/
public static function forget(string $name): void
{
cache()->forget($name);
}
}