Repository navigation
Expand file tree
/
Copy pathavatar_cache.py
More file actions
215 lines (173 loc) · 8.16 KB
/
Copy pathavatar_cache.py
File metadata and controls
215 lines (173 loc) · 8.16 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""Channel avatar cache with background fetching."""
import asyncio
import logging
import time
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
import innertube
import invidious_proxy
from converters import resolve_invidious_url
from settings import get_settings
logger = logging.getLogger("avatar_cache")
@dataclass
class CachedAvatar:
"""Cached avatar data."""
channel_id: str
thumbnails: List[Dict[str, Any]]
cached_at: float
def is_expired(self) -> bool:
s = get_settings()
return time.time() - self.cached_at > s.cache_avatar_ttl
class AvatarCache:
"""In-memory cache for channel avatars with background fetching."""
def __init__(self):
self._cache: Dict[str, CachedAvatar] = {}
self._pending: set[str] = set() # Channel IDs currently being fetched
self._lock = asyncio.Lock()
self._fetch_semaphore = asyncio.Semaphore(5) # Limit concurrent avatar fetches
async def get(self, channel_id: str) -> Optional[List[Dict[str, Any]]]:
"""Get cached avatar thumbnails for a channel.
Returns None if not cached or expired.
"""
cached = self._cache.get(channel_id)
if cached and not cached.is_expired():
logger.debug(f"[AvatarCache] Cache hit for {channel_id}")
return cached.thumbnails
return None
async def fetch_and_cache(self, channel_id: str) -> Optional[List[Dict[str, Any]]]:
"""Fetch avatar via InnerTube first, then Invidious fallback.
Returns the thumbnails if successful, None otherwise.
"""
async with self._lock:
if channel_id in self._pending:
logger.debug(f"[AvatarCache] Already fetching {channel_id}")
return None
self._pending.add(channel_id)
try:
thumbnails = await self._fetch_innertube(channel_id) or await self._fetch_invidious(channel_id)
if thumbnails:
await self._evict_if_needed()
self._cache[channel_id] = CachedAvatar(
channel_id=channel_id, thumbnails=thumbnails, cached_at=time.time()
)
logger.info(f"[AvatarCache] Cached avatar for {channel_id} ({len(thumbnails)} thumbnails)")
return thumbnails
except (KeyError, TypeError, ValueError) as e:
logger.error(f"[AvatarCache] Unexpected error fetching avatar for {channel_id}: {e}")
finally:
async with self._lock:
self._pending.discard(channel_id)
return None
async def _fetch_innertube(self, channel_id: str) -> Optional[List[Dict[str, Any]]]:
"""Fetch avatar thumbnails from InnerTube."""
try:
channel_info = await innertube.get_channel_info(channel_id)
if channel_info and channel_info.get("authorThumbnails"):
return channel_info["authorThumbnails"]
except innertube.InnerTubeError as e:
logger.debug(f"[AvatarCache] InnerTube failed for {channel_id}: {e}")
return None
async def _fetch_invidious(self, channel_id: str) -> Optional[List[Dict[str, Any]]]:
"""Fetch avatar thumbnails from Invidious."""
if not invidious_proxy.is_enabled():
return None
try:
channel_data = await invidious_proxy.get_channel(channel_id)
if channel_data and "authorThumbnails" in channel_data:
invidious_base = invidious_proxy.get_base_url()
return [
{**thumb, "url": resolve_invidious_url(thumb.get("url", ""), invidious_base)}
for thumb in channel_data.get("authorThumbnails", [])
]
except invidious_proxy.InvidiousProxyError as e:
logger.warning(f"[AvatarCache] Invidious failed for {channel_id}: {e}")
return None
def schedule_background_fetch(self, channel_id: str):
"""Schedule a background fetch for a channel avatar.
This is fire-and-forget - doesn't block the caller.
"""
logger.debug(f"[AvatarCache] schedule_background_fetch called for {channel_id}")
# Skip if already cached and not expired
cached = self._cache.get(channel_id)
if cached and not cached.is_expired():
logger.debug(f"[AvatarCache] Avatar already cached for {channel_id}, skipping")
return
# Skip if already being fetched
if channel_id in self._pending:
logger.debug(f"[AvatarCache] Avatar fetch already pending for {channel_id}, skipping")
return
try:
logger.info(f"[AvatarCache] Creating background task for {channel_id}")
asyncio.create_task(self._background_fetch(channel_id))
logger.info(f"[AvatarCache] Background task created successfully for {channel_id}")
except RuntimeError as e:
logger.error(f"[AvatarCache] Failed to create background task for {channel_id}: {e}")
except Exception as e:
logger.error(f"[AvatarCache] Unexpected error scheduling fetch for {channel_id}: {e}", exc_info=True)
async def _background_fetch(self, channel_id: str):
"""Background task to fetch and cache avatar with rate limiting."""
async with self._fetch_semaphore:
try:
await self.fetch_and_cache(channel_id)
except Exception as e:
logger.error(f"[AvatarCache] Background fetch error for {channel_id}: {e}", exc_info=True)
async def _evict_if_needed(self):
"""Evict oldest entries if cache exceeds max size."""
# Note: avatar_cache_max_size is not in settings, use a reasonable default
max_size = 10000
if len(self._cache) >= max_size:
# Sort by cached_at and remove oldest 10%
sorted_entries = sorted(self._cache.items(), key=lambda x: x[1].cached_at)
to_remove = len(self._cache) // 10 or 1
for channel_id, _ in sorted_entries[:to_remove]:
del self._cache[channel_id]
logger.info(f"[AvatarCache] Evicted {to_remove} old entries")
def cleanup_expired(self):
"""Remove all expired entries from cache."""
expired = [channel_id for channel_id, cached in self._cache.items() if cached.is_expired()]
for channel_id in expired:
del self._cache[channel_id]
if expired:
logger.info(f"[AvatarCache] Cleaned up {len(expired)} expired entries")
def stats(self) -> Dict[str, Any]:
"""Return cache statistics."""
s = get_settings()
expired_count = sum(1 for c in self._cache.values() if c.is_expired())
return {
"total_entries": len(self._cache),
"expired_entries": expired_count,
"pending_fetches": len(self._pending),
"max_size": 10000,
"ttl_seconds": s.cache_avatar_ttl,
}
# Global cache instance
_avatar_cache: Optional[AvatarCache] = None
def get_cache() -> AvatarCache:
"""Get or create the global avatar cache."""
global _avatar_cache
if _avatar_cache is None:
_avatar_cache = AvatarCache()
return _avatar_cache
# --- Periodic cleanup ---
CLEANUP_INTERVAL = 3600 # Clean up expired avatars every hour
_cleanup_task: Optional[asyncio.Task] = None
async def _avatar_cleanup_loop():
"""Periodically clean up expired avatar cache entries."""
while True:
await asyncio.sleep(CLEANUP_INTERVAL)
try:
get_cache().cleanup_expired()
except Exception as e:
logger.error(f"[AvatarCache] Cleanup loop error: {e}", exc_info=True)
def start_avatar_cleanup_task():
"""Start the periodic avatar cache cleanup task."""
global _cleanup_task
if _cleanup_task is None or _cleanup_task.done():
_cleanup_task = asyncio.create_task(_avatar_cleanup_loop())
logger.info(f"[AvatarCache] Started periodic cleanup task (interval: {CLEANUP_INTERVAL}s)")
def stop_avatar_cleanup_task():
"""Stop the periodic avatar cache cleanup task."""
global _cleanup_task
if _cleanup_task and not _cleanup_task.done():
_cleanup_task.cancel()
logger.info("[AvatarCache] Stopped periodic cleanup task")