diff --git a/fir_ser/api/utils/TokenManager.py b/fir_ser/api/utils/TokenManager.py index 6e81bc0..fada543 100644 --- a/fir_ser/api/utils/TokenManager.py +++ b/fir_ser/api/utils/TokenManager.py @@ -9,7 +9,7 @@ import random import time from threading import Thread from django.core.cache import cache - +from fir_ser.settings import CACHE_KEY_TEMPLATE ''' user = cache.get(token) @@ -20,15 +20,22 @@ from django.core.cache import cache class DownloadToken(object): def make_token(self,release_id,time_limit=60): - random_str = uuid.uuid1().__str__().split("-")[0:-1] - user_ran_str = uuid.uuid5(uuid.NAMESPACE_DNS, release_id).__str__().split("-") - user_ran_str.extend(random_str) - new_str = "".join(user_ran_str) - cache.set(new_str, { - "atime":time.time()+time_limit, - "data":release_id - }, time_limit) - return new_str + + token_key = "%s%s"%(CACHE_KEY_TEMPLATE.get("make_token_key"),release_id) + make_token_key = cache.get(token_key) + if make_token_key: + return cache.get(make_token_key) + else: + random_str = uuid.uuid1().__str__().split("-")[0:-1] + user_ran_str = uuid.uuid5(uuid.NAMESPACE_DNS, release_id).__str__().split("-") + user_ran_str.extend(random_str) + new_str = "".join(user_ran_str) + cache.set(new_str, { + "atime":time.time()+time_limit, + "data":release_id + }, time_limit) + cache.set(token_key,new_str,time_limit-1) + return new_str def verify_token(self,token,release_id): try: diff --git a/fir_ser/api/utils/app/apputils.py b/fir_ser/api/utils/app/apputils.py index b962423..ffb079c 100644 --- a/fir_ser/api/utils/app/apputils.py +++ b/fir_ser/api/utils/app/apputils.py @@ -5,8 +5,9 @@ # date: 2020/3/6 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,del_cache_response_by_short +import random +from api.utils.storage.storage import Storage +from api.utils.storage.caches import del_cache_response_by_short def make_resigned(bin_url,img_url,bundle_id,app_version,name): diff --git a/fir_ser/api/utils/crontab/run.py b/fir_ser/api/utils/crontab/run.py index 16981e9..be3b4d3 100644 --- a/fir_ser/api/utils/crontab/run.py +++ b/fir_ser/api/utils/crontab/run.py @@ -6,7 +6,7 @@ from apscheduler.schedulers.background import BackgroundScheduler from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job - +from fir_ser.settings import SYNC_CACHE_TO_DATABASE from api.utils.crontab.sync_cache import sync_download_times import atexit @@ -24,14 +24,16 @@ try: # 调度器使用DjangoJobStore() scheduler.add_jobstore(DjangoJobStore(), "default") + # 设置定时任务,选择方式为interval,时间间隔为10s # 另一种方式为每天固定时间执行任务,对应代码为: # @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='9', minute='30', second='10',id='task_time') - @register_job(scheduler, "interval", seconds=10) + @register_job(scheduler, "interval", seconds=SYNC_CACHE_TO_DATABASE.get("download_times")) def sync_download_times_job(): # 这里写你要执行的任务 sync_download_times() + register_events(scheduler) scheduler.start() except Exception as e: @@ -42,11 +44,10 @@ try: except: pass + def unlock(): fcntl.flock(f, fcntl.LOCK_UN) f.close() -atexit.register(unlock) - - +atexit.register(unlock) diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 09d3c68..74c0192 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -45,3 +45,7 @@ def get_app_download_by_cache(app_id): cache.incr(key) return download_times + 1 + +def del_cache_response_by_short(short,app_id): + cache.delete("%s%s"%(CACHE_KEY_TEMPLATE.get("download_short_key"),short)) + cache.delete("%s%s" % (CACHE_KEY_TEMPLATE.get("app_instance"), app_id)) \ No newline at end of file diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index 241b85f..dfff827 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -104,14 +104,3 @@ class Storage(object): return {**auth_dict, **additionalparameters} - -def del_cache_response_by_short(short,app_id): - id = "download" - rtn = '_'.join([ - id, - "short", - short - ]) - cache.delete(rtn) - app_obj_key = "%s_%s" % ('app_instance', app_id) - cache.delete(app_obj_key) \ No newline at end of file diff --git a/fir_ser/api/views/apps.py b/fir_ser/api/views/apps.py index dbb6294..250839c 100644 --- a/fir_ser/api/views/apps.py +++ b/fir_ser/api/views/apps.py @@ -12,7 +12,8 @@ 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,del_cache_response_by_short +from api.utils.storage.storage import Storage +from api.utils.storage.caches import del_cache_response_by_short from api.models import Apps, AppReleaseInfo from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index e1a80e3..e3dbad4 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -17,7 +17,6 @@ from rest_framework_extensions.cache.decorators import cache_response from api.utils.serializer import AppsSerializer from api.models import Apps,AppReleaseInfo -from django.db.models import F from django.http import FileResponse class DownloadView(APIView): ''' diff --git a/fir_ser/api/views/uploads.py b/fir_ser/api/views/uploads.py index 5419eb9..c3bd337 100644 --- a/fir_ser/api/views/uploads.py +++ b/fir_ser/api/views/uploads.py @@ -5,8 +5,9 @@ # date: 2020/3/6 from api.utils.app.apputils import get_random_short,SaveAppInfos -from api.utils.storage.storage import Storage,del_cache_response_by_short -from api.models import Apps,AppReleaseInfo,UserInfo +from api.utils.storage.storage import Storage +from api.utils.storage.caches import del_cache_response_by_short +from api.models import Apps,AppReleaseInfo from api.utils.app.randomstrings import make_app_uuid from rest_framework.views import APIView from api.utils.response import BaseResponse diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index 30a309d..45993bd 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -234,7 +234,14 @@ THIRD_PART_CONFIG = { } CACHE_KEY_TEMPLATE={ - 'download_times_key':'app_download_times_' + 'download_times_key':'app_download_times_', + 'make_token_key':'make_token_', + 'download_short_key':'download_short_', + 'app_instance_key':'app_instance_' +} + +SYNC_CACHE_TO_DATABASE={ + 'download_times':10 #下载次数同步时间 } SESSION_ENGINE = "django.contrib.sessions.backends.cache"