Skip to content

Commit

Permalink
Allow custom asset container contents cache store
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Feb 21, 2025
1 parent 4ace966 commit bf2d122
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
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');
}
}
17 changes: 17 additions & 0 deletions tests/Assets/AssetFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,23 @@ 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');

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

$this->assertSame('asset_container_contents', $container->contents()->cacheStore()->getName());
}

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

0 comments on commit bf2d122

Please sign in to comment.