Skip to content
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
8 changes: 6 additions & 2 deletions src/Stache/Stores/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ public function cacheIndexUsage($index)

$indexes->push($index);

$this->usedIndexes = $indexes;
// Another process may have recorded usages since this one memoized its
// list (e.g. parallel warming), so merge with the cache rather than replace it.
$cached = collect(Stache::cacheStore()->get($key = $this->indexUsageCacheKey(), []));

Stache::cacheStore()->put($this->indexUsageCacheKey(), $indexes->all());
$this->usedIndexes = $cached->merge($indexes)->unique()->sort()->values();

Stache::cacheStore()->put($key, $this->usedIndexes->all());
}

protected function indexUsageCacheKey()
Expand Down
18 changes: 18 additions & 0 deletions tests/Stache/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ public function it_gets_the_paths_from_the_cache_again_after_memoized_state_is_r
$this->assertEquals($expected, $store->paths());
$this->assertEquals(2, $cacheHits);
}

#[Test]
public function index_usage_cached_by_another_process_is_kept()
{
// Two instances of the same store stand in for two processes warming
// the Stache in parallel. Each memoizes its own usage list, so the
// second write must merge with the cache instead of replacing it.
$cacheKey = "stache::indexes::{$this->store->key()}::_indexes";
$other = new TestStore;

$this->store->cacheIndexUsage('path');
$other->cacheIndexUsage('path');

$this->store->cacheIndexUsage('site');
$other->cacheIndexUsage('uri');

$this->assertEquals(['path', 'site', 'uri'], Cache::get($cacheKey));
}
}

class TestStore extends Store
Expand Down
Loading