Added caching to the settings helper so that we avoid unnecessary queries to the database.

This commit is contained in:
Alex Tselegidis 2021-12-17 08:44:38 +01:00
parent e071ec46dc
commit 775d385e09

View file

@ -45,6 +45,8 @@ if ( ! function_exists('setting'))
throw new InvalidArgumentException('The $key argument cannot be empty.'); throw new InvalidArgumentException('The $key argument cannot be empty.');
} }
$cache = config('settings');
if (is_array($key)) if (is_array($key))
{ {
foreach ($key as $name => $value) foreach ($key as $name => $value)
@ -60,13 +62,22 @@ if ( ! function_exists('setting'))
} }
$CI->settings_model->save($setting); $CI->settings_model->save($setting);
$cache[$name] = $value;
} }
config(['settings' => $cache]);
return NULL; return NULL;
} }
if (empty($cache[$key]))
{
$setting = $CI->settings_model->query()->where('name', $key)->get()->row_array(); $setting = $CI->settings_model->query()->where('name', $key)->get()->row_array();
return $setting['value'] ?? $default; $cache[$key] = $setting['value'] ?? NULL;
}
return $cache[$key] ?? $default;
} }
} }