aiblog/app/Services/CacheService.php
2024-11-17 11:30:01 +08:00

40 lines
No EOL
817 B
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use Illuminate\Support\Facades\Cache;
class CacheService
{
protected string $prefix;
protected int $ttl;
public function __construct(string $prefix = '', int $ttl = 3600)
{
$this->prefix = $prefix;
$this->ttl = $ttl;
}
public function remember(string $key, callable $callback)
{
return Cache::remember($this->getCacheKey($key), $this->ttl, $callback);
}
public function forget(string $key): void
{
Cache::forget($this->getCacheKey($key));
}
public function tags(array $tags): self
{
$this->prefix = implode(':', $tags) . ':';
return $this;
}
protected function getCacheKey(string $key): string
{
return $this->prefix . $key;
}
}