From 9dadcfccbaeb8bd69cb6dd477f95d476f93ba6d3 Mon Sep 17 00:00:00 2001 From: hopetree Date: Mon, 11 Dec 2023 08:37:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E"=E6=98=A8=E6=97=A5=E7=83=AD?= =?UTF-8?q?=E6=A6=9C"=E6=A8=A1=E5=9D=97=EF=BC=8C=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=88=B0redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/blog/templatetags/dashboard.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/blog/templatetags/dashboard.py b/apps/blog/templatetags/dashboard.py index 14936316e..7f8879c8f 100644 --- a/apps/blog/templatetags/dashboard.py +++ b/apps/blog/templatetags/dashboard.py @@ -35,6 +35,7 @@ def get_hot_article_list(): """ 获取昨日热门文章,元数据来自于定时任务统计的每日阅读量数据 验证:有删除,新增文章的操作,不能报错 + 只保留10篇文章就行,且都需要有阅读量的增加 @return: """ yesterday_str = (datetime.today() - timedelta(days=1)).strftime('%Y%m%d') # 昨天 @@ -60,10 +61,12 @@ def get_hot_article_list(): result = [] for each in data: article_obj_query = Article.objects.filter(id=each['key']) - if article_obj_query: + if article_obj_query and each['value']: article_obj = article_obj_query.first() article_obj.add_view = each['value'] result.append(article_obj) + if len(result) >= 10: + break cache.set(redis_key, result, 3600 * 24) # 缓存一天即可,反正到了新一天自动更换key return result return []