-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear Table Cache when running migrations (#1531)
* create TableCache support class * Forget PowerGrid table cache on migrations * replace Cache with PowerGridTableCache * ignore template error
- Loading branch information
1 parent
dcc2096
commit d394b2f
Showing
4 changed files
with
83 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |