chore(oembed.php): add redis caching for oembed responses

This commit is contained in:
Cory Dransfeldt 2025-05-13 06:02:22 -07:00
parent f7f9596dd1
commit 00123f1da8
No known key found for this signature in database
3 changed files with 26 additions and 5 deletions

View file

@ -23,11 +23,27 @@ class OembedHandler extends BaseHandler
));
if (!$relativePath) $this->sendErrorResponse('Invalid url', 400);
$relativePath = '/' . ltrim($relativePath ?? '', '/');
if ($relativePath !== '/' && str_ends_with($relativePath, '/')) $relativePath = rtrim($relativePath, '/');
$cacheKey = 'oembed:' . md5($relativePath);
if ($this->cache && $this->cache->exists($cacheKey)) {
$cachedItem = json_decode($this->cache->get($cacheKey), true);
$this->sendResponse($this->buildResponse(
$cachedItem['title'],
$cachedItem['url'],
$cachedItem['image_url'],
$globals,
$cachedItem['description'] ?? ''
));
}
$results = $this->fetchFromApi('optimized_oembed', 'url=eq.' . urlencode($relativePath));
if (!empty($results)) {
$item = $results[0];
if ($this->cache) $this->cache->setex($cacheKey, 300, json_encode($item));
$this->sendResponse($this->buildResponse(
$item['title'],
$item['url'],
@ -72,13 +88,18 @@ class OembedHandler extends BaseHandler
private function fetchGlobals(): array
{
$globals = $this->fetchFromApi('optimized_globals', 'limit=1');
return $globals[0] ?? [
$cacheKey = 'globals_data';
if ($this->cache && $this->cache->exists($cacheKey)) return json_decode($this->cache->get($cacheKey), true);
$globals = $this->fetchFromApi('optimized_globals', 'limit=1')[0] ?? [
'author' => 'Cory Dransfeldt',
'site_name' => 'coryd.dev',
'url' => 'https://www.coryd.dev',
'avatar' => ''
];
if ($this->cache) $this->cache->setex($cacheKey, 3600, json_encode($globals));
return $globals;
}
}

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd.dev",
"version": "4.1.8",
"version": "4.2.8",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd.dev",
"version": "4.1.8",
"version": "4.2.8",
"license": "MIT",
"dependencies": {
"html-minifier-terser": "7.2.0",

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "4.1.8",
"version": "4.2.8",
"description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module",
"engines": {