2021-09-06 14:37:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer\Classes;
|
|
|
|
|
|
|
|
use Cache;
|
|
|
|
|
|
|
|
class CacheManager
|
|
|
|
{
|
|
|
|
|
|
|
|
public FileObserver $fileObserver;
|
|
|
|
public TagGenerator $tag;
|
|
|
|
|
|
|
|
public function __construct(FileObserver $fileObserver, TagGenerator $tag)
|
|
|
|
{
|
|
|
|
$this->tag = $tag;
|
|
|
|
$this->fileObserver = $fileObserver;
|
|
|
|
}
|
|
|
|
|
2021-09-06 18:23:06 +02:00
|
|
|
public function get(string $path, string $size, ?string $sizes): string
|
2021-09-06 14:37:35 +02:00
|
|
|
{
|
2021-09-06 18:23:06 +02:00
|
|
|
return Cache::tags($this->getTag($path, $sizes))
|
|
|
|
->rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size, $sizes));
|
2021-09-06 15:35:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(string $path): void
|
|
|
|
{
|
|
|
|
$path = $this->fileObserver->normalizePath($path);
|
2021-09-06 17:22:57 +02:00
|
|
|
Cache::tags("resizer.$path")->flush();
|
2021-09-06 15:35:45 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 17:22:57 +02:00
|
|
|
private function getTag(string $path): array
|
2021-09-06 15:35:45 +02:00
|
|
|
{
|
2021-09-06 17:22:57 +02:00
|
|
|
return ['resizer', "resizer.{$this->fileObserver->normalizePath($path)}"];
|
2021-09-06 14:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function cacheKey(string $path, string $size): string
|
|
|
|
{
|
|
|
|
$normalPath = $this->fileObserver->normalizePath($path);
|
|
|
|
|
2021-09-06 17:22:57 +02:00
|
|
|
return "resizer.{$size}.{$normalPath}";
|
2021-09-06 14:37:35 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 17:47:46 +02:00
|
|
|
public function flush(): void
|
|
|
|
{
|
|
|
|
Cache::tags('resizer')->flush();
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:37:35 +02:00
|
|
|
}
|