Commit 53acfc54 authored by Marcelo Rivera's avatar Marcelo Rivera

(feat): implement caching layer for homepage content

1 merge request!308WIP: (feat): Minds Pro
Pipeline #82479130 failed with stages
in 12 minutes and 29 seconds
......@@ -116,6 +116,11 @@ class channel implements Interfaces\Api
$custom_type = isset($_GET['custom_type']) && $_GET['custom_type'] ? [$_GET['custom_type']] : null;
$cache = (bool) $_GET['cache'] ?? false;
/** @var Core\Data\cache\abstractCacher $cacher */
$cacher = Di::_()->get('Cache');
/** @var Core\Feeds\Top\Entities $entities */
$entities = new Core\Feeds\Top\Entities();
$entities->setActor($currentUser);
......@@ -164,6 +169,15 @@ class channel implements Interfaces\Api
$opts['filter_hashtags'] = true;
}
$cacheKey = $this->getCacheKey($opts);
if ($cache) {
$cached = $cacher->get($cacheKey);
if ($cached && count($cached) > 0) {
return Factory::response($cached);
}
}
try {
$result = $this->getData($entities, $opts, $asActivities, $sync);
......@@ -172,17 +186,28 @@ class channel implements Interfaces\Api
$result = $this->getData($entities, $opts, $asActivities, $sync);
}
return Factory::response([
$response = [
'status' => 'success',
'entities' => Exportable::_($result),
'load-next' => $result->getPagingToken(),
]);
];
if ($cache) {
$cacher->set($cacheKey, $response, 300); // cache for 5 mins
}
return Factory::response($response);
} catch (\Exception $e) {
error_log($e);
return Factory::response(['status' => 'error', 'message' => $e->getMessage()]);
}
}
private function getCacheKey(array $opts): string
{
return "feeds-channel:" . implode(':', opts);
}
/**
* @param Core\Feeds\Top\Entities $entities
* @param array $opts
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment