From d8e47026328c0064273e9ad1943b49516d5b6987 Mon Sep 17 00:00:00 2001 From: nineven Date: Sat, 4 Apr 2020 16:32:44 +0800 Subject: [PATCH] add download_url cache --- fir_ser/api/utils/storage/storage.py | 13 +++++++++++-- fir_ser/api/views/login.py | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index 863fed7..3037fbf 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -8,8 +8,9 @@ from api.models import AppStorage, UserInfo from .aliyunApi import AliYunOss from .qiniuApi import QiNiuOss from .localApi import LocalStorage -import json +import json,time from fir_ser import settings +from django.core.cache import cache class Storage(object): @@ -22,7 +23,15 @@ class Storage(object): def get_download_url(self, filename, expires=900,ftype=None): if self.storage: - return self.storage.get_download_url(filename, expires,ftype) + now = time.time() + download_val = cache.get("%s_%s"%('download_url',filename)) + if download_val: + if download_val.get("time") > now - 60: + return download_val.get("download_url") + + download_url=self.storage.get_download_url(filename, expires,ftype) + cache.set("%s_%s"%('download_url',filename),{"download_url":download_url,"time":now+expires},expires) + return download_url def delete_file(self, filename, apptype=None): if self.storage: diff --git a/fir_ser/api/views/login.py b/fir_ser/api/views/login.py index 9d1cdfb..7339e66 100644 --- a/fir_ser/api/views/login.py +++ b/fir_ser/api/views/login.py @@ -23,10 +23,10 @@ class LoginView(APIView): receive = request.data if request.method == 'POST': - print(receive) + # print(receive) # is_valid = verify(receive) is_valid = True - print("is_valid", is_valid) + # print("is_valid", is_valid) if is_valid: username = receive.get("username") password = receive.get("password")