add short cache

super_signature
nineven 5 years ago
parent d8e4702632
commit 1513cb4ca9
  1. 3
      fir_ser/api/utils/app/apputils.py
  2. 8
      fir_ser/api/utils/serializer.py
  3. 11
      fir_ser/api/utils/storage/storage.py
  4. 13
      fir_ser/api/views/apps.py
  5. 13
      fir_ser/api/views/download.py
  6. 3
      fir_ser/api/views/uploads.py
  7. 9
      fir_ser/fir_ser/settings.py

@ -6,7 +6,7 @@
from api.utils.app.randomstrings import make_app_uuid from api.utils.app.randomstrings import make_app_uuid
from api.models import AppReleaseInfo,Apps from api.models import AppReleaseInfo,Apps
import random,xmltodict,json 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): 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 return
else: else:
try: try:
del_cache_response_by_short(appmobj.short)
appmobj.short = short appmobj.short = short
appmobj.name = appinfo["labelname"] appmobj.name = appinfo["labelname"]
appmobj.save() appmobj.save()

@ -46,7 +46,7 @@ class AppsSerializer(serializers.ModelSerializer):
if self.context.get("storage", None) and self.context.get("storage") != "undefined": if self.context.get("storage", None) and self.context.get("storage") != "undefined":
storage = self.context.get("storage", None) 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 = { datainfo = {
"app_version": master_release_obj.app_version, "app_version": master_release_obj.app_version,
"icon_url": icon_url, "icon_url": icon_url,
@ -60,7 +60,7 @@ class AppsSerializer(serializers.ModelSerializer):
"binary_url":master_release_obj.binary_url, "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 datainfo["download_token"] = download_token
return datainfo return datainfo
@ -90,12 +90,12 @@ class AppReleaseSerializer(serializers.ModelSerializer):
def get_download_token(self,obj): 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 return download_token
def get_icon_url(self, obj): 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": if self.context.get("storage", None) and self.context.get("storage") != "undefined":
storage = self.context.get("storage", None) storage = self.context.get("storage", None)
icon_url = storage.get_download_url(os.path.basename(obj.icon_url)) icon_url = storage.get_download_url(os.path.basename(obj.icon_url))

@ -102,3 +102,14 @@ class Storage(object):
print(e) print(e)
additionalparameters = {} additionalparameters = {}
return {**auth_dict, **additionalparameters} return {**auth_dict, **additionalparameters}
def del_cache_response_by_short(short):
id = "download"
rtn = '_'.join([
id,
"short",
short
])
cache.delete(rtn)

@ -12,7 +12,7 @@ from django.db.models import Sum
import os import os
from fir_ser import settings from fir_ser import settings
from api.utils.app.randomstrings import make_from_user_uuid 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.models import Apps, AppReleaseInfo
from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer
@ -116,8 +116,11 @@ class AppInfoView(APIView):
has_combo = apps_obj.has_combo has_combo = apps_obj.has_combo
if has_combo: if has_combo:
del_cache_response_by_short(apps_obj.has_combo.short)
apps_obj.has_combo.has_combo = None apps_obj.has_combo.has_combo = None
apps_obj.delete() apps_obj.delete()
del_cache_response_by_short(apps_obj.short)
return Response(res.dict) return Response(res.dict)
@ -145,6 +148,9 @@ class AppInfoView(APIView):
has_combo.update(**{"has_combo": apps_obj.first()}) has_combo.update(**{"has_combo": apps_obj.first()})
else: else:
pass pass
del_cache_response_by_short(apps_obj.short)
del_cache_response_by_short(has_combo.short)
except Exception as e: except Exception as e:
res.code = 1004 res.code = 1004
res.msg = "该应用已经关联" res.msg = "该应用已经关联"
@ -152,6 +158,7 @@ class AppInfoView(APIView):
try: try:
apps_obj = Apps.objects.filter(user_id=request.user, app_id=app_id).first() apps_obj = Apps.objects.filter(user_id=request.user, app_id=app_id).first()
apps_obj.description = data.get("description", apps_obj.description) 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.short = data.get("short", apps_obj.short)
apps_obj.name = data.get("name", apps_obj.name) apps_obj.name = data.get("name", apps_obj.name)
apps_obj.save() apps_obj.save()
@ -204,6 +211,7 @@ class AppInfoView(APIView):
release_obj.icon_url = release_obj.icon_url.replace(old_file_name.split(".")[0], release_obj.icon_url = release_obj.icon_url.replace(old_file_name.split(".")[0],
random_file_name) random_file_name)
release_obj.save() release_obj.save()
del_cache_response_by_short(apps_obj.short)
storage = Storage(request.user) storage = Storage(request.user)
storage.delete_file(old_file_name) storage.delete_file(old_file_name)
@ -262,6 +270,7 @@ class AppReleaseinfoView(APIView):
apps_obj.delete() apps_obj.delete()
else: else:
pass pass
del_cache_response_by_short(apps_obj.short)
return Response(res.dict) return Response(res.dict)
@ -291,11 +300,13 @@ class AppReleaseinfoView(APIView):
print(binary_url) print(binary_url)
appreleaseobj.update(**{"binary_url": binary_url}) appreleaseobj.update(**{"binary_url": binary_url})
except Exception as e: except Exception as e:
res.code = 1006 res.code = 1006
res.msg = "更新失败" res.msg = "更新失败"
return Response(res.dict) return Response(res.dict)
del_cache_response_by_short(apps_obj.short)
app_serializer = AppsSerializer(apps_obj) app_serializer = AppsSerializer(apps_obj)
res.data["currentapp"] = app_serializer.data res.data["currentapp"] = app_serializer.data

@ -12,6 +12,7 @@ from api.utils.app.randomstrings import make_random_uuid
from api.utils.app.apputils import make_resigned from api.utils.app.apputils import make_resigned
from api.utils.storage.storage import Storage,LocalStorage from api.utils.storage.storage import Storage,LocalStorage
import os import os
from rest_framework_extensions.cache.decorators import cache_response
from api.utils.serializer import AppsSerializer from api.utils.serializer import AppsSerializer
from api.models import Apps,AppReleaseInfo 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): def get(self,request,short):
res = BaseResponse() res = BaseResponse()
release_id = request.query_params.get("release_id", None) release_id = request.query_params.get("release_id", None)
@ -82,6 +84,17 @@ class ShortDownloadView(APIView):
res.data = app_serializer.data res.data = app_serializer.data
return Response(res.dict) 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): class InstallView(APIView):
''' '''

@ -5,7 +5,7 @@
# date: 2020/3/6 # date: 2020/3/6
from api.utils.app.apputils import get_random_short,SaveAppInfos 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.models import Apps,AppReleaseInfo,UserInfo
from api.utils.app.randomstrings import make_app_uuid from api.utils.app.randomstrings import make_app_uuid
from rest_framework.views import APIView from rest_framework.views import APIView
@ -231,6 +231,7 @@ class UploadView(APIView):
old_file_key = release_obj.icon_url old_file_key = release_obj.icon_url
release_obj.icon_url = certinfo.get("upload_key") release_obj.icon_url = certinfo.get("upload_key")
release_obj.save() release_obj.save()
del_cache_response_by_short(app_obj.short)
storage.delete_file(old_file_key) storage.delete_file(old_file_key)
return Response(res.dict) return Response(res.dict)
elif ftype and ftype == 'head': elif ftype and ftype == 'head':

@ -162,6 +162,15 @@ CACHES = {
}, },
} }
# DRF扩展缓存时间
REST_FRAMEWORK_EXTENSIONS = {
# 缓存时间
'DEFAULT_CACHE_RESPONSE_TIMEOUT': 3600,
# 缓存存储
'DEFAULT_USE_CACHE': 'default',
}
THIRD_PART_CONFIG = { THIRD_PART_CONFIG = {
# 阿里云服务配置 # 阿里云服务配置
"ALI_YUN": { "ALI_YUN": {

Loading…
Cancel
Save