Skip to content

Commit 7bfad56

Browse files
feat(seo): add caching for SEO responses
- Implement caching for SEO responses to improve performance - Retrieve cached data before querying the database - Store generated HTML responses in the cache
1 parent d35c2a8 commit 7bfad56

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

api/routers/seo.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ async def seo_spec_overview(spec_id: str, db: AsyncSession | None = Depends(opti
181181
)
182182
)
183183

184+
key = cache_key("seo", spec_id)
185+
cached = get_cache(key)
186+
if cached:
187+
return HTMLResponse(cached)
188+
184189
repo = SpecRepository(db)
185190
spec = await repo.get_by_id(spec_id)
186191
if not spec:
@@ -190,14 +195,14 @@ async def seo_spec_overview(spec_id: str, db: AsyncSession | None = Depends(opti
190195
has_previews = any(i.preview_url for i in spec.impls)
191196
image = f"https://api.pyplots.ai/og/{spec_id}.png" if has_previews else DEFAULT_HOME_IMAGE
192197

193-
return HTMLResponse(
194-
BOT_HTML_TEMPLATE.format(
195-
title=f"{html.escape(spec.title)} | pyplots.ai",
196-
description=html.escape(spec.description or DEFAULT_DESCRIPTION),
197-
image=html.escape(image, quote=True),
198-
url=f"https://pyplots.ai/{html.escape(spec_id)}",
199-
)
198+
result = BOT_HTML_TEMPLATE.format(
199+
title=f"{html.escape(spec.title)} | pyplots.ai",
200+
description=html.escape(spec.description or DEFAULT_DESCRIPTION),
201+
image=html.escape(image, quote=True),
202+
url=f"https://pyplots.ai/{html.escape(spec_id)}",
200203
)
204+
set_cache(key, result)
205+
return HTMLResponse(result)
201206

202207

203208
@router.get("/seo-proxy/{spec_id}/{library}")
@@ -214,6 +219,11 @@ async def seo_spec_implementation(spec_id: str, library: str, db: AsyncSession |
214219
)
215220
)
216221

222+
key = cache_key("seo", spec_id, library)
223+
cached = get_cache(key)
224+
if cached:
225+
return HTMLResponse(cached)
226+
217227
repo = SpecRepository(db)
218228
spec = await repo.get_by_id(spec_id)
219229
if not spec:
@@ -224,11 +234,11 @@ async def seo_spec_implementation(spec_id: str, library: str, db: AsyncSession |
224234
# Use branded og:image endpoint if implementation has preview
225235
image = f"https://api.pyplots.ai/og/{spec_id}/{library}.png" if impl and impl.preview_url else DEFAULT_HOME_IMAGE
226236

227-
return HTMLResponse(
228-
BOT_HTML_TEMPLATE.format(
229-
title=f"{html.escape(spec.title)} - {html.escape(library)} | pyplots.ai",
230-
description=html.escape(spec.description or DEFAULT_DESCRIPTION),
231-
image=html.escape(image, quote=True),
232-
url=f"https://pyplots.ai/{html.escape(spec_id)}/{html.escape(library)}",
233-
)
237+
result = BOT_HTML_TEMPLATE.format(
238+
title=f"{html.escape(spec.title)} - {html.escape(library)} | pyplots.ai",
239+
description=html.escape(spec.description or DEFAULT_DESCRIPTION),
240+
image=html.escape(image, quote=True),
241+
url=f"https://pyplots.ai/{html.escape(spec_id)}/{html.escape(library)}",
234242
)
243+
set_cache(key, result)
244+
return HTMLResponse(result)

0 commit comments

Comments
 (0)