Skip to content

Commit 99ab840

Browse files
⚡ perf: Add Redis caching to /v1/analytics/{user_email} with privacy-safe email hashing
1 parent 19c4d05 commit 99ab840

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

api/v1/analytics.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Standard library imports
44
import datetime
5+
import hashlib
56
import html
67
import json
78
import logging
@@ -94,6 +95,11 @@ def cache_analytics(
9495
pass
9596

9697

98+
def hash_email(email: str) -> str:
99+
"""Hash email for privacy-safe cache keys."""
100+
return hashlib.sha256(email.lower().encode()).hexdigest()[:16]
101+
102+
97103
class ShieldOnException(Exception):
98104
"""Exception raised when shield is on."""
99105

@@ -1050,12 +1056,20 @@ async def get_analytics(
10501056
alert_key = data_store.key("xon_alert", user_email)
10511057
alert_record = data_store.get(alert_key)
10521058

1059+
# Always check shieldOn first (privacy - can't cache this)
10531060
if alert_record and alert_record.get("shieldOn"):
10541061
raise ShieldOnException("Shield is on")
10551062

1063+
# Check cache after shieldOn validation
1064+
cache_key = f"analytics-hierarchy:{hash_email(user_email)}"
1065+
cached_result = get_cached_analytics(cache_key)
1066+
if cached_result:
1067+
return BreachHierarchyResponse(**cached_result)
1068+
10561069
if xon_record:
10571070
site = str(xon_record["site"])
10581071
breach_hierarchy = await get_breach_hierarchy_analytics(site, "")
1072+
cache_analytics(cache_key, breach_hierarchy)
10591073
return BreachHierarchyResponse(**breach_hierarchy)
10601074

10611075
return JSONResponse(content=None)

0 commit comments

Comments
 (0)