Skip to content

Commit

Permalink
when entries are saved, update them in the stache
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Apr 29, 2024
1 parent f740018 commit 0f90874
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/Stache/Stores/CollectionEntriesStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class CollectionEntriesStore extends ChildStore
{
protected $collection;
private bool $shouldBlinkEntryUris = true;

protected function collection()
{
Expand Down Expand Up @@ -241,10 +242,19 @@ protected function getCachedItem($key)
return null;
}

if ($cache['uri']) {
if ($this->shouldBlinkEntryUris && $cache['uri']) {
Blink::store('entry-uris')->put($cache['entry']->id(), $cache['uri']);
}

return $cache['entry'];
}

public function withoutBlinkingEntryUris($callback)
{
$this->shouldBlinkEntryUris = false;
$return = $callback();
$this->shouldBlinkEntryUris = true;

return $return;
}
}
17 changes: 13 additions & 4 deletions src/Stache/Stores/CollectionsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ protected function getDefaultPublishState($data)

public function updateEntryUris($collection, $ids = null)
{
$index = Stache::store('entries')
->store($collection->handle())
->index('uri');
$store = Stache::store('entries')->store($collection->handle());
$this->updateEntriesWithinIndex($store->index('uri'), $ids);
$this->updateEntriesWithinStore($store, $ids);
}

$this->updateEntriesWithinIndex($index, $ids);
private function updateEntriesWithinStore($store, $ids)
{
if (empty($ids)) {
$ids = $store->paths()->keys();
}

$entries = $store->withoutBlinkingEntryUris(fn () => collect($ids)->map(fn ($id) => Entry::find($id))->filter());

$entries->each(fn ($entry) => $store->cacheItem($entry));
}

public function updateEntryOrder($collection, $ids = null)
Expand Down
2 changes: 0 additions & 2 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,6 @@ public function a_localized_entry_in_a_structured_collection_without_a_route_for
*/
public function it_gets_urls_for_first_child_redirects($value)
{
\Event::fake(); // Don't invalidate static cache etc when saving entries.

$this->setSites([
'en' => ['url' => 'http://domain.com/', 'locale' => 'en_US'],
]);
Expand Down

0 comments on commit 0f90874

Please sign in to comment.