From badfde41e8288b5f7970b71a7fb4dc2469e82ab0 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 4 Nov 2021 11:52:35 +0100 Subject: [PATCH 1/2] Working proof of concept --- src/Stache/Indexes/Index.php | 21 +++++++++++++++++++-- src/Stache/Stores/Store.php | 7 ++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Stache/Indexes/Index.php b/src/Stache/Indexes/Index.php index 45b8084f700..1a19f0d5d20 100644 --- a/src/Stache/Indexes/Index.php +++ b/src/Stache/Indexes/Index.php @@ -2,20 +2,23 @@ namespace Statamic\Stache\Indexes; -use Illuminate\Support\Facades\Cache; +use Statamic\Facades\Entry; use Statamic\Facades\Stache; +use Illuminate\Support\Facades\Cache; abstract class Index { protected $store; protected $name; + protected $dates; protected $items = []; protected $loaded = false; - public function __construct($store, $name) + public function __construct($store, $name, $dates = null) { $this->store = $store; $this->name = $name; + $this->dates = $dates; } public function name() @@ -70,6 +73,20 @@ public function load() $this->items = Cache::get($this->cacheKey()); + if ($this->name === 'status') { + $scheduledItems = collect($this->items)->filter(function ($value) { + return $value === 'scheduled'; + }); + + $this->dates + ->intersectByKeys($scheduledItems) + ->each(function ($date, $id) { + if ($date->isPast()) { + $this->updateItem(Entry::find($id)); + } + }); + } + if ($this->items === null) { $this->update(); } diff --git a/src/Stache/Stores/Store.php b/src/Stache/Stores/Store.php index de91f7864e3..e9ab30006f9 100644 --- a/src/Stache/Stores/Store.php +++ b/src/Stache/Stores/Store.php @@ -10,6 +10,7 @@ use Statamic\Stache\Exceptions\DuplicateKeyException; use Statamic\Stache\Indexes; use Statamic\Stache\Indexes\Index; +use Statamic\Stache\Indexes\Value; use Statamic\Support\Arr; abstract class Store @@ -55,7 +56,11 @@ protected function resolveIndex($name) $class = $this->indexes()->get($name, $this->valueIndex); - $index = new $class($this, $name); + if ($name === 'status') { + $dates = (new Value($this, 'date'))->load()->items(); + } + + $index = new $class($this, $name, $dates ?? null); $cached->put($key, $index); From f74d0279ef9f6afcd43d8e1a7c5aa53d45174a79 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Thu, 4 Nov 2021 12:54:09 +0100 Subject: [PATCH 2/2] Another working proof of concept --- src/Stache/Indexes/Index.php | 18 ++++++------------ src/Stache/Indexes/Value.php | 21 +++++++++++++++++++++ src/Stache/Stores/Store.php | 6 +----- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Stache/Indexes/Index.php b/src/Stache/Indexes/Index.php index 1a19f0d5d20..8484a5ae541 100644 --- a/src/Stache/Indexes/Index.php +++ b/src/Stache/Indexes/Index.php @@ -10,15 +10,13 @@ abstract class Index { protected $store; protected $name; - protected $dates; protected $items = []; protected $loaded = false; - public function __construct($store, $name, $dates = null) + public function __construct($store, $name) { $this->store = $store; $this->name = $name; - $this->dates = $dates; } public function name() @@ -74,17 +72,13 @@ public function load() $this->items = Cache::get($this->cacheKey()); if ($this->name === 'status') { - $scheduledItems = collect($this->items)->filter(function ($value) { + collect($this->items)->filter(function ($value) { return $value === 'scheduled'; + })->each(function ($value, $id) { + if ($this->getItemTimestamp($id)->isPast()) { + $this->updateItem(Entry::find($id)); + } }); - - $this->dates - ->intersectByKeys($scheduledItems) - ->each(function ($date, $id) { - if ($date->isPast()) { - $this->updateItem(Entry::find($id)); - } - }); } if ($this->items === null) { diff --git a/src/Stache/Indexes/Value.php b/src/Stache/Indexes/Value.php index bbcbdcfe2a7..fc935d3518a 100644 --- a/src/Stache/Indexes/Value.php +++ b/src/Stache/Indexes/Value.php @@ -3,12 +3,15 @@ namespace Statamic\Stache\Indexes; use Statamic\Support\Str; +use Illuminate\Support\Facades\Cache; class Value extends Index { public function getItems() { return $this->store->getItemsFromFiles()->map(function ($item) { + $this->cacheItemTimestamp($item); + return $this->getItemValue($item); })->all(); } @@ -40,4 +43,22 @@ public function getItemValue($item) return $item->get($this->name); } + + public function cacheItemTimestamp($item) + { + $scheduledItems = Cache::get($this->timestampCacheKey()) ?? collect(); + $scheduledItems->put($item->id(), $item->date()); + + Cache::forever($this->timestampCacheKey(), collect($scheduledItems)); + } + + public function getItemTimestamp($id) + { + return Cache::get($this->timestampCacheKey())->get($id); + } + + protected function timestampCacheKey() + { + return "stache::timestamps::{$this->store->key()}"; + } } diff --git a/src/Stache/Stores/Store.php b/src/Stache/Stores/Store.php index e9ab30006f9..c3368f9c752 100644 --- a/src/Stache/Stores/Store.php +++ b/src/Stache/Stores/Store.php @@ -56,11 +56,7 @@ protected function resolveIndex($name) $class = $this->indexes()->get($name, $this->valueIndex); - if ($name === 'status') { - $dates = (new Value($this, 'date'))->load()->items(); - } - - $index = new $class($this, $name, $dates ?? null); + $index = new $class($this, $name); $cached->put($key, $index);