File size: 8,470 Bytes
25834e9 | 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 | """URL-specific Redis cache.
Stores UrlCacheData as JSON (not pickle) so cache entries are debuggable and
safe to deserialise across Python versions. Keys are scoped by fqdn:
``url_cache:<domain>:<alias>``.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import redis.asyncio as aioredis
from pydantic import BaseModel, ConfigDict, Field
from app.infrastructure.crypto import verify_password as verify_password_hash
from app.infrastructure.logging import get_logger
from app.schemas.models.url import AbVariant
from app.shared.datetime_utils import to_unix_timestamp, to_unix_timestamp_ceil
if TYPE_CHECKING:
from app.schemas.models.url import UrlV2Doc
log = get_logger(__name__)
class UrlCacheData(BaseModel):
"""Unified cache schema covering both v1 (legacy) and v2 URLs."""
model_config = ConfigDict(populate_by_name=True)
id: str = Field(alias="_id") # accepts "_id" from cached JSON, stored as "id"
alias: str
long_url: str
block_bots: bool
password_hash: str | None
# Unix timestamp; None = no expiry OR tz-ambiguous v1 value (never expires)
expiration_time: int | None
max_clicks: int | None
url_status: str # ACTIVE, INACTIVE, BLOCKED, EXPIRED
schema_version: str # "v1" or "v2"
owner_id: str | None # ObjectId as string; None for v1 URLs
total_clicks: int = 0 # Live click count for v1 max-clicks check
domain: str = ""
# ISO alpha-2 country code → destination URL. None for v1/legacy and
# non-geo links; entries cached before this field existed deserialize
# to None (default), so no cache version bump is needed.
geo_rules: dict[str, str] | None = None
# v2 only; served with 302 once the link is EXPIRED.
expired_redirect_url: str | None = None
# Same decoding story as geo_rules: absent on older entries → None.
ab_variants: list[AbVariant] | None = None
# Custom meta-tags (v2 only; None = feature disabled on this link).
# meta_title is the enabled-signal: LinkMetaTags.title is mandatory.
meta_title: str | None = None
meta_description: str | None = None
meta_image: str | None = None
meta_color: str | None = None
meta_image_width: int | None = None # from async image validation
meta_image_height: int | None = None
# Unix timestamp before which the link is not live; None = live now.
# Entries cached before this field existed deserialize to None.
start_time: int | None = None
pre_start_url: str | None = None
@classmethod
def from_v2_doc(cls, doc: UrlV2Doc) -> UrlCacheData:
"""Project a UrlV2Doc into the hot-path cache shape.
On the model (not a service helper) because three callers need it —
resolve, the edge write-through, the image validator — and a shared
derived projection belongs with the data, not behind a private
cross-module import.
"""
im = doc.meta_tags.image_meta if doc.meta_tags else None
return cls(
id=str(doc.id),
alias=doc.alias,
long_url=doc.long_url,
block_bots=bool(doc.block_bots),
password_hash=doc.password,
# to_unix_timestamp treats naive as UTC — v2 datetimes come back
# from Mongo naive-UTC, so this stays host-TZ-independent.
expiration_time=to_unix_timestamp(doc.expire_after),
max_clicks=doc.max_clicks,
url_status=doc.status,
schema_version="v2",
owner_id=str(doc.owner_id) if doc.owner_id else None,
domain=doc.domain,
geo_rules=doc.geo_rules,
expired_redirect_url=doc.expired_redirect_url,
ab_variants=doc.ab_variants,
meta_title=doc.meta_tags.title if doc.meta_tags else None,
meta_description=doc.meta_tags.description if doc.meta_tags else None,
meta_image=doc.meta_tags.image if doc.meta_tags else None,
meta_color=doc.meta_tags.color if doc.meta_tags else None,
meta_image_width=im.width if im else None,
meta_image_height=im.height if im else None,
# Ceil: a fractional start must never read as live a moment early.
start_time=to_unix_timestamp_ceil(doc.starts_at),
pre_start_url=doc.pre_start_url,
)
def is_not_yet_live(self, now_ts: float) -> bool:
"""Start-time check for the hot path. Strict ``>``: the link goes
live at the exact second, the mirror of ``is_time_expired``'s
``<=`` boundary."""
return self.start_time is not None and self.start_time > now_ts
def is_time_expired(self, now_ts: float) -> bool:
"""Time-lapse check for the hot path. ``expiration_time`` is None
for links without expiry AND for v1 rows whose stored value is
tz-ambiguous (see ``_legacy_doc_to_cache``) — both never expire here.
"""
return self.expiration_time is not None and self.expiration_time <= now_ts
def verify_password(self, password: str | None) -> bool:
"""Check a password against this URL's stored hash.
Handles schema-specific hashing: argon2 for v2, plaintext for v1/emoji.
Returns True if no password is set or if the password matches.
"""
if not self.password_hash:
return True
if password is None:
return False
if self.schema_version == "v2":
return verify_password_hash(password, self.password_hash)
return password == self.password_hash
class UrlCache:
def __init__(
self, redis_client: aioredis.Redis | None, ttl_seconds: int = 300
) -> None:
self._redis = redis_client
self.ttl_seconds = ttl_seconds
def _key(self, short_code: str, domain: str) -> str:
return f"url_cache:{domain}:{short_code}"
async def get(self, short_code: str, domain: str) -> UrlCacheData | None:
if self._redis is None:
return None
try:
raw = await self._redis.get(self._key(short_code, domain))
if raw is None:
return None
return UrlCacheData.model_validate_json(raw)
except Exception as e:
log.warning("url_cache_get_error", short_code=short_code, error=str(e))
return None
async def set(self, short_code: str, data: UrlCacheData) -> None:
# Domain is read from `data.domain` — caller always has the doc and
# the doc's domain is the canonical source. No redundant parameter.
if self._redis is None:
return
try:
await self._redis.setex(
self._key(short_code, data.domain),
self.ttl_seconds,
data.model_dump_json(by_alias=True),
)
except Exception as e:
log.error("url_cache_set_error", short_code=short_code, error=str(e))
async def invalidate(self, short_code: str, domain: str) -> None:
if self._redis is None:
return
try:
await self._redis.delete(self._key(short_code, domain))
log.info(
"cache_invalidated", short_code=short_code, reason="manual_invalidation"
)
except Exception as e:
log.error("url_cache_invalidate_error", short_code=short_code, error=str(e))
async def invalidate_many(self, short_codes: list[str], domain: str) -> None:
"""Bulk-invalidate cache entries for a list of aliases on one domain.
Chunked: one unbounded DELETE with six figures of keys would block
the redirect hot path's Redis."""
if not short_codes or self._redis is None:
return
keys = [self._key(c, domain) for c in short_codes]
chunk_size = 10_000
try:
for i in range(0, len(keys), chunk_size):
await self._redis.delete(*keys[i : i + chunk_size])
log.info(
"cache_invalidated_bulk",
count=len(short_codes),
domain=domain,
reason="bulk_invalidation",
)
except Exception as e:
log.error("url_cache_invalidate_many_error", domain=domain, error=str(e))
|