JSON 상태 저장소 DB 전환

This commit is contained in:
seo
2026-07-23 02:29:13 +09:00
parent 8cea4105eb
commit 1947b31c87
3 changed files with 106 additions and 52 deletions
+88 -48
View File
@@ -121,6 +121,62 @@ function dmesg_log(): array
];
}
function json_store_key_from_path(string $path): string
{
$name = preg_replace('/[^a-zA-Z0-9_.-]+/', '-', basename($path, '.json')) ?: md5($path);
return 'file:' . $name;
}
function json_store_read(string $key, ?string $legacyPath = null): ?array
{
try {
$stmt = db()->prepare("
SELECT payload, expires_at
FROM app_json_store
WHERE store_key = :store_key
AND (expires_at IS NULL OR expires_at >= NOW())
LIMIT 1
");
$stmt->execute([':store_key' => $key]);
$row = $stmt->fetch();
if ($row) {
$decoded = json_decode((string)$row['payload'], true);
if (is_array($decoded)) {
return $decoded;
}
}
} catch (Throwable) {
}
if ($legacyPath !== null && is_readable($legacyPath)) {
$decoded = json_decode((string)@file_get_contents($legacyPath), true);
if (is_array($decoded)) {
json_store_write($key, $decoded);
return $decoded;
}
}
return null;
}
function json_store_write(string $key, array $payload, ?int $ttlSeconds = null): void
{
$expiresAt = $ttlSeconds !== null ? date('Y-m-d H:i:s', time() + max(1, $ttlSeconds)) : null;
$stmt = db()->prepare("
INSERT INTO app_json_store (store_key, payload, expires_at)
VALUES (:store_key, :payload, :expires_at)
ON DUPLICATE KEY UPDATE
payload = VALUES(payload),
expires_at = VALUES(expires_at),
updated_at = CURRENT_TIMESTAMP
");
$stmt->execute([
':store_key' => $key,
':payload' => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
':expires_at' => $expiresAt,
]);
}
function throttled_event_status(?int $flags, string $raw, int $activeBit, int $seenBit, string $statePath): array
{
$now = time();
@@ -129,10 +185,6 @@ function throttled_event_status(?int $flags, string $raw, int $activeBit, int $s
$active = $flags !== null && (bool)($flags & $activeBit);
$seen = $flags !== null && (bool)($flags & $seenBit);
if (is_file($statePath) && !is_writable($statePath)) {
@chmod($statePath, 0666);
}
$state = [
'active' => false,
'active_since' => null,
@@ -143,12 +195,8 @@ function throttled_event_status(?int $flags, string $raw, int $activeBit, int $s
'events' => [],
];
$fp = @fopen($statePath, 'c+');
if ($fp !== false) {
@chmod($statePath, 0666);
@flock($fp, LOCK_EX);
$saved = stream_get_contents($fp);
$decoded = json_decode((string)$saved, true);
try {
$decoded = json_store_read(json_store_key_from_path($statePath), $statePath);
if (is_array($decoded)) {
$state = array_merge($state, array_intersect_key($decoded, $state));
}
@@ -204,15 +252,9 @@ function throttled_event_status(?int $flags, string $raw, int $activeBit, int $s
)), -200);
$state['updated_at'] = $now;
ftruncate($fp, 0);
rewind($fp);
fwrite($fp, json_encode($state, JSON_UNESCAPED_SLASHES));
fflush($fp);
@chmod($statePath, 0666);
json_store_write(json_store_key_from_path($statePath), $state);
}
@flock($fp, LOCK_UN);
fclose($fp);
} catch (Throwable) {
}
$duration = 0;
@@ -885,7 +927,8 @@ function battery_profile_history(int $days = 45): array
static $cache = [];
$cacheKey = (string)$days;
$now = time();
$cacheFile = sys_get_temp_dir() . '/control-battery-profile-' . $days . 'd.json';
$storeKey = 'battery_profile:' . $days . 'd';
$legacyCacheFile = sys_get_temp_dir() . '/control-battery-profile-' . $days . 'd.json';
if (
isset($cache[$cacheKey])
@@ -895,21 +938,18 @@ function battery_profile_history(int $days = 45): array
return $cache[$cacheKey]['rows'];
}
if (is_readable($cacheFile)) {
$raw = @file_get_contents($cacheFile);
$decoded = is_string($raw) ? json_decode($raw, true) : null;
if (
is_array($decoded)
&& isset($decoded['created_at'], $decoded['rows'])
&& is_array($decoded['rows'])
&& ($now - (int)$decoded['created_at']) < 21600
) {
$cache[$cacheKey] = [
'time' => $now,
'rows' => $decoded['rows'],
];
return $decoded['rows'];
}
$decoded = json_store_read($storeKey, $legacyCacheFile);
if (
is_array($decoded)
&& isset($decoded['created_at'], $decoded['rows'])
&& is_array($decoded['rows'])
&& ($now - (int)$decoded['created_at']) < 21600
) {
$cache[$cacheKey] = [
'time' => $now,
'rows' => $decoded['rows'],
];
return $decoded['rows'];
}
$maxId = (int)db()->query("SELECT MAX(id) FROM sensor_logs")->fetchColumn();
@@ -952,14 +992,11 @@ function battery_profile_history(int $days = 45): array
'time' => $now,
'rows' => $rows,
];
@file_put_contents(
$cacheFile,
json_encode([
'created_at' => $now,
'days' => $days,
'rows' => $rows,
], JSON_UNESCAPED_UNICODE)
);
json_store_write($storeKey, [
'created_at' => $now,
'days' => $days,
'rows' => $rows,
], 21600);
return $rows;
}
@@ -3405,12 +3442,12 @@ function systemd_service_logs(string $unit, int $limit = 12): array
function custom_systemd_services(): array
{
$cachePath = '/tmp/control-custom-services.json';
$cacheSeconds = (int)setting_value('display.custom_service_cache_seconds');
if (is_file($cachePath) && filemtime($cachePath) >= time() - $cacheSeconds) {
$cached = json_decode((string)@file_get_contents($cachePath), true);
if (is_array($cached)) {
return $cached;
$cacheKey = 'custom_services';
$cached = json_store_read($cacheKey, '/tmp/control-custom-services.json');
if (is_array($cached) && isset($cached['created_at'], $cached['rows']) && is_array($cached['rows'])) {
if (time() - (int)$cached['created_at'] <= $cacheSeconds) {
return $cached['rows'];
}
}
@@ -3473,7 +3510,10 @@ function custom_systemd_services(): array
];
}
@file_put_contents($cachePath, json_encode($rows, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
json_store_write($cacheKey, [
'created_at' => time(),
'rows' => $rows,
], $cacheSeconds);
return $rows;
}