Skip to content

Commit

Permalink
Add persist driver cache (#1555)
Browse files Browse the repository at this point in the history
* Add persist driver cache

* Add persist driver cache, cleanup
  • Loading branch information
marcheffels committed May 22, 2024
1 parent 8499b46 commit 211426d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 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
17 changes: 13 additions & 4 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 @@ -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())
};
}

Expand All @@ -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())
};

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

0 comments on commit 211426d

Please sign in to comment.