Fixed: Clear cache after resizing image

This commit is contained in:
philipp lang 2021-12-27 02:18:56 +01:00
parent cb3228bc53
commit 425a6aa3be
3 changed files with 24 additions and 4 deletions

View File

@ -18,7 +18,7 @@ class CacheManager
public function get(MediaPath $path, string $size, ?string $sizes, array $options = []): string public function get(MediaPath $path, string $size, ?string $sizes, array $options = []): string
{ {
return Cache::tags($this->pathTag($path, $size))->rememberForever( return Cache::tags($this->pathTag($path))->rememberForever(
$this->cacheKey($path, $size), $this->cacheKey($path, $size),
fn () => $this->tagGenerator->generate($path, $size, $sizes, $options) fn () => $this->tagGenerator->generate($path, $size, $sizes, $options)
); );
@ -26,7 +26,7 @@ class CacheManager
public function delete(MediaPath $path): void public function delete(MediaPath $path): void
{ {
Cache::tags([$this->singlePathTag($path)])->flush(); Cache::tags($this->pathTag($path))->flush();
} }
public function flush(): void public function flush(): void
@ -39,7 +39,7 @@ class CacheManager
return "resizer.{$size}.{$path->normal()}"; return "resizer.{$size}.{$path->normal()}";
} }
private function pathTag(MediaPath $path, string $size): array private function pathTag(MediaPath $path): array
{ {
return [$this->tagAll, $this->singlePathTag($path)]; return [$this->tagAll, $this->singlePathTag($path)];
} }

View File

@ -2,6 +2,7 @@
namespace Aweos\Resizer\Classes; namespace Aweos\Resizer\Classes;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Lib\MediaPath; use Aweos\Resizer\Lib\MediaPath;
use Log; use Log;
use Throwable; use Throwable;
@ -12,7 +13,10 @@ class ResizeJob
public function fire($job, $params) public function fire($job, $params)
{ {
list($file) = $params; list($file) = $params;
app(ImageResizer::class)->generate(new MediaPath($file), $params['update'] ?? false); $media = new MediaPath($file);
app(ImageResizer::class)->generate($media, $params['update'] ?? false);
app(CacheManager::class)->delete($media);
$job->delete(); $job->delete();
} }

View File

@ -7,6 +7,7 @@ use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase; use Aweos\Resizer\Tests\TestCase;
use Event; use Event;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use MediaLibrary; use MediaLibrary;
use Storage; use Storage;
@ -33,6 +34,21 @@ class ResizerTest extends TestCase
$this->assertFileCount(0, ''); $this->assertFileCount(0, '');
} }
public function testDeleteOldCacheWhenUploadingFile(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/test.jpg', $file);
Cache::tags(['resizer', 'resizer.pages/test.jpg'])->put('resizer.original.pages/test.jpg', 'AAA');
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(0, '');
$this->assertFalse(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->has('resizer.original.pages/test.jpg'));
}
public function testThrowExceptionWhenOriginalFileNotFound(): void public function testThrowExceptionWhenOriginalFileNotFound(): void
{ {
$this->expectException(ResizerException::class); $this->expectException(ResizerException::class);