优化访问速度

super_signature
nineven 5 years ago
parent a8b5d4dbbe
commit 7421d1a38d
  1. 9
      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
from threading import Thread
from django.core.cache import cache
from fir_ser.settings import CACHE_KEY_TEMPLATE
'''
user = cache.get(token)
@ -20,6 +20,12 @@ from django.core.cache import cache
class DownloadToken(object):
def make_token(self,release_id,time_limit=60):
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)
@ -28,6 +34,7 @@ class DownloadToken(object):
"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):

@ -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):

@ -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)

@ -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))

@ -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)

@ -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

@ -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):
'''

@ -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

@ -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"

Loading…
Cancel
Save