Files
weval-l99/fix_em_live_cache.py
2026-04-20 13:36:02 +02:00

41 lines
1.0 KiB
Python

#!/usr/bin/env python3
# V84: Add file-based cache 5s to em-live-kpi.php (reduce 9.6s→ms via cache)
path = "/var/www/html/api/em-live-kpi.php"
with open(path, "rb") as f:
raw = f.read()
if b"V84 cache" in raw:
print("ALREADY")
exit(0)
# Insert cache logic after <?php
old = b"<?php"
new = b"""<?php
// V84 cache: 5s TTL to avoid 9s shell_exec cascade
$_emcache = '/tmp/em-live-kpi.cache.json';
if (file_exists($_emcache) && (time() - filemtime($_emcache)) < 5) {
header('Content-Type: application/json');
header('X-Cache: HIT');
echo file_get_contents($_emcache);
exit;
}
register_shutdown_function(function() use ($_emcache) {
// Write cache after output buffered
$out = ob_get_clean();
if ($out && strlen($out) > 100) {
@file_put_contents($_emcache, $out);
}
echo $out;
});
ob_start();
"""
if old in raw:
idx = raw.find(old)
raw = raw[:idx] + new + raw[idx+len(old):]
with open(path, "wb") as f:
f.write(raw)
print(f"PATCHED size: {len(raw)}")
else:
print("PHP_TAG_NOT_FOUND")