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

View File

@@ -27,7 +27,6 @@
namespace LibreNMS\Traits; namespace LibreNMS\Traits;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use LibreNMS\Util\Laravel;
trait RuntimeClassCache trait RuntimeClassCache
{ {
@@ -45,9 +44,18 @@ trait RuntimeClassCache
protected function cacheGet(string $name, callable $actual) protected function cacheGet(string $name, callable $actual)
{ {
if (! array_key_exists($name, $this->runtimeCache)) { if (! array_key_exists($name, $this->runtimeCache)) {
$this->runtimeCache[$name] = $this->runtimeCacheExternalTTL && Laravel::isBooted() if ($this->runtimeCacheExternalTTL) {
? Cache::remember('runtimeCache' . __CLASS__ . $name, $this->runtimeCacheExternalTTL, $actual) try {
: $actual(); $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]; return $this->runtimeCache[$name];