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..3cecf8ac0 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 */ @@ -55,9 +55,12 @@ protected function persistState(string $tableItem): void return; } + $jsonState = strval(json_encode($state)); + match ($this->getPersistDriverConfig()) { - 'session' => Session::put($this->getPersistKeyName(), strval(json_encode($state))), - default => Cookie::queue($this->getPersistKeyName(), strval(json_encode($state)), now()->addYears(5)->unix()) + 'session' => Session::put($this->getPersistKeyName(), $jsonState), + 'cache' => Cache::store($this->getPersistDriverStoreConfig())->put($this->getPersistKeyName(), $jsonState), + default => Cookie::queue($this->getPersistKeyName(), $jsonState, now()->addYears(5)->unix()) }; } @@ -72,6 +75,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 +111,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 strval(config('livewire-powergrid.persist_driver_store')); + } + private function getPersistKeyName(): string { if (!empty($this->persistPrefix)) {