Revert "Revert "Use flexible cache""

This reverts commit 3cfededd63.
This commit is contained in:
DariusIII
2024-09-13 22:24:15 +02:00
parent 0229d5e097
commit 94d3e539b9
3 changed files with 56 additions and 137 deletions
+10 -32
View File
@@ -363,16 +363,9 @@ class Category extends Model
*/
public static function getChildren($categoryId)
{
$expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
$result = Cache::get(md5($categoryId));
if ($result !== null) {
return $result;
}
$result = RootCategory::find($categoryId)->categories;
Cache::put(md5($categoryId), $result, $expiresAt);
return $result;
return Cache::flexible(md5($categoryId), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($categoryId) {
return RootCategory::find($categoryId)->categories;
});
}
/**
@@ -406,16 +399,9 @@ class Category extends Model
public static function getByIds($ids)
{
if (\count($ids) > 0) {
$expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
$result = Cache::get(md5(implode(',', $ids)));
if ($result !== null) {
return $result;
}
$result = self::query()->whereIn('id', $ids)->get();
Cache::put(md5(md5(implode(',', $ids))), $result, $expiresAt);
return $result;
return Cache::flexible(md5(md5(implode(',', $ids))), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($ids) {
return self::query()->whereIn('id', $ids)->get();
});
}
return false;
@@ -477,21 +463,13 @@ class Category extends Model
}
/**
* @return mixed
* Get categories for the API.
*/
public static function getForApi()
{
$expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
$result = Cache::get(md5('ForApi'));
if ($result !== null) {
return $result;
}
$result = RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get();
Cache::put(md5('ForApi'), $result, $expiresAt);
return $result;
return Cache::flexible('ForApi', [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () {
return RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get();
});
}
/**