oc-resizer-plugin/classes/CacheManager.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, array $options = []): string
{
return Cache::tags($this->pathTag($path, $size))->rememberForever(
$this->cacheKey($path, $size),
fn () => $this->tagGenerator->generate($path, $size, $sizes, $options)
);
}
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()}";
}
}