修复双重缓存过期不一致

super_signature
nineven 5 years ago
parent 870b6a2424
commit eaa98f49d6
  1. 7
      fir_ser/api/utils/TokenManager.py
  2. 8
      fir_ser/api/utils/serializer.py
  3. 13
      fir_ser/api/utils/storage/caches.py
  4. 11
      fir_ser/api/utils/storage/storage.py
  5. 2
      fir_ser/api/views/download.py
  6. 9
      fir_ser/fir_ser/settings.py

@ -19,9 +19,8 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE
class DownloadToken(object): class DownloadToken(object):
def make_token(self,release_id,time_limit=60): def make_token(self,release_id,time_limit=60,key=''):
token_key = "_".join([key.lower(), CACHE_KEY_TEMPLATE.get("make_token_key"),release_id])
token_key = "%s%s"%(CACHE_KEY_TEMPLATE.get("make_token_key"),release_id)
make_token_key = cache.get(token_key) make_token_key = cache.get(token_key)
if make_token_key: if make_token_key:
return make_token_key return make_token_key
@ -40,7 +39,7 @@ class DownloadToken(object):
def verify_token(self,token,release_id): def verify_token(self,token,release_id):
try: try:
values = cache.get(token) values = cache.get(token)
if release_id in values.get("data",None): if values and release_id in values.get("data",None):
return True return True
except Exception as e: except Exception as e:
print(e) print(e)

@ -43,10 +43,12 @@ class AppsSerializer(serializers.ModelSerializer):
if master_release_obj: if master_release_obj:
icon_url = "" icon_url = ""
key=''
if self.context.get("key", None) and self.context.get("key") != "undefined":
key=self.context.get("key", '')
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),600) icon_url = storage.get_download_url(os.path.basename(master_release_obj.icon_url),600,key=key)
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 +62,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,600,key=key)
datainfo["download_token"] = download_token datainfo["download_token"] = download_token
return datainfo return datainfo

@ -11,16 +11,16 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE
from api.utils.storage.storage import Storage,LocalStorage from api.utils.storage.storage import Storage,LocalStorage
from api.utils.crontab import run from api.utils.crontab import run
def get_download_url_by_cache(app_obj, filename, limit, isdownload=False): def get_download_url_by_cache(app_obj, filename, limit, isdownload=True):
now = time.time() now = time.time()
if isdownload is None:
local_storage = LocalStorage('localhost', False)
return local_storage.get_download_url(filename, limit, 'plist')
download_val = cache.get("%s_%s" % ('download_url', filename)) download_val = cache.get("%s_%s" % ('download_url', filename))
if download_val: if download_val:
if download_val.get("time") > now - 60: if download_val.get("time") > now - 60:
return download_val.get("download_url") return download_val.get("download_url")
else: else:
if isdownload:
local_storage = LocalStorage('localhost', False)
return local_storage.get_download_url(filename, limit, 'plist')
user_obj = UserInfo.objects.filter(pk=app_obj.get("user_id")).first() user_obj = UserInfo.objects.filter(pk=app_obj.get("user_id")).first()
storage = Storage(user_obj) storage = Storage(user_obj)
return storage.get_download_url(filename, limit) return storage.get_download_url(filename, limit)
@ -45,7 +45,6 @@ def get_app_download_by_cache(app_id):
cache.incr(key) cache.incr(key)
return download_times + 1 return download_times + 1
def del_cache_response_by_short(short,app_id): def del_cache_response_by_short(short,app_id):
cache.delete("%s%s"%(CACHE_KEY_TEMPLATE.get("download_short_key"),short)) cache.delete("_".join([CACHE_KEY_TEMPLATE.get("download_short_key"),short]))
cache.delete("%s%s" % (CACHE_KEY_TEMPLATE.get("app_instance"), app_id)) cache.delete("_".join([CACHE_KEY_TEMPLATE.get("app_instance"),app_id]))

@ -9,7 +9,7 @@ from .aliyunApi import AliYunOss
from .qiniuApi import QiNiuOss from .qiniuApi import QiNiuOss
from .localApi import LocalStorage from .localApi import LocalStorage
import json,time import json,time
from fir_ser import settings from fir_ser.settings import THIRD_PART_CONFIG,CACHE_KEY_TEMPLATE
from django.core.cache import cache from django.core.cache import cache
@ -21,16 +21,17 @@ class Storage(object):
if self.storage: if self.storage:
return self.storage.get_upload_token(filename, expires) return self.storage.get_upload_token(filename, expires)
def get_download_url(self, filename, expires=900,ftype=None): def get_download_url(self,filename, expires=900,ftype=None,key=''):
if self.storage: if self.storage:
now = time.time() now = time.time()
download_val = cache.get("%s_%s"%('download_url',filename)) down_key = "_".join([key.lower(),CACHE_KEY_TEMPLATE.get('download_url_key'),filename])
download_val = cache.get(down_key)
if download_val: if download_val:
if download_val.get("time") > now - 60: if download_val.get("time") > now - 60:
return download_val.get("download_url") return download_val.get("download_url")
download_url=self.storage.get_download_url(filename, expires,ftype) 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) cache.set(down_key,{"download_url":download_url,"time":now+expires},expires)
return download_url return download_url
def delete_file(self, filename, apptype=None): def delete_file(self, filename, apptype=None):
@ -63,7 +64,7 @@ class Storage(object):
if admin_storage: if admin_storage:
return self.get_storage(UserInfo) return self.get_storage(UserInfo)
else: else:
storage_lists = settings.THIRD_PART_CONFIG.get('storage') storage_lists = THIRD_PART_CONFIG.get('storage')
for storage in storage_lists: for storage in storage_lists:
if storage.get("active",None): if storage.get("active",None):
storage_type= storage.get('type',None) storage_type= storage.get('type',None)

@ -81,7 +81,7 @@ class ShortDownloadView(APIView):
res.msg="该应用不存在" res.msg="该应用不存在"
return Response(res.dict) return Response(res.dict)
app_serializer = AppsSerializer(app_obj,context={"release_id":release_id,"storage":Storage(app_obj.user_id)}) app_serializer = AppsSerializer(app_obj,context={"key":"ShortDownloadView","release_id":release_id,"storage":Storage(app_obj.user_id)})
res.data = app_serializer.data res.data = app_serializer.data
return Response(res.dict) return Response(res.dict)

@ -234,10 +234,11 @@ THIRD_PART_CONFIG = {
} }
CACHE_KEY_TEMPLATE={ CACHE_KEY_TEMPLATE={
'download_times_key':'app_download_times_', 'download_times_key':'app_download_times',
'make_token_key':'make_token_', 'make_token_key':'make_token',
'download_short_key':'download_short_', 'download_short_key':'download_short',
'app_instance_key':'app_instance_' 'app_instance_key':'app_instance',
'download_url_key':'download_url'
} }
SYNC_CACHE_TO_DATABASE={ SYNC_CACHE_TO_DATABASE={

Loading…
Cancel
Save