oc-resizer-plugin/classes/CacheManager.php

60 lines
1.4 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;
2022-02-16 02:49:19 +01:00
use Aweos\Resizer\Lib\StorageMediaPath;
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
2024-09-28 01:31:05 +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
{
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
{
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
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-09 19:18:41 +02:00
return "resizer.{$path->normal()}";
}
2021-09-19 23:08:11 +02:00
public function biggestVersion(string $path, string $size): string
{
2022-02-16 02:49:19 +01:00
$path = new StorageMediaPath($path);
2021-09-19 23:08:11 +02:00
return $this->tagGenerator->singleFile($path, $size);
}
2021-09-06 14:37:35 +02:00
}