Skip to content

Commit

Permalink
Enable config caching
Browse files Browse the repository at this point in the history
default 5 minutes (when testing set CONFIG_CACHE_TTL higher so issues are obvious)
lnms config:clear now clears both Laravel and LibreNMS config caches.  LibreNMS config cache will automatically regenerate on next application load.
  • Loading branch information
murrant committed Nov 26, 2024
1 parent ecc08bf commit 31db60b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 3 deletions.
16 changes: 14 additions & 2 deletions app/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ public function persist($key, $value): bool
// delete any children (there should not be any unless it is legacy)
\App\Models\Config::query()->where('config_name', 'like', "$key.%")->delete();

$this->invalidateCache(); // config has been changed, it will need to be reloaded

return true;
} catch (Exception $e) {
if (class_exists(Log::class)) {
Expand Down Expand Up @@ -316,6 +318,16 @@ public function getAll(): array
return $this->config;
}

/**
* Invalidate config cache (but don't reload)
* Next time the config is loaded, it will loaded fresh
* Because this is currently hardcoded to file cache, it will only clear the cache on this node
*/
public function invalidateCache(): void
{
Cache::driver('file')->forget('librenms-config');
}

/**
* merge the database config with the global config,
* global config overrides db
Expand Down Expand Up @@ -456,8 +468,8 @@ private function loadPostUserConfigDefaults(): void
if (! $this->has('snmp.unescape')) {
$this->persist('snmp.unescape', version_compare((new Version($this))->netSnmp(), '5.8.0', '<'));
}
if (! self::has('reporting.usage')) {
self::persist('reporting.usage', (bool) Callback::get('enabled'));
if (! $this->has('reporting.usage')) {
$this->persist('reporting.usage', (bool) Callback::get('enabled'));
}

// populate legacy DB credentials, just in case something external uses them. Maybe remove this later
Expand Down
26 changes: 26 additions & 0 deletions app/Console/Commands/ClearConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Console\Commands;

use App\Console\LnmsCommand;
use App\Facades\LibrenmsConfig;
use Illuminate\Foundation\Console\ConfigClearCommand;
use Symfony\Component\Console\Input\ArrayInput;

class ClearConfigCommand extends LnmsCommand
{
protected $name = 'config:clear';

/**
* Execute the console command.
*
* @return void
*/
public function handle(ConfigClearCommand $configClearCommand)
{
$configClearCommand->setLaravel($this->laravel);
$configClearCommand->run(new ArrayInput([]), $this->output);

LibrenmsConfig::invalidateCache();
}
}
3 changes: 3 additions & 0 deletions app/Console/Commands/DevicePoll.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Console\LnmsCommand;
use App\Events\DevicePolled;
use App\Facades\LibrenmsConfig;
use App\Jobs\PollDevice;
use App\Models\Device;
use App\Polling\Measure\MeasurementManager;
Expand Down Expand Up @@ -56,6 +57,8 @@ public function handle(MeasurementManager $measurements): int
if ($this->getOutput()->isVerbose()) {
Log::debug(Version::get()->header());
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
LibrenmsConfig::invalidateCache();
LibrenmsConfig::reload();
}

$module_overrides = Module::parseUserOverrides(explode(',', $this->option('modules') ?? ''));
Expand Down
2 changes: 1 addition & 1 deletion config/librenms.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@
| Amount of seconds to allow the config to be cached. 0 means no cache.
*/

'config_cache_ttl' => env('CONFIG_CACHE_TTL', 0),
'config_cache_ttl' => env('CONFIG_CACHE_TTL', 300),
];
2 changes: 2 additions & 0 deletions discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
echo "DEBUG!\n";
Debug::setVerbose(isset($options['v']));
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
LibrenmsConfig::invalidateCache();
LibrenmsConfig::reload();
}

if (! $where) {
Expand Down
3 changes: 3 additions & 0 deletions lang/en/commands.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

return [
'config:clear' => [
'description' => 'Clear config cache. This will allow any changes that have been made since the last full config load to be reflected in the current config.',
],
'config:get' => [
'description' => 'Get configuration value',
'arguments' => [
Expand Down
2 changes: 2 additions & 0 deletions scripts/collect-snmp-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@

echo 'Capturing Data: ';
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
LibrenmsConfig::invalidateCache();
LibrenmsConfig::reload();
$capture->captureFromDevice($device['device_id'], $prefer_new_snmprec, $full);
echo "\nVerify these file(s) do not contain any private data before sharing!\n";
} catch (InvalidModuleException $e) {
Expand Down
2 changes: 2 additions & 0 deletions scripts/save-test-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@
echo PHP_EOL;

\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
LibrenmsConfig::invalidateCache();
LibrenmsConfig::reload();
$tester = new ModuleTestHelper($modules, $target_os, $target_variant);
if (! $no_save && ! empty($output_file)) {
$tester->setJsonSavePath($output_file);
Expand Down
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,7 @@
}

\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
LibrenmsConfig::invalidateCache();
LibrenmsConfig::reload();

app()->terminate(); // destroy the bootstrap Laravel application

0 comments on commit 31db60b

Please sign in to comment.