Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cached asset handling #3201

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/Assets/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Assets;

use Exception;
use Illuminate\Support\Facades\Cache;
use Statamic\Contracts\Assets\AssetContainer;
use Statamic\Contracts\Assets\QueryBuilder as Contract;
use Statamic\Facades;
Expand Down Expand Up @@ -30,7 +31,12 @@ protected function getBaseItems()
$assets = $this->convertPathsToAssets($assets);
}

return $this->collect($assets);
$assets = $this->collect($assets);

// If any assets were deleted through the filesystem (e.g. manually or
// through git) during the file listing cache window, the conversion
// above would have resulted in nulls. We remove the nulls here.
return $assets->filter()->values();
}

protected function limitItems($items)
Expand All @@ -57,11 +63,30 @@ public function get($columns = ['*'])
{
$items = parent::get($columns);

if (! $this->requiresAssetInstances()) {
$items = $this->convertPathsToAssets($items);
// If we required asset instances, they would have already been converted.
if ($this->requiresAssetInstances()) {
return $items;
}

return $items;
$items = $this->convertPathsToAssets($items);

// If any assets were deleted through the filesystem (e.g. manually or through git)
// during the file listing cache window, the conversion above would have resulted
// in nulls. In that case, we'll bust the cache and requery. We also only care
// when it's being limited like in pagination or when using the take method.
if ($this->hasAnyNulls($items) && $this->limit) {
Cache::forget($this->container->filesCacheKey());
Cache::forget($this->container->filesCacheKey($this->folder));

return $this->get($columns);
}

return $items->filter()->values();
}

private function hasAnyNulls($items)
{
return $items->reject()->isNotEmpty();
}

private function requiresAssetInstances()
Expand Down