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-12-27 02:18:56 +01:00
|
|
|
return Cache::tags($this->pathTag($path))->rememberForever(
|
2021-09-09 19:18:41 +02:00
|
|
|
$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-12-27 02:18:56 +01:00
|
|
|
Cache::tags($this->pathTag($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-12-27 02:18:56 +01:00
|
|
|
private function pathTag(MediaPath $path): array
|
2021-09-09 19:18:41 +02:00
|
|
|
{
|
|
|
|
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-06 17:47:46 +02:00
|
|
|
{
|
2021-09-09 19:18:41 +02:00
|
|
|
return "resizer.{$path->normal()}";
|
2021-09-06 17:47:46 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 23:08:11 +02:00
|
|
|
public function biggestVersion(string $path, string $size): string
|
|
|
|
{
|
|
|
|
$path = new MediaPath($path);
|
|
|
|
|
|
|
|
return $this->tagGenerator->singleFile($path, $size);
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:37:35 +02:00
|
|
|
}
|