Ad Cachemanager

This commit is contained in:
Philipp Lang 2021-09-06 14:37:35 +02:00
parent 3f91586b62
commit 0f478507d3
3 changed files with 36 additions and 4 deletions

View File

@ -1,5 +1,6 @@
<?php namespace Aweos\Resizer; <?php namespace Aweos\Resizer;
use Aweos\Resizer\Classes\CacheManager;
use Aweos\Resizer\Classes\FileObserver; use Aweos\Resizer\Classes\FileObserver;
use Aweos\Resizer\Classes\ImageResizer; use Aweos\Resizer\Classes\ImageResizer;
use Aweos\Resizer\Classes\ResizeJob; use Aweos\Resizer\Classes\ResizeJob;
@ -99,10 +100,7 @@ class Plugin extends PluginBase
public function registerMarkupTags() { public function registerMarkupTags() {
return [ return [
'filters' => [ 'filters' => [
'resize' => function($media, $size = 'original') { 'resize' => fn ($media, $size = 'original') => app(CacheManager::class)->get($media, $size)
$normalPath = app(FileObserver::class)->normalizePath($media);
return Cache::rememberForever("resize.{$size}.{$normalPath}", fn () => app(TagGenerator::class)->generate($media, $size));
},
] ]
]; ];
} }

31
classes/CacheManager.php Normal file
View File

@ -0,0 +1,31 @@
<?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
{
return Cache::rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size));
}
private function cacheKey(string $path, string $size): string
{
$normalPath = $this->fileObserver->normalizePath($path);
return "resize.{$size}.{$normalPath}";
}
}

View File

@ -4,6 +4,7 @@ namespace Aweos\Resizer\Tests\MediaTest;
use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase; use Aweos\Resizer\Tests\TestCase;
use Cache;
use Event; use Event;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Storage; use Storage;
@ -19,6 +20,7 @@ class DeleteTest extends TestCase
public function testItDeletesAllVersionsIfOriginalImageDeleted() public function testItDeletesAllVersionsIfOriginalImageDeleted()
{ {
Cache::set('resizer.original.pages/test.jpg', '::cacheContent::');
Setting::set('folders', ['pages']); Setting::set('folders', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
@ -28,6 +30,7 @@ class DeleteTest extends TestCase
$this->assertFileCount(0, 'pages'); $this->assertFileCount(0, 'pages');
Storage::assertMissing('uploads/public/c/pages'); Storage::assertMissing('uploads/public/c/pages');
$this->assertNull(Cache::get('resizer.original.pages/test.jpg'));
} }
public function testItPreservesDirectoryWhenThereAreOtherFiles() public function testItPreservesDirectoryWhenThereAreOtherFiles()