Skip to content

Commit

Permalink
[5.x] Cache mounted collections (#9675)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
JohnathonKoster and jasonvarga authored Apr 2, 2024
1 parent 7a0b80d commit d00fedb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public function save()
Facades\Collection::save($this);

Blink::forget('collection-handles');
Blink::forget('mounted-collections');
Blink::flushStartingWith("collection-{$this->id()}");

if ($isNew) {
Expand Down Expand Up @@ -766,6 +767,8 @@ public function delete()

CollectionDeleted::dispatch($this);

Blink::forget('mounted-collections');

return true;
}

Expand Down
8 changes: 5 additions & 3 deletions src/Stache/Repositories/CollectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public function findByMount($mount): ?Collection
return null;
}

return $this->all()->first(function ($collection) use ($mount) {
return optional($collection->mount())->id() === $mount->id();
});
return Blink::once('mounted-collections', fn () => $this
->all()
->keyBy(fn ($collection) => $collection->mount()?->id())
->filter()
)->get($mount->id());
}

public function findOrFail($id): Collection
Expand Down
1 change: 1 addition & 0 deletions tests/Data/Entries/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ public function it_saves_the_collection_through_the_api()
Facades\Collection::shouldReceive('save')->with($collection)->once();
Facades\Collection::shouldReceive('handleExists')->with('test')->once();
Facades\Blink::shouldReceive('forget')->with('collection-handles')->once();
Facades\Blink::shouldReceive('forget')->with('mounted-collections')->once();
Facades\Blink::shouldReceive('flushStartingWith')->with('collection-test')->once();

$return = $collection->save();
Expand Down

0 comments on commit d00fedb

Please sign in to comment.