Issue 9276: Cache the trending hashtags in the background

This commit is contained in:
Michael 2020-09-26 09:42:12 +00:00
parent 04f993b611
commit f7cf8fe3d0
2 changed files with 59 additions and 22 deletions

View file

@ -488,14 +488,30 @@ class Tag
* Returns a list of the most frequent global hashtags over the given period * Returns a list of the most frequent global hashtags over the given period
* *
* @param int $period Period in hours to consider posts * @param int $period Period in hours to consider posts
* @param int $limit Number of returned tags
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getGlobalTrendingHashtags(int $period, $limit = 10) public static function getGlobalTrendingHashtags(int $period, $limit = 10)
{ {
$tags = DI::cache()->get('global_trending_tags'); $tags = DI::cache()->get('global_trending_tags');
if (!empty($tags)) {
return $tags;
} else {
return self::setGlobalTrendingHashtags($period, $limit);
}
}
if (empty($tags)) { /**
* Creates a list of the most frequent global hashtags over the given period
*
* @param int $period Period in hours to consider posts
* @param int $limit Number of returned tags
* @return array
* @throws \Exception
*/
public static function setGlobalTrendingHashtags(int $period, $limit = 10)
{
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
FROM `tag-search-view` FROM `tag-search-view`
WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
@ -505,24 +521,40 @@ class Tag
if (DBA::isResult($tagsStmt)) { if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt); $tags = DBA::toArray($tagsStmt);
DI::cache()->set('global_trending_tags', $tags, Duration::HOUR); DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
} return $tags;
} }
return $tags ?: []; return [];
} }
/** /**
* Returns a list of the most frequent local hashtags over the given period * Returns a list of the most frequent local hashtags over the given period
* *
* @param int $period Period in hours to consider posts * @param int $period Period in hours to consider posts
* @param int $limit Number of returned tags
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getLocalTrendingHashtags(int $period, $limit = 10) public static function getLocalTrendingHashtags(int $period, $limit = 10)
{ {
$tags = DI::cache()->get('local_trending_tags'); $tags = DI::cache()->get('local_trending_tags');
if (!empty($tags)) {
return $tags;
} else {
return self::setLocalTrendingHashtags($period, $limit);
}
}
if (empty($tags)) { /**
* Returns a list of the most frequent local hashtags over the given period
*
* @param int $period Period in hours to consider posts
* @param int $limit Number of returned tags
* @return array
* @throws \Exception
*/
public static function setLocalTrendingHashtags(int $period, $limit = 10)
{
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
FROM `tag-search-view` FROM `tag-search-view`
WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
@ -532,10 +564,10 @@ class Tag
if (DBA::isResult($tagsStmt)) { if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt); $tags = DBA::toArray($tagsStmt);
DI::cache()->set('local_trending_tags', $tags, Duration::HOUR); DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
} return $tags;
} }
return $tags ?: []; return [];
} }
/** /**

View file

@ -25,6 +25,7 @@ use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Tag;
class Cron class Cron
{ {
@ -74,6 +75,10 @@ class Cron
// Repair entries in the database // Repair entries in the database
Worker::add(PRIORITY_LOW, 'RepairDatabase'); Worker::add(PRIORITY_LOW, 'RepairDatabase');
// Update trending tags cache for the community page
Tag::setLocalTrendingHashtags(24, 20);
Tag::setGlobalTrendingHashtags(24, 20);
// Hourly cron calls // Hourly cron calls
if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) { if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {