From 1513cb4ca992fc755ef5e369080094c686946405 Mon Sep 17 00:00:00 2001 From: nineven Date: Sun, 5 Apr 2020 18:52:24 +0800 Subject: [PATCH] add short cache --- fir_ser/api/utils/app/apputils.py | 3 ++- fir_ser/api/utils/serializer.py | 8 ++++---- fir_ser/api/utils/storage/storage.py | 11 +++++++++++ fir_ser/api/views/apps.py | 13 ++++++++++++- fir_ser/api/views/download.py | 13 +++++++++++++ fir_ser/api/views/uploads.py | 3 ++- fir_ser/fir_ser/settings.py | 9 +++++++++ 7 files changed, 53 insertions(+), 7 deletions(-) diff --git a/fir_ser/api/utils/app/apputils.py b/fir_ser/api/utils/app/apputils.py index 487b71e..9896bae 100644 --- a/fir_ser/api/utils/app/apputils.py +++ b/fir_ser/api/utils/app/apputils.py @@ -6,7 +6,7 @@ from api.utils.app.randomstrings import make_app_uuid from api.models import AppReleaseInfo,Apps import random,xmltodict,json -from api.utils.storage.storage import Storage +from api.utils.storage.storage import Storage,del_cache_response_by_short def make_resigned(bin_url,img_url,bundle_id,app_version,name): @@ -129,6 +129,7 @@ def SaveAppInfos(app_file_name,user_obj,appinfo,bundle_id,app_img,short,size): return else: try: + del_cache_response_by_short(appmobj.short) appmobj.short = short appmobj.name = appinfo["labelname"] appmobj.save() diff --git a/fir_ser/api/utils/serializer.py b/fir_ser/api/utils/serializer.py index 1489467..e6b7a91 100644 --- a/fir_ser/api/utils/serializer.py +++ b/fir_ser/api/utils/serializer.py @@ -46,7 +46,7 @@ class AppsSerializer(serializers.ModelSerializer): if self.context.get("storage", None) and self.context.get("storage") != "undefined": storage = self.context.get("storage", None) - icon_url = storage.get_download_url(os.path.basename(master_release_obj.icon_url)) + icon_url = storage.get_download_url(os.path.basename(master_release_obj.icon_url),300) datainfo = { "app_version": master_release_obj.app_version, "icon_url": icon_url, @@ -60,7 +60,7 @@ class AppsSerializer(serializers.ModelSerializer): "binary_url":master_release_obj.binary_url, } - download_token = token_obj.make_token(master_release_obj.release_id,600) + download_token = token_obj.make_token(master_release_obj.release_id,300) datainfo["download_token"] = download_token return datainfo @@ -90,12 +90,12 @@ class AppReleaseSerializer(serializers.ModelSerializer): def get_download_token(self,obj): - download_token = token_obj.make_token(obj.release_id, 600) + download_token = token_obj.make_token(obj.release_id, 300) return download_token def get_icon_url(self, obj): - icon_url="/".join([obj.app_id.user_id.domain_name, obj.icon_url]) + icon_url="" if self.context.get("storage", None) and self.context.get("storage") != "undefined": storage = self.context.get("storage", None) icon_url = storage.get_download_url(os.path.basename(obj.icon_url)) diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index 3037fbf..f569a81 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -102,3 +102,14 @@ class Storage(object): print(e) additionalparameters = {} return {**auth_dict, **additionalparameters} + + + +def del_cache_response_by_short(short): + id = "download" + rtn = '_'.join([ + id, + "short", + short + ]) + cache.delete(rtn) \ No newline at end of file diff --git a/fir_ser/api/views/apps.py b/fir_ser/api/views/apps.py index 78f88c5..0ba20ce 100644 --- a/fir_ser/api/views/apps.py +++ b/fir_ser/api/views/apps.py @@ -12,7 +12,7 @@ from django.db.models import Sum import os from fir_ser import settings from api.utils.app.randomstrings import make_from_user_uuid -from api.utils.storage.storage import Storage +from api.utils.storage.storage import Storage,del_cache_response_by_short from api.models import Apps, AppReleaseInfo from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer @@ -116,8 +116,11 @@ class AppInfoView(APIView): has_combo = apps_obj.has_combo if has_combo: + del_cache_response_by_short(apps_obj.has_combo.short) apps_obj.has_combo.has_combo = None + apps_obj.delete() + del_cache_response_by_short(apps_obj.short) return Response(res.dict) @@ -145,6 +148,9 @@ class AppInfoView(APIView): has_combo.update(**{"has_combo": apps_obj.first()}) else: pass + del_cache_response_by_short(apps_obj.short) + del_cache_response_by_short(has_combo.short) + except Exception as e: res.code = 1004 res.msg = "该应用已经关联" @@ -152,6 +158,7 @@ class AppInfoView(APIView): try: apps_obj = Apps.objects.filter(user_id=request.user, app_id=app_id).first() apps_obj.description = data.get("description", apps_obj.description) + del_cache_response_by_short(apps_obj.short) apps_obj.short = data.get("short", apps_obj.short) apps_obj.name = data.get("name", apps_obj.name) apps_obj.save() @@ -204,6 +211,7 @@ class AppInfoView(APIView): release_obj.icon_url = release_obj.icon_url.replace(old_file_name.split(".")[0], random_file_name) release_obj.save() + del_cache_response_by_short(apps_obj.short) storage = Storage(request.user) storage.delete_file(old_file_name) @@ -262,6 +270,7 @@ class AppReleaseinfoView(APIView): apps_obj.delete() else: pass + del_cache_response_by_short(apps_obj.short) return Response(res.dict) @@ -291,11 +300,13 @@ class AppReleaseinfoView(APIView): print(binary_url) appreleaseobj.update(**{"binary_url": binary_url}) + except Exception as e: res.code = 1006 res.msg = "更新失败" return Response(res.dict) + del_cache_response_by_short(apps_obj.short) app_serializer = AppsSerializer(apps_obj) res.data["currentapp"] = app_serializer.data diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index aaba66e..f1e2b4e 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -12,6 +12,7 @@ from api.utils.app.randomstrings import make_random_uuid from api.utils.app.apputils import make_resigned from api.utils.storage.storage import Storage,LocalStorage import os +from rest_framework_extensions.cache.decorators import cache_response from api.utils.serializer import AppsSerializer from api.models import Apps,AppReleaseInfo @@ -69,6 +70,7 @@ class ShortDownloadView(APIView): 根据下载短链接,获取应用信息 ''' + @cache_response(timeout=300-60, cache="default",key_func='calculate_cache_key',cache_errors=False) def get(self,request,short): res = BaseResponse() release_id = request.query_params.get("release_id", None) @@ -82,6 +84,17 @@ class ShortDownloadView(APIView): res.data = app_serializer.data return Response(res.dict) + #key的设置 + def calculate_cache_key(self, view_instance, view_method, + request, args, kwargs): + id = "download" + rtn = '_'.join([ + id, + "short", + kwargs.get("short",None) + ]) + # print( request.META) + return rtn class InstallView(APIView): ''' diff --git a/fir_ser/api/views/uploads.py b/fir_ser/api/views/uploads.py index dd08bf2..1404c13 100644 --- a/fir_ser/api/views/uploads.py +++ b/fir_ser/api/views/uploads.py @@ -5,7 +5,7 @@ # date: 2020/3/6 from api.utils.app.apputils import get_random_short,SaveAppInfos -from api.utils.storage.storage import Storage +from api.utils.storage.storage import Storage,del_cache_response_by_short from api.models import Apps,AppReleaseInfo,UserInfo from api.utils.app.randomstrings import make_app_uuid from rest_framework.views import APIView @@ -231,6 +231,7 @@ class UploadView(APIView): old_file_key = release_obj.icon_url release_obj.icon_url = certinfo.get("upload_key") release_obj.save() + del_cache_response_by_short(app_obj.short) storage.delete_file(old_file_key) return Response(res.dict) elif ftype and ftype == 'head': diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index 5079c2e..20bc825 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -162,6 +162,15 @@ CACHES = { }, } + +# DRF扩展缓存时间 +REST_FRAMEWORK_EXTENSIONS = { + # 缓存时间 + 'DEFAULT_CACHE_RESPONSE_TIMEOUT': 3600, + # 缓存存储 + 'DEFAULT_USE_CACHE': 'default', +} + THIRD_PART_CONFIG = { # 阿里云服务配置 "ALI_YUN": {