1
0

Removed anything related to cache - Permit to sort bundles

This commit is contained in:
Richard Dern 2024-05-09 01:10:24 +02:00
parent 5cf1ae7910
commit beb701d752

View File

@ -11,7 +11,6 @@
use Exception; use Exception;
use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use League\Flysystem\StorageAttributes; use League\Flysystem\StorageAttributes;
@ -219,24 +218,14 @@ public function touch()
} }
} }
public function render(bool $ignoreCache = false) public function render()
{ {
$lastModified = Carbon::parse($this->metadata()->get('lastModified', '1970-01-02'));
$lastRendered = Carbon::parse($this->metadata()->get('lastRendered', '1970-01-01'));
$cacheKey = sprintf('render_%s', Str::slug($this->getPath()));
if ($lastRendered->gt($lastModified) && Cache::has($cacheKey) && !$ignoreCache) {
return Cache::get($cacheKey);
}
$renderer = BundleRenderer::getBundleRendererFor($this); $renderer = BundleRenderer::getBundleRendererFor($this);
$result = $renderer->render(); $result = $renderer->render();
$this->metadata()->set('lastRendered', now()->toIso8601String()); $this->metadata()->set('lastRendered', now()->toIso8601String());
$this->metadata()->save(); $this->metadata()->save();
Cache::put($cacheKey, $result, now()->addMonth());
return $result; return $result;
} }
@ -250,7 +239,7 @@ public function renderCard()
/** /**
* Return a list of bundles in the specified path * Return a list of bundles in the specified path
*/ */
public static function findBundles(FilesystemAdapter $disk, ?string $path = '/', bool $recursive = false): array public static function findBundles(FilesystemAdapter $disk, ?string $path = '/', bool $recursive = false, bool $sortByDate = true): array
{ {
$bundles = []; $bundles = [];
@ -272,14 +261,16 @@ public static function findBundles(FilesystemAdapter $disk, ?string $path = '/',
} }
} }
$bundles = collect($bundles) if ($sortByDate) {
->sortBy([ $bundles = collect($bundles)
fn (Bundle $a, Bundle $b) => Carbon::parse($a->metadata()->get('date')) ->sortBy([
->gt(Carbon::parse($b->metadata()->get('date'))), fn (Bundle $a, Bundle $b) => Carbon::parse($a->metadata()->get('date'))
fn (Bundle $a, Bundle $b) => Carbon::parse($b->metadata()->get('date')) ->gt(Carbon::parse($b->metadata()->get('date'))),
->gt(Carbon::parse($a->metadata()->get('date'))), fn (Bundle $a, Bundle $b) => Carbon::parse($b->metadata()->get('date'))
]) ->gt(Carbon::parse($a->metadata()->get('date'))),
->toArray(); ])
->toArray();
}
return $bundles; return $bundles;
} }