Browse Source

优化使用帮助增加缓存

zhangdongming 1 tháng trước cách đây
mục cha
commit
541967080b
2 tập tin đã thay đổi với 18 bổ sung1 xóa
  1. 1 1
      Ansjer/urls.py
  2. 17 0
      Controller/FAQController.py

+ 1 - 1
Ansjer/urls.py

@@ -211,7 +211,7 @@ urlpatterns = [
     re_path(r'^alexa/(?P<operation>.*)$', Alexa.AlexaConnectNum.as_view()),
     re_path(r'^faq/upload', FAQController.FAQUploadView.as_view()),
     re_path(r'^faq/image/(?P<filePath>.*)$', FAQController.getFAQImage.as_view()),
-    re_path(r'^faq/helpLink/(?P<filePath>.*)$', FAQController.HelpLinkView.as_view()),
+    re_path(r'^faq/helpLink/(?P<operation>.*)$', FAQController.HelpLinkView.as_view()),
     re_path(r'^faq/(?P<operation>.*)$', FAQController.FAQView.as_view()),
     re_path(r'^ios/authsign', UserController.AppleAuthLogin.as_view()),
     re_path(r'^appLog/(?P<operation>.*)$', AppLogController.AppLogView.as_view()),

+ 17 - 0
Controller/FAQController.py

@@ -15,6 +15,7 @@ from django.views.generic.base import View
 import Ansjer
 from Ansjer.config import BASE_DIR, SERVER_TYPE, SERVER_DOMAIN, SERVER_DOMAIN_SSL
 from Model.models import FAQModel, HelpLink
+from Object.Enums.RedisKeyConstant import RedisKeyConstant
 from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
@@ -423,6 +424,15 @@ class HelpLinkView(View):
             return response.json(444, {'message': 'device_type和lang参数不能为空'})
             
         try:
+            redis = RedisObject()
+            cache_key = RedisKeyConstant.HELP_LINK_TYPE.value + f'{device_type}:{lang}'
+            
+            # 先尝试从缓存获取
+            cached_data = redis.get_data(cache_key)
+            if cached_data:
+                cached_data = json.loads(cached_data)
+                return response.json(0, cached_data)
+                
             # 查询匹配的帮助链接
             help_link = HelpLink.objects.filter(
                 device_type=device_type,
@@ -440,6 +450,13 @@ class HelpLinkView(View):
                 'description': help_link.description
             }
             
+            # 设置缓存,过期时间30天
+            try:
+                redis.set_data(cache_key, json.dumps(data), RedisKeyConstant.EXPIRE_TIME_30_DAYS.value)
+            except Exception as e:
+                print(f"设置Redis缓存出错: {str(e)}")
+                # 缓存失败不影响主流程
+                
             return response.json(0, data)
             
         except Exception as e: