mailwolt/app/Console/Commands/SettingsSyncCommand.php

35 lines
944 B
PHP

<?php
namespace App\Console\Commands;
use App\Models\Setting;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class SettingsSyncCommand extends Command
{
protected $signature = 'settings:sync';
protected $description = 'Synchronize all DB settings into Redis';
public function handle(): int
{
$count = 0;
$this->info('Syncing settings to Redis...');
Setting::chunk(200, function ($settings) use (&$count) {
foreach ($settings as $setting) {
$key = "settings:{$setting->group}:{$setting->key}";
try {
Redis::set($key, $setting->value);
$count++;
} catch (\Throwable $e) {
$this->error("Failed to write {$key}");
}
}
});
$this->info("✅ Synced {$count} settings to Redis.");
return self::SUCCESS;
}
}