chore(oembed.php): add redis caching for oembed responses
This commit is contained in:
parent
f7f9596dd1
commit
00123f1da8
3 changed files with 26 additions and 5 deletions
|
@ -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
4
package-lock.json
generated
|
@ -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",
|
||||
|
|
|
@ -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": {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue