Fix runtime cache (#16272)

Fix runtime cache when redis is enabled, but not running
This commit is contained in:
Tony Murray 2024-08-06 13:11:44 -05:00 committed by GitHub
parent d1ee543b6b
commit cc971b6233
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,7 +27,6 @@
namespace LibreNMS\Traits;
use Illuminate\Support\Facades\Cache;
use LibreNMS\Util\Laravel;
trait RuntimeClassCache
{
@ -45,9 +44,18 @@ trait RuntimeClassCache
protected function cacheGet(string $name, callable $actual)
{
if (! array_key_exists($name, $this->runtimeCache)) {
$this->runtimeCache[$name] = $this->runtimeCacheExternalTTL && Laravel::isBooted()
? Cache::remember('runtimeCache' . __CLASS__ . $name, $this->runtimeCacheExternalTTL, $actual)
: $actual();
if ($this->runtimeCacheExternalTTL) {
try {
$this->runtimeCache[$name] = Cache::remember('runtimeCache' . __CLASS__ . $name, $this->runtimeCacheExternalTTL, $actual);
return $this->runtimeCache[$name]; // system cache success, don't use local cache
} catch (\Exception $e) {
// go to fallback code
}
}
// non-persistent cache
$this->runtimeCache[$name] = $actual();
}
return $this->runtimeCache[$name];