53 lines
1.2 KiB
PHP
53 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Aweos\Resizer\Classes;
|
|
|
|
use Aweos\Resizer\Lib\MediaPath;
|
|
use Cache;
|
|
|
|
class CacheManager
|
|
{
|
|
|
|
public TagGenerator $tagGenerator;
|
|
private string $tagAll = 'resizer';
|
|
|
|
public function __construct(TagGenerator $tagGenerator)
|
|
{
|
|
$this->tagGenerator = $tagGenerator;
|
|
}
|
|
|
|
public function get(MediaPath $path, string $size, ?string $sizes): string
|
|
{
|
|
return Cache::tags($this->pathTag($path, $size))->rememberForever(
|
|
$this->cacheKey($path, $size),
|
|
fn () => $this->tagGenerator->generate($path, $size, $sizes)
|
|
);
|
|
}
|
|
|
|
public function delete(MediaPath $path): void
|
|
{
|
|
Cache::tags([$this->singlePathTag($path)])->flush();
|
|
}
|
|
|
|
public function flush(): void
|
|
{
|
|
Cache::tags([$this->tagAll])->flush();
|
|
}
|
|
|
|
private function cacheKey(MediaPath $path, string $size): string
|
|
{
|
|
return "resizer.{$size}.{$path->normal()}";
|
|
}
|
|
|
|
private function pathTag(MediaPath $path, string $size): array
|
|
{
|
|
return [$this->tagAll, $this->singlePathTag($path)];
|
|
}
|
|
|
|
public function singlePathTag(MediaPath $path): string
|
|
{
|
|
return "resizer.{$path->normal()}";
|
|
}
|
|
|
|
}
|