diff --git a/resources/config/livewire-powergrid.php b/resources/config/livewire-powergrid.php index ef1a47b3e..68eabeb22 100644 --- a/resources/config/livewire-powergrid.php +++ b/resources/config/livewire-powergrid.php @@ -86,6 +86,7 @@ | | PowerGrid supports persisting of the filters, columns and sorting. | 'session': persist in the session. + | 'cache': persist with cache. | 'cookies': persist with cookies (default). | */ diff --git a/src/Concerns/Persist.php b/src/Concerns/Persist.php index a16484ba4..5c7bc93ec 100644 --- a/src/Concerns/Persist.php +++ b/src/Concerns/Persist.php @@ -3,7 +3,7 @@ namespace PowerComponents\LivewirePowerGrid\Concerns; use Exception; -use Illuminate\Support\Facades\{Cookie, Session}; +use Illuminate\Support\Facades\{Cache, Cookie, Session}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; /** @codeCoverageIgnore */ @@ -57,6 +57,7 @@ protected function persistState(string $tableItem): void match ($this->getPersistDriverConfig()) { 'session' => Session::put($this->getPersistKeyName(), strval(json_encode($state))), + 'cache' => Cache::store($this->getPersistDriverStoreConfig())->put($this->getPersistKeyName(), strval(json_encode($state))), default => Cookie::queue($this->getPersistKeyName(), strval(json_encode($state)), now()->addYears(5)->unix()) }; } @@ -72,6 +73,7 @@ private function restoreState(): void $storage = match ($this->getPersistDriverConfig()) { 'session' => Session::get($this->getPersistKeyName()), + 'cache' => Cache::store($this->getPersistDriverStoreConfig())->get($this->getPersistKeyName()), default => Cookie::get($this->getPersistKeyName()) }; @@ -107,13 +109,18 @@ private function getPersistDriverConfig(): string { $persistDriver = strval(config('livewire-powergrid.persist_driver', 'cookies')); - if (!in_array($persistDriver, ['session', 'cookies'])) { + if (!in_array($persistDriver, ['session', 'cache', 'cookies',])) { throw new Exception('Invalid persist driver'); } return $persistDriver; } + private function getPersistDriverStoreConfig(): ?string + { + return config('livewire-powergrid.persist_driver_store'); + } + private function getPersistKeyName(): string { if (!empty($this->persistPrefix)) {