Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add persist driver cache #1555

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest:

    private function getPersistDriverStoreConfig(): string
    {
        return strval(config('livewire-powergrid.persist_driver_store'));
    }

and perhaps we could profit and improve code readability with:

        $jsonState = strval(json_encode($state));

        match ($this->getPersistDriverConfig()) {
            '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())
        };

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
Loading