oc-resizer-plugin/classes/CacheManager.php

53 lines
1.2 KiB
PHP
Raw Normal View History

2021-09-06 14:37:35 +02:00
<?php
namespace Aweos\Resizer\Classes;
2021-09-09 19:18:41 +02:00
use Aweos\Resizer\Lib\MediaPath;
2021-09-06 14:37:35 +02:00
use Cache;
class CacheManager
{
2021-09-09 19:18:41 +02:00
public TagGenerator $tagGenerator;
private string $tagAll = 'resizer';
2021-09-06 14:37:35 +02:00
2021-09-09 19:18:41 +02:00
public function __construct(TagGenerator $tagGenerator)
2021-09-06 14:37:35 +02:00
{
2021-09-09 19:18:41 +02:00
$this->tagGenerator = $tagGenerator;
2021-09-06 14:37:35 +02:00
}
2021-09-14 02:13:29 +02:00
public function get(MediaPath $path, string $size, ?string $sizes, array $options = []): string
2021-09-06 14:37:35 +02:00
{
2021-09-09 19:18:41 +02:00
return Cache::tags($this->pathTag($path, $size))->rememberForever(
$this->cacheKey($path, $size),
2021-09-14 02:13:29 +02:00
fn () => $this->tagGenerator->generate($path, $size, $sizes, $options)
2021-09-09 19:18:41 +02:00
);
2021-09-06 15:35:45 +02:00
}
2021-09-09 19:18:41 +02:00
public function delete(MediaPath $path): void
2021-09-06 15:35:45 +02:00
{
2021-09-09 19:18:41 +02:00
Cache::tags([$this->singlePathTag($path)])->flush();
2021-09-06 15:35:45 +02:00
}
2021-09-09 19:18:41 +02:00
public function flush(): void
2021-09-06 15:35:45 +02:00
{
2021-09-09 19:18:41 +02:00
Cache::tags([$this->tagAll])->flush();
2021-09-06 14:37:35 +02:00
}
2021-09-09 19:18:41 +02:00
private function cacheKey(MediaPath $path, string $size): string
2021-09-06 14:37:35 +02:00
{
2021-09-09 19:18:41 +02:00
return "resizer.{$size}.{$path->normal()}";
}
2021-09-06 14:37:35 +02:00
2021-09-09 19:18:41 +02:00
private function pathTag(MediaPath $path, string $size): array
{
return [$this->tagAll, $this->singlePathTag($path)];
2021-09-06 14:37:35 +02:00
}
2021-09-09 19:18:41 +02:00
public function singlePathTag(MediaPath $path): string
{
2021-09-09 19:18:41 +02:00
return "resizer.{$path->normal()}";
}
2021-09-06 14:37:35 +02:00
}