From f7cf8fe3d0e22b9416208cb03dcf4d0a6e6c25ae Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 26 Sep 2020 09:42:12 +0000 Subject: [PATCH 1/2] Issue 9276: Cache the trending hashtags in the background --- src/Model/Tag.php | 76 ++++++++++++++++++++++++++++++++------------- src/Worker/Cron.php | 5 +++ 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 40f2f8d6a1..f5932dd602 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -488,54 +488,86 @@ class Tag * Returns 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 getGlobalTrendingHashtags(int $period, $limit = 10) { $tags = DI::cache()->get('global_trending_tags'); + if (!empty($tags)) { + return $tags; + } else { + return self::setGlobalTrendingHashtags($period, $limit); + } + } - if (empty($tags)) { - $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` - FROM `tag-search-view` - WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) - GROUP BY `term` ORDER BY `score` DESC LIMIT ?", - Item::PUBLIC, $period, $limit); + /** + * 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` + FROM `tag-search-view` + WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) + GROUP BY `term` ORDER BY `score` DESC LIMIT ?", + Item::PUBLIC, $period, $limit); - if (DBA::isResult($tagsStmt)) { - $tags = DBA::toArray($tagsStmt); - DI::cache()->set('global_trending_tags', $tags, Duration::HOUR); - } + if (DBA::isResult($tagsStmt)) { + $tags = DBA::toArray($tagsStmt); + 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 * * @param int $period Period in hours to consider posts + * @param int $limit Number of returned tags * @return array * @throws \Exception */ public static function getLocalTrendingHashtags(int $period, $limit = 10) { $tags = DI::cache()->get('local_trending_tags'); + if (!empty($tags)) { + return $tags; + } else { + return self::setLocalTrendingHashtags($period, $limit); + } + } - if (empty($tags)) { - $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` - FROM `tag-search-view` - WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) - GROUP BY `term` ORDER BY `score` DESC LIMIT ?", - Item::PUBLIC, $period, $limit); + /** + * 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` + FROM `tag-search-view` + WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) + GROUP BY `term` ORDER BY `score` DESC LIMIT ?", + Item::PUBLIC, $period, $limit); - if (DBA::isResult($tagsStmt)) { - $tags = DBA::toArray($tagsStmt); - DI::cache()->set('local_trending_tags', $tags, Duration::HOUR); - } + if (DBA::isResult($tagsStmt)) { + $tags = DBA::toArray($tagsStmt); + DI::cache()->set('local_trending_tags', $tags, Duration::HOUR); + return $tags; } - return $tags ?: []; + return []; } /** diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 7a23a2c705..43a18ceafc 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -25,6 +25,7 @@ use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Worker; use Friendica\DI; +use Friendica\Model\Tag; class Cron { @@ -74,6 +75,10 @@ class Cron // Repair entries in the database Worker::add(PRIORITY_LOW, 'RepairDatabase'); + // Update trending tags cache for the community page + Tag::setLocalTrendingHashtags(24, 20); + Tag::setGlobalTrendingHashtags(24, 20); + // Hourly cron calls if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) { From 3b90dc028974767400376f327b2522d0b2b7a8d3 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 26 Sep 2020 12:16:46 +0000 Subject: [PATCH 2/2] Use period and limit in the cache key --- src/Model/Tag.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Tag.php b/src/Model/Tag.php index f5932dd602..0d6c9cfb51 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -494,7 +494,7 @@ class Tag */ public static function getGlobalTrendingHashtags(int $period, $limit = 10) { - $tags = DI::cache()->get('global_trending_tags'); + $tags = DI::cache()->get('global_trending_tags-' . $period . '-' . $limit); if (!empty($tags)) { return $tags; } else { @@ -520,7 +520,7 @@ class Tag if (DBA::isResult($tagsStmt)) { $tags = DBA::toArray($tagsStmt); - DI::cache()->set('global_trending_tags', $tags, Duration::HOUR); + DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::HOUR); return $tags; }