From: Vincent Vanwaelscappel Date: Thu, 16 Jun 2022 08:36:59 +0000 (+0200) Subject: wip #4210 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=39f1566e32239ec89f754c96d04c6a620680f723;p=cubist_cms-back.git wip #4210 --- diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index ad147d9..ed6cd0e 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -42,11 +42,8 @@ class Model extends Field $tag = 'model_' . $modelClass; if ($force || !isset(static::$_options[$globalCacheKey])) { - if ($force) { - cache()->forget($globalCacheKey); - } start_measure($globalCacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr); - static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope) { + $closure = function () use ($modelClass, $attr, $modelScope) { set_time_limit(0); /** @var \Illuminate\Database\Eloquent\Model $inst */ $inst = new $modelClass(); @@ -56,7 +53,15 @@ class Model extends Field $q = $q->$modelScope(); } return $q->where('created_ok', 1)->get()->pluck($attr, $inst->getKeyName())->toArray(); - }); + }; + $ttl = 86400; + $cache = cache()->tags([$tag]); + if ($force) { + static::$_options[$globalCacheKey] = $closure(); + $cache->put($globalCacheKey, $ttl, static::$_options[$globalCacheKey]); + } else { + static::$_options[$globalCacheKey] = $cache->remember($globalCacheKey, $ttl, $closure); + } stop_measure($globalCacheKey); } @@ -77,10 +82,7 @@ class Model extends Field return static::$_options[$cacheKey]; } - if ($force || !isset(static::$_options[$cacheKeysKey])) { - if ($force ) { - cache()->forget($cacheKeysKey); - } + if (!isset(static::$_options[$cacheKeysKey])) { static::$_options[$cacheKeysKey] = cache()->tags([$tag])->remember($cacheKeysKey, 86400, function () use ($modelClass, $modelOrderBy, $modelOrderByWay) { set_time_limit(0); /** @var Builder $q */ @@ -92,9 +94,6 @@ class Model extends Field }); } - if ($force) { - cache()->forget($cacheKey); - } static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($globalCacheKey, $cacheKeysKey) { $res = []; foreach (static::$_options[$cacheKeysKey] as $k) {