Skip to content

Commit

Permalink
Clear Table Cache when running migrations (#1531)
Browse files Browse the repository at this point in the history
* create TableCache support class

* Forget PowerGrid table cache on migrations

* replace Cache with PowerGridTableCache

* ignore template error
  • Loading branch information
dansysanalyst authored May 14, 2024
1 parent dcc2096 commit d394b2f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ parameters:
- '#^Method .*::fromLivewire\(\) has no return type specified\.#'
- '#^Method .*::fromLivewire\(\) has parameter \$value with no type specified\.#'
- '#Call to an undefined method PowerComponents\\LivewirePowerGrid\\PowerGridComponent::datasource\(\).#'
-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
path: src/Support/PowerGridTableCache.php

-
message: "#^Unable to resolve the template type TValue in call to function collect$#"
count: 1
path: src/Support/PowerGridTableCache.php

paths:
- src

Expand Down
13 changes: 7 additions & 6 deletions src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Illuminate\Database\Eloquent\{Builder as EloquentBuilder, RelationNotFoundException};
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\{Cache, Schema};
use Illuminate\Support\Facades\Schema;
use PowerComponents\LivewirePowerGrid\Components\Filters\{Builders\Number};
use PowerComponents\LivewirePowerGrid\Support\PowerGridTableCache;

use PowerComponents\LivewirePowerGrid\{Column,
Components\Filters\Builders\Boolean,
Components\Filters\Builders\DatePicker,
Expand Down Expand Up @@ -317,12 +319,11 @@ private function getColumnType(string $modelTable, string $field): ?string
private function getColumnList(string $modelTable): array
{
try {
return (array) Cache::remember(
'powergrid_columns_in_' . $modelTable,
60 * 60 * 3,
return PowerGridTableCache::getOrCreate(
$modelTable,
fn () => collect(Schema::getColumns($modelTable))
->pluck('type', 'name')
->toArray()
->pluck('type', 'name')
->toArray()
);
} catch (\Throwable $throwable) {
logger()->warning('PowerGrid [getColumnList] warning: ', [
Expand Down
6 changes: 5 additions & 1 deletion src/Providers/PowerGridServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace PowerComponents\LivewirePowerGrid\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Support\Facades\{Blade, Event};
use Illuminate\Support\ServiceProvider;
use Livewire\Features\SupportLegacyModels\{EloquentCollectionSynth, EloquentModelSynth};
use Livewire\Livewire;
Expand All @@ -11,6 +12,7 @@
use PowerComponents\LivewirePowerGrid\Components\Actions\Macros;
use PowerComponents\LivewirePowerGrid\Components\Filters\FilterManager;
use PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager;
use PowerComponents\LivewirePowerGrid\Support\PowerGridTableCache;
use PowerComponents\LivewirePowerGrid\Themes\ThemeManager;
use PowerComponents\LivewirePowerGrid\{Livewire\LazyChild, Livewire\PerformanceCard, PowerGridManager};

Expand Down Expand Up @@ -56,6 +58,8 @@ public function register(): void

Livewire::component('lazy-child', LazyChild::class);

Event::listen(MigrationsEnded::class, fn () => PowerGridTableCache::forgetAll());

if (class_exists(\Laravel\Pulse\Facades\Pulse::class)) {
Livewire::component('powergrid-performance-card', PerformanceCard::class);
}
Expand Down
61 changes: 61 additions & 0 deletions src/Support/PowerGridTableCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Support;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\{Cache};

final class PowerGridTableCache
{
private const THREE_HOURS = 60 * 60 * 3;

private static string $cachedTablesListTag = 'pg_cached_tables';

private static string $cachedTableTag = 'powergrid_columns_in_';

public static function getOrCreate(string $tableName, callable $tableColumns): array
{
$tag = self::generateTag($tableName);

if (Cache::has($tag)) {
return (array) Cache::get($tag);
}

self::addToCachedTablesList($tag);

/** @phpstan-ignore-next-line */
return (array) Cache::remember($tag, self::THREE_HOURS, $tableColumns);
}

public static function forgetAll(): void
{
rescue(function (): void {
/** @phpstan-ignore-next-line */
self::list()->each(fn (string $tag): bool => Cache::forget($tag));

Cache::forget('pg_cached_tables');
}, report: false);
}

private static function list(): Collection
{
return collect(
/** @phpstan-ignore-next-line */
rescue(
fn () => (Cache::get(self::$cachedTablesListTag) ?? []),
[],
report: false
)
);
}

private static function addToCachedTablesList(string $tag): void
{
rescue(fn () => Cache::put(self::$cachedTablesListTag, self::list()->push($tag)->unique()->toArray()), report: false);
}

private static function generateTag(string $tableName): string
{
return self::$cachedTableTag . $tableName;
}
}

0 comments on commit d394b2f

Please sign in to comment.