|
24 | 24 | use FacturaScripts\Core\Cache; |
25 | 25 | use FacturaScripts\Core\DbQuery; |
26 | 26 | use FacturaScripts\Core\DbUpdater; |
| 27 | +use FacturaScripts\Core\Internal\CacheWithMemory; |
27 | 28 | use FacturaScripts\Core\Lib\Import\CSVImport; |
28 | 29 | use FacturaScripts\Core\Tools; |
29 | 30 | use FacturaScripts\Core\Where; |
@@ -184,9 +185,9 @@ public function clear(): void |
184 | 185 |
|
185 | 186 | public function clearCache(): void |
186 | 187 | { |
187 | | - Cache::deleteMulti('model-' . $this->modelClassName() . '-'); |
188 | | - Cache::deleteMulti('join-model-'); |
189 | | - Cache::deleteMulti('table-' . static::tableName() . '-'); |
| 188 | + CacheWithMemory::deleteMulti('model-' . $this->modelClassName() . '-'); |
| 189 | + CacheWithMemory::deleteMulti('join-model-'); |
| 190 | + CacheWithMemory::deleteMulti('table-' . static::tableName() . '-'); |
190 | 191 | } |
191 | 192 |
|
192 | 193 | public function delete(): bool |
@@ -645,9 +646,15 @@ protected function belongsTo(string $modelName, string $foreignKey): ?object |
645 | 646 | $modelName = end($parts); |
646 | 647 | } |
647 | 648 |
|
648 | | - $modelClass = '\\FacturaScripts\\Dinamic\\Model\\' . $modelName; |
649 | | - $model = new $modelClass(); |
650 | | - return $model->load($this->{$foreignKey}) ? $model : null; |
| 649 | + // Cache key for this relationship |
| 650 | + $key = $this->{$foreignKey}; |
| 651 | + $cacheKey = 'model-' . $modelName . '-' . $key; |
| 652 | + |
| 653 | + return Cache::withMemory()->remember($cacheKey, function () use ($modelName, $key) { |
| 654 | + $modelClass = '\\FacturaScripts\\Dinamic\\Model\\' . $modelName; |
| 655 | + $model = new $modelClass(); |
| 656 | + return $model->load($key) ? $model : null; |
| 657 | + }); |
651 | 658 | } |
652 | 659 |
|
653 | 660 | protected static function db(): DataBase |
|
0 commit comments