diff --git a/api/oembed.php b/api/oembed.php index a1a5731..2f7f067 100644 --- a/api/oembed.php +++ b/api/oembed.php @@ -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; } } diff --git a/package-lock.json b/package-lock.json index 525c19d..3a544da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 989a2b8..426e388 100644 --- a/package.json +++ b/package.json @@ -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": {