优化访问速度

super_signature
nineven 5 years ago
parent a8b5d4dbbe
commit 7421d1a38d
  1. 27
      fir_ser/api/utils/TokenManager.py
  2. 5
      fir_ser/api/utils/app/apputils.py
  3. 11
      fir_ser/api/utils/crontab/run.py
  4. 4
      fir_ser/api/utils/storage/caches.py
  5. 11
      fir_ser/api/utils/storage/storage.py
  6. 3
      fir_ser/api/views/apps.py
  7. 1
      fir_ser/api/views/download.py
  8. 5
      fir_ser/api/views/uploads.py
  9. 9
      fir_ser/fir_ser/settings.py

@ -9,7 +9,7 @@ import random
import time import time
from threading import Thread from threading import Thread
from django.core.cache import cache from django.core.cache import cache
from fir_ser.settings import CACHE_KEY_TEMPLATE
''' '''
user = cache.get(token) user = cache.get(token)
@ -20,15 +20,22 @@ from django.core.cache import cache
class DownloadToken(object): class DownloadToken(object):
def make_token(self,release_id,time_limit=60): 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("-") token_key = "%s%s"%(CACHE_KEY_TEMPLATE.get("make_token_key"),release_id)
user_ran_str.extend(random_str) make_token_key = cache.get(token_key)
new_str = "".join(user_ran_str) if make_token_key:
cache.set(new_str, { return cache.get(make_token_key)
"atime":time.time()+time_limit, else:
"data":release_id random_str = uuid.uuid1().__str__().split("-")[0:-1]
}, time_limit) user_ran_str = uuid.uuid5(uuid.NAMESPACE_DNS, release_id).__str__().split("-")
return new_str 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): def verify_token(self,token,release_id):
try: try:

@ -5,8 +5,9 @@
# date: 2020/3/6 # date: 2020/3/6
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
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
def make_resigned(bin_url,img_url,bundle_id,app_version,name): def make_resigned(bin_url,img_url,bundle_id,app_version,name):

@ -6,7 +6,7 @@
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job 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 from api.utils.crontab.sync_cache import sync_download_times
import atexit import atexit
@ -24,14 +24,16 @@ try:
# 调度器使用DjangoJobStore() # 调度器使用DjangoJobStore()
scheduler.add_jobstore(DjangoJobStore(), "default") scheduler.add_jobstore(DjangoJobStore(), "default")
# 设置定时任务,选择方式为interval,时间间隔为10s # 设置定时任务,选择方式为interval,时间间隔为10s
# 另一种方式为每天固定时间执行任务,对应代码为: # 另一种方式为每天固定时间执行任务,对应代码为:
# @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='9', minute='30', second='10',id='task_time') # @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(): def sync_download_times_job():
# 这里写你要执行的任务 # 这里写你要执行的任务
sync_download_times() sync_download_times()
register_events(scheduler) register_events(scheduler)
scheduler.start() scheduler.start()
except Exception as e: except Exception as e:
@ -42,11 +44,10 @@ try:
except: except:
pass pass
def unlock(): def unlock():
fcntl.flock(f, fcntl.LOCK_UN) fcntl.flock(f, fcntl.LOCK_UN)
f.close() f.close()
atexit.register(unlock)
atexit.register(unlock)

@ -45,3 +45,7 @@ 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):
cache.delete("%s%s"%(CACHE_KEY_TEMPLATE.get("download_short_key"),short))
cache.delete("%s%s" % (CACHE_KEY_TEMPLATE.get("app_instance"), app_id))

@ -104,14 +104,3 @@ class Storage(object):
return {**auth_dict, **additionalparameters} 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)

@ -12,7 +12,8 @@ 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,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.models import Apps, AppReleaseInfo
from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer

@ -17,7 +17,6 @@ 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
from django.db.models import F
from django.http import FileResponse from django.http import FileResponse
class DownloadView(APIView): class DownloadView(APIView):
''' '''

@ -5,8 +5,9 @@
# 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,del_cache_response_by_short from api.utils.storage.storage import Storage
from api.models import Apps,AppReleaseInfo,UserInfo 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 api.utils.app.randomstrings import make_app_uuid
from rest_framework.views import APIView from rest_framework.views import APIView
from api.utils.response import BaseResponse from api.utils.response import BaseResponse

@ -234,7 +234,14 @@ 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_',
'download_short_key':'download_short_',
'app_instance_key':'app_instance_'
}
SYNC_CACHE_TO_DATABASE={
'download_times':10 #下载次数同步时间
} }
SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_ENGINE = "django.contrib.sessions.backends.cache"

Loading…
Cancel
Save