Spaces:
Runtime error
Runtime error
File size: 7,611 Bytes
162c9bb | 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | import os
import json
import hashlib
import time
import redis
from dotenv import load_dotenv
load_dotenv()
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
# Initialize Redis client with graceful fallback
_redis_client = None
def _get_redis():
"""Lazy-initialize Redis connection. Returns None if Redis is unavailable."""
global _redis_client
if _redis_client is not None:
return _redis_client
try:
_redis_client = redis.from_url(
REDIS_URL,
decode_responses=True,
socket_connect_timeout=3,
socket_timeout=3,
retry_on_timeout=True,
)
_redis_client.ping()
print(f"[Redis] Connected successfully to {REDIS_URL.split('@')[-1] if '@' in REDIS_URL else 'localhost'}")
return _redis_client
except Exception as e:
print(f"[Redis] Connection failed ({e}). App will run without cache (direct DB/API calls).")
_redis_client = None
return None
# βββ Core Cache Helpers βββββββββββββββββββββββββββββββββββββββββββββββ
def cache_get(key: str):
"""Get a cached JSON value by key. Returns None on miss or Redis error."""
try:
r = _get_redis()
if r is None:
return None
raw = r.get(key)
if raw is None:
return None
return json.loads(raw)
except Exception as e:
print(f"[Redis] GET error for '{key}': {e}")
return None
def cache_set(key: str, data, ttl_seconds: int = 3600):
"""Set a JSON value in cache with TTL. Fails silently on Redis error."""
try:
r = _get_redis()
if r is None:
return
r.setex(key, ttl_seconds, json.dumps(data, default=str))
except Exception as e:
print(f"[Redis] SET error for '{key}': {e}")
def cache_delete(key: str):
"""Delete a single cache key. Fails silently."""
try:
r = _get_redis()
if r is None:
return
r.delete(key)
except Exception as e:
print(f"[Redis] DELETE error for '{key}': {e}")
def cache_delete_pattern(pattern: str):
"""Delete all keys matching a glob pattern (e.g. 'jobs:feed:*'). Fails silently."""
try:
r = _get_redis()
if r is None:
return
cursor = 0
while True:
cursor, keys = r.scan(cursor, match=pattern, count=100)
if keys:
r.delete(*keys)
if cursor == 0:
break
except Exception as e:
print(f"[Redis] DELETE_PATTERN error for '{pattern}': {e}")
# βββ Specialized Helpers ββββββββββββββββββββββββββββββββββββββββββββββ
def hash_text(text: str) -> str:
"""SHA-256 hash of text for use as cache key component."""
return hashlib.sha256(text.encode("utf-8")).hexdigest()[:24]
def invalidate_user_cache(user_id: int):
"""Clear all cached data for a specific user (on resume upload, profile change, etc.)."""
keys = [
f"user:{user_id}:resume",
f"user:{user_id}:ai_profile",
f"user:{user_id}:resume_analysis",
f"user:{user_id}:saved_jobs",
f"user:{user_id}:profile",
]
try:
r = _get_redis()
if r is None:
return
r.delete(*keys)
print(f"[Redis] Invalidated all cache for user {user_id}")
except Exception as e:
print(f"[Redis] Error invalidating user {user_id} cache: {e}")
def invalidate_jobs_cache():
"""Clear all job-related caches (on scraper refresh)."""
cache_delete("jobs:locations")
cache_delete_pattern("jobs:feed:*")
cache_delete_pattern("search:*")
cache_delete_pattern("job:*")
# Also clear all users' saved_jobs caches since saved_jobs references are deleted by scraper
cache_delete_pattern("user:*:saved_jobs")
print("[Redis] Invalidated all job caches (feed, locations, search, saved_jobs)")
# βββ Redis Rate Limiting (Sorted Sets) βββββββββββββββββββββββββββββββ
def rate_limit_check(key: str, max_requests: int, window_seconds: int = 60) -> bool:
"""
Sliding window rate limiter using Redis Sorted Sets.
Returns True if request is allowed, raises nothing.
Returns False if rate limit exceeded.
"""
try:
r = _get_redis()
if r is None:
return True # If Redis is down, allow (fallback to in-memory)
now = time.time()
window_start = now - window_seconds
pipe = r.pipeline()
pipe.zremrangebyscore(key, 0, window_start) # Remove expired entries
pipe.zadd(key, {f"{now}": now}) # Add current request
pipe.zcard(key) # Count requests in window
pipe.expire(key, window_seconds + 10) # Auto-cleanup TTL
results = pipe.execute()
current_count = results[2]
if current_count > max_requests:
# Remove the entry we just added since it's over limit
r.zrem(key, f"{now}")
return False
return True
except Exception as e:
print(f"[Redis] Rate limit check error: {e}")
return True # Allow on Redis error
def token_bucket_consume(key: str, tokens: int, max_tokens: int, window_seconds: int = 1800):
"""
Token rate limiter using Redis hash with sliding window.
Returns (allowed: bool, remaining: int).
"""
try:
r = _get_redis()
if r is None:
return True, max_tokens # If Redis is down, allow
now = time.time()
bucket_key = f"tokens:{key}"
# Get current bucket state
pipe = r.pipeline()
pipe.hgetall(bucket_key)
result = pipe.execute()
bucket = result[0]
window_start = now - window_seconds
used = int(bucket.get("used", 0)) if bucket else 0
last_reset = float(bucket.get("reset_at", 0)) if bucket else 0
# Reset if window has passed
if last_reset < window_start:
used = 0
if used + tokens > max_tokens:
remaining = max(0, max_tokens - used)
return False, remaining
# Consume tokens
pipe2 = r.pipeline()
pipe2.hset(bucket_key, mapping={"used": used + tokens, "reset_at": str(now if last_reset < window_start else last_reset or now)})
pipe2.expire(bucket_key, window_seconds + 60)
pipe2.execute()
remaining = max(0, max_tokens - (used + tokens))
return True, remaining
except Exception as e:
print(f"[Redis] Token bucket error: {e}")
return True, max_tokens
def token_bucket_remaining(key: str, max_tokens: int, window_seconds: int = 1800) -> int:
"""Get remaining tokens for a user without consuming any."""
try:
r = _get_redis()
if r is None:
return max_tokens
now = time.time()
bucket_key = f"tokens:{key}"
bucket = r.hgetall(bucket_key)
if not bucket:
return max_tokens
window_start = now - window_seconds
last_reset = float(bucket.get("reset_at", 0))
if last_reset < window_start:
return max_tokens
used = int(bucket.get("used", 0))
return max(0, max_tokens - used)
except Exception as e:
print(f"[Redis] Token remaining check error: {e}")
return max_tokens
|