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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(string $path, string $size): string
|
|
|
|
{
|
2021-09-06 15:35:45 +02:00
|
|
|
return Cache::tags($this->getTag($path))
|
|
|
|
->rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|