Add tags for flushing cache
This commit is contained in:
parent
0f478507d3
commit
70f66aaacc
|
@ -72,6 +72,7 @@ class Plugin extends PluginBase
|
|||
});
|
||||
Event::listen('media.file.delete', function($widget, $filePath) {
|
||||
app(FileObserver::class)->delete($filePath);
|
||||
app(CacheManager::class)->delete($filePath);
|
||||
});
|
||||
|
||||
Event::listen('media.file.move', function($widget, $old, $new) {
|
||||
|
|
|
@ -18,7 +18,19 @@ class CacheManager
|
|||
|
||||
public function get(string $path, string $size): string
|
||||
{
|
||||
return Cache::rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size));
|
||||
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);
|
||||
Cache::tags($path)->flush();
|
||||
}
|
||||
|
||||
private function getTag(string $path): string
|
||||
{
|
||||
return $this->fileObserver->normalizePath($path);
|
||||
}
|
||||
|
||||
private function cacheKey(string $path, string $size): string
|
||||
|
|
|
@ -20,7 +20,7 @@ class DeleteTest extends TestCase
|
|||
|
||||
public function testItDeletesAllVersionsIfOriginalImageDeleted()
|
||||
{
|
||||
Cache::set('resizer.original.pages/test.jpg', '::cacheContent::');
|
||||
Cache::tags('pages/test.jpg')->set('resizer.original.pages/test.jpg', '::cacheContent::');
|
||||
Setting::set('folders', ['pages']);
|
||||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', []);
|
||||
|
|
|
@ -96,7 +96,7 @@ class ImageTagTest extends TestCase
|
|||
UploadedFile::fake()->image('test.jpg', 640, 149)->storeAs('uploads/public/c/pages', 'test-640x149.jpg', 'local');
|
||||
|
||||
$output = app(Twig::class)->parse('{{"pages/test.jpg" | resize}}');
|
||||
$this->assertEquals($output, Cache::get('resize.original.pages/test.jpg'));
|
||||
$this->assertEquals($output, Cache::tags('pages/test.jpg')->get('resize.original.pages/test.jpg'));
|
||||
}
|
||||
|
||||
public function testPickOriginalImageWhenSvgIsGiven()
|
||||
|
@ -111,7 +111,7 @@ class ImageTagTest extends TestCase
|
|||
$this->assertFalse(str_contains($output, 'srcset'));
|
||||
$this->assertFalse(str_contains($output, 'sizes'));
|
||||
$this->assertTrue(str_contains($output, 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"'));
|
||||
$this->assertEquals($output, Cache::get('resize.original.pages/test.svg'));
|
||||
$this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg'));
|
||||
}
|
||||
|
||||
public function testNormalizeFilePathForCache()
|
||||
|
@ -122,8 +122,7 @@ class ImageTagTest extends TestCase
|
|||
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
|
||||
|
||||
$output = app(Twig::class)->parse('{{"/pages/test.svg" | resize}}');
|
||||
|
||||
$this->assertEquals($output, Cache::get('resize.original.pages/test.svg'));
|
||||
$this->assertEquals($output, Cache::tags('pages/test.svg')->get('resize.original.pages/test.svg'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue