-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add opt-out flags for token negative cache and rate limiting #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,23 +230,25 @@ async def _migrate_poster_rating_format_raw(self, token: str, redis_key: str, da | |
| @alru_cache(maxsize=2000, ttl=43200) | ||
| async def get_user_data(self, token: str) -> dict[str, Any] | None: | ||
| # Short-circuit for tokens known to be missing | ||
| try: | ||
| if token in self._missing_tokens: | ||
| logger.debug(f"[REDIS] Negative cache hit for missing token {token}") | ||
| return None | ||
| except Exception as e: | ||
| logger.debug(f"Failed to check negative cache for {token}: {e}") | ||
| if settings.ENABLE_TOKEN_NEGATIVE_CACHE: | ||
| try: | ||
| if token in self._missing_tokens: | ||
| logger.debug(f"[REDIS] Negative cache hit for missing token {token}") | ||
| return None | ||
|
Comment on lines
+235
to
+237
|
||
| except Exception as e: | ||
| logger.debug(f"Failed to check negative cache for {token}: {e}") | ||
|
Comment on lines
+233
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve readability and reduce nesting, you can combine the feature flag check with the cache lookup. Python's try:
if settings.ENABLE_TOKEN_NEGATIVE_CACHE and token in self._missing_tokens:
logger.debug(f"[REDIS] Negative cache hit for missing token {token}")
return None
except Exception as e:
logger.debug(f"Failed to check negative cache for {token}: {e}") |
||
|
|
||
| logger.debug(f"[REDIS] Cache miss. Fetching data from redis for {token}") | ||
| key = self._format_key(token) | ||
|
Comment on lines
+239
to
242
|
||
| data_raw = await redis_service.get(key) | ||
|
|
||
| if not data_raw: | ||
| # remember negative result briefly | ||
| try: | ||
| self._missing_tokens[token] = True | ||
| except Exception as e: | ||
| logger.debug(f"Failed to set negative cache for missing token {token}: {e}") | ||
| if settings.ENABLE_TOKEN_NEGATIVE_CACHE: | ||
| try: | ||
| self._missing_tokens[token] = True | ||
| except Exception as e: | ||
| logger.debug(f"Failed to set negative cache for missing token {token}: {e}") | ||
| return None | ||
|
|
||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
@alru_cacheon get_user_data, results are cached per token at this layer. That means disabling ENABLE_TOKEN_NEGATIVE_CACHE may still leave a replica returning a cached “missing” (None) response for some period, so it may not achieve the goal of always re-checking Redis for previously-missing tokens in multi-replica deployments. Consider conditionally bypassing the alru_cache when negative caching is disabled, or ensuring that missing-token results are not cached / have a very short TTL.