oc-resizer-plugin/classes/CacheManager.php

61 lines
1.4 KiB
PHP

<?php
namespace Aweos\Resizer\Classes;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Lib\StorageMediaPath;
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))->rememberForever(
$this->cacheKey($path, $size),
fn () => $this->tagGenerator->generate($path, $size, $sizes, $options)
);
}
public function delete(MediaPath $path): void
{
Cache::tags($this->pathTag($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): array
{
return [$this->tagAll, $this->singlePathTag($path)];
}
public function singlePathTag(MediaPath $path): string
{
return "resizer.{$path->normal()}";
}
public function biggestVersion(string $path, string $size): string
{
$path = new StorageMediaPath($path);
return $this->tagGenerator->singleFile($path, $size);
}
}