Skip to content

Commit

Permalink
Add persist driver cache
Browse files Browse the repository at this point in the history
  • Loading branch information
marcheffels committed May 16, 2024
1 parent 780ebd3 commit 5ce8348
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions resources/config/livewire-powergrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
|
*/
Expand Down
11 changes: 9 additions & 2 deletions src/Concerns/Persist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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())
};
}
Expand All @@ -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())
};

Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 5ce8348

Please sign in to comment.