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

[5.x] Allow custom asset container contents cache store #11481

Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions src/Assets/AssetContainerContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function all()
return $this->files;
}

return $this->files = Cache::remember($this->key(), $this->ttl(), function () {
return $this->files = $this->cacheStore()->remember($this->key(), $this->ttl(), function () {
return collect($this->getRawFlysystemDirectoryListing())
->keyBy('path')
->map(fn ($file) => $this->normalizeFlysystemAttributes($file))
Expand Down Expand Up @@ -164,7 +164,7 @@ private function getNormalizedFlysystemMetadata($path)

public function cached()
{
return Cache::get($this->key());
return $this->cacheStore()->get($this->key());
}

public function files()
Expand Down Expand Up @@ -271,7 +271,7 @@ private function filesystem()

public function save()
{
Cache::put($this->key(), $this->all(), $this->ttl());
$this->cacheStore()->put($this->key(), $this->all(), $this->ttl());
}

public function forget($path)
Expand All @@ -298,7 +298,7 @@ public function add($path)
$files = $this->all()->put($path, $metadata);

if (Statamic::isWorker()) {
Cache::put($this->key(), $files, $this->ttl());
$this->cacheStore()->put($this->key(), $files, $this->ttl());
}

$this->filteredFiles = null;
Expand All @@ -316,4 +316,14 @@ private function ttl()
{
return Stache::isWatcherEnabled() ? 0 : null;
}

public function cacheStore()
{
return Cache::store($this->hasCustomStore() ? 'asset_container_contents' : null);
}

private function hasCustomStore(): bool
{
return config()->has('cache.stores.asset_container_contents');
}
}
18 changes: 18 additions & 0 deletions tests/Assets/AssetFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,24 @@ public function it_converts_to_an_array()
], $folder->toArray());
}

#[Test]
public function it_uses_a_custom_cache_store()
{
config([
'cache.stores.asset_container_contents' => [
'driver' => 'file',
'path' => storage_path('statamic/asset-container-contents'),
],
]);

Storage::fake('local');

$store = Facades\AssetContainer::make('test')->disk('local')->contents()->cacheStore();

// ideally we would have checked the store name, but laravel 10 doesnt give us a way to do that
$this->assertStringContainsString('asset-container-contents', $store->getStore()->getDirectory());
}

private function containerWithDisk()
{
Storage::fake('local');
Expand Down
Loading