A free open source IT asset/license management system
Cache adapters to prevent N+1
snipe8df2eb45769c
diff --git a/app/Models/SyncAdapterConfig.php b/app/Models/SyncAdapterConfig.phpindex ebd004a6b9..ef7d7fe587 100644--- a/app/Models/SyncAdapterConfig.php+++ b/app/Models/SyncAdapterConfig.php@@ -26,9 +26,12 @@ namespace App\Models;*/class SyncAdapterConfig{+ // Cache the sync adapters to prevent N+1+ private static array $instanceCache = [];+public static function get(int $instanceId, string $configKey, mixed $default = null): mixed{- $instance = SyncAdapterInstance::find($instanceId);+ $instance = self::instance($instanceId);if ($instance === null) {return $default;}@@ -40,7 +43,7 @@ class SyncAdapterConfigpublic static function put(int $instanceId, string $configKey, ?string $value): void{- $instance = SyncAdapterInstance::find($instanceId);+ $instance = self::instance($instanceId);if ($instance === null) {return;}@@ -53,7 +56,7 @@ class SyncAdapterConfigpublic static function has(int $instanceId, string $configKey): bool{- $instance = SyncAdapterInstance::find($instanceId);+ $instance = self::instance($instanceId);if ($instance === null) {return false;}@@ -65,7 +68,7 @@ class SyncAdapterConfigpublic static function forget(int $instanceId, string $configKey): void{- $instance = SyncAdapterInstance::find($instanceId);+ $instance = self::instance($instanceId);if ($instance === null) {return;}@@ -89,6 +92,20 @@ class SyncAdapterConfig*/public static function listForInstance(int $instanceId): array{- return SyncAdapterInstance::find($instanceId)?->config ?? [];+ return self::instance($instanceId)?->config ?? [];+ }++ public static function flushInstanceCache(): void+ {+ self::$instanceCache = [];+ }++ private static function instance(int $instanceId): ?SyncAdapterInstance+ {+ if (array_key_exists($instanceId, self::$instanceCache)) {+ return self::$instanceCache[$instanceId];+ }++ return self::$instanceCache[$instanceId] = SyncAdapterInstance::find($instanceId);}}diff --git a/tests/TestCase.php b/tests/TestCase.phpindex d92beb12cc..0fb5e1c5a2 100644--- a/tests/TestCase.php+++ b/tests/TestCase.php@@ -54,6 +54,10 @@ abstract class TestCase extends BaseTestCase// increment may hand the same id to a different test's user with a// different pivot set.Company::flushCompanyIdsCache();++ // Sync-adapter instance cache is another static that would hold+ // rolled-back model references between tests otherwise.+ \App\Models\SyncAdapterConfig::flushInstanceCache();}// ...existing code...