-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_cache.py
More file actions
27 lines (20 loc) · 771 Bytes
/
Copy pathredis_cache.py
File metadata and controls
27 lines (20 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
import asyncio
from Waveform import get_direct_audio_url,extract_waveform_array
REDIS_KEY_PREFIX = "yt:waveform:"
async def generate_waveform_cached(yt_url, redis_client):
cache_key = REDIS_KEY_PREFIX + yt_url
# 1. Check cache
cached = await redis_client.get(cache_key)
if cached:
print("📦 Loaded waveform from cache")
return json.loads(cached)
# 2. If not in cache, generate it
def task():
audio_url = get_direct_audio_url(yt_url)
return extract_waveform_array(audio_url)
waveform = await asyncio.get_event_loop().run_in_executor(None, task)
# 3. Save to Redis
await redis_client.set(cache_key, json.dumps(waveform))
print("✅ Cached waveform in Redis")
return waveform