feat: cache analysis results alongside audio files
After the first analysis of a WAV/FLAC file, the result is written to <filename>.analysis.json next to the audio. Subsequent requests with the same threshold and min_gap parameters return the cached result immediately without re-reading the audio data. The cache is invalidated automatically if either parameter changes. Written via temp-then-replace for thread safety. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -331,6 +331,15 @@ class _Handler(BaseHTTPRequestHandler):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
cache_path = path.parent / (path.name + '.analysis.json')
|
||||
try:
|
||||
cached = json.loads(cache_path.read_text('utf-8'))
|
||||
if cached.get('threshold') == threshold and cached.get('min_gap') == min_gap:
|
||||
self._send(200, json.dumps(cached['result']).encode('utf-8'), 'application/json')
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ext = path.suffix.lower()
|
||||
if ext == '.wav':
|
||||
result = analyze_wav(path, threshold=threshold, min_gap=min_gap)
|
||||
@@ -343,6 +352,13 @@ class _Handler(BaseHTTPRequestHandler):
|
||||
self._json_err(400, f'Loudness analysis is not available for {ext} files')
|
||||
return
|
||||
|
||||
try:
|
||||
tmp = cache_path.with_suffix('.tmp')
|
||||
tmp.write_text(json.dumps({'threshold': threshold, 'min_gap': min_gap, 'result': result}), 'utf-8')
|
||||
os.replace(tmp, cache_path)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self._send(200, json.dumps(result).encode('utf-8'), 'application/json')
|
||||
|
||||
def _api_status(self):
|
||||
|
||||
Reference in New Issue
Block a user