缓存优化

super_signature
nineven 5 years ago
parent eaa98f49d6
commit 917ed49f74
  1. 1
      fir_client/src/components/FirAppInfostimeline.vue
  2. 12
      fir_ser/api/utils/auth.py
  3. 2
      fir_ser/api/utils/crontab/sync_cache.py
  4. 16
      fir_ser/api/utils/storage/caches.py
  5. 47
      fir_ser/api/utils/storage/storage.py
  6. 9
      fir_ser/api/views/download.py
  7. 4
      fir_ser/api/views/login.py
  8. 4
      fir_ser/fir_ser/settings.py

@ -168,6 +168,7 @@
this.release_apps = data.data.release_apps;
this.currentapp = data.data.currentapp;
} else if (data.code === 1003) {
loading.close();
this.$router.push({name: 'FirApps'});
}
loading.close();

@ -1,12 +1,8 @@
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
import datetime
from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from rest_framework.exceptions import AuthenticationFailed
from api.models import Token, UserInfo
import pytz
from fir_ser.settings import CACHE_KEY_TEMPLATE
import base64
class ExpiringTokenAuthentication(BaseAuthentication):
@ -26,7 +22,9 @@ class ExpiringTokenAuthentication(BaseAuthentication):
if not auth_token:
raise AuthenticationFailed({"code": 1001, "error": "缺少token"})
cacheuserinfo = cache.get(auth_token)
auth_key = "_".join([CACHE_KEY_TEMPLATE.get('user_auth_token_key'), auth_token])
cacheuserinfo = cache.get(auth_key)
if not cacheuserinfo:
raise AuthenticationFailed({"code": 1001, "error": "无效的token"})
if user_name != cacheuserinfo.get('username',None):
@ -37,7 +35,7 @@ class ExpiringTokenAuthentication(BaseAuthentication):
if not user_obj:
raise AuthenticationFailed({"code": 1001, "error": "无效的token"})
if user_obj.is_active:
cache.set(auth_token, cacheuserinfo, 3600 * 24 * 7)
cache.set(auth_key, cacheuserinfo, 3600 * 24 * 7)
return user_obj,auth_token
else:
raise AuthenticationFailed({"code": 1001, "error": "用户被禁用"})

@ -11,7 +11,7 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE
def sync_download_times():
down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key")
key = "%s%s" %(down_tem_key,'*')
key = "_".join([down_tem_key,'*'])
for app_download in cache.iter_keys(key):
app_id = app_download.split(down_tem_key)[1]
Apps.objects.filter(app_id=app_id).update(count_hits=cache.get(app_download))

@ -27,24 +27,24 @@ def get_download_url_by_cache(app_obj, filename, limit, isdownload=True):
def get_app_instance_by_cache(app_id, limit):
app_obj_cache = cache.get("%s_%s" % ('app_instance', app_id))
app_key="_".join([CACHE_KEY_TEMPLATE.get("app_instance_key"),app_id])
app_obj_cache = cache.get(app_key)
if not app_obj_cache:
app_obj_cache = Apps.objects.filter(app_id=app_id).values("pk", 'user_id', 'type').first()
cache.set("%s_%s" % ('app_instance', app_id), app_obj_cache, limit)
cache.set(app_key, app_obj_cache, limit)
return app_obj_cache
def get_app_download_by_cache(app_id):
down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key")
key = "%s%s" %(down_tem_key,app_id)
download_times = cache.get(key)
down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_times_key"),app_id])
download_times = cache.get(down_tem_key)
if not download_times:
download_times=Apps.objects.filter(app_id=app_id).values("count_hits").first().get('count_hits')
cache.set(key, download_times + 1, 900)
cache.set(down_tem_key, download_times + 1, 900)
else:
cache.incr(key)
cache.incr(down_tem_key)
return download_times + 1
def del_cache_response_by_short(short,app_id):
cache.delete("_".join([CACHE_KEY_TEMPLATE.get("download_short_key"),short]))
cache.delete("_".join([CACHE_KEY_TEMPLATE.get("app_instance"),app_id]))
cache.delete("_".join([CACHE_KEY_TEMPLATE.get("app_instance_key"),app_id]))

@ -8,8 +8,8 @@ from api.models import AppStorage, UserInfo
from .aliyunApi import AliYunOss
from .qiniuApi import QiNiuOss
from .localApi import LocalStorage
import json,time
from fir_ser.settings import THIRD_PART_CONFIG,CACHE_KEY_TEMPLATE
import json, time, base64
from fir_ser.settings import THIRD_PART_CONFIG, CACHE_KEY_TEMPLATE
from django.core.cache import cache
@ -21,17 +21,17 @@ class Storage(object):
if self.storage:
return self.storage.get_upload_token(filename, expires)
def get_download_url(self,filename, expires=900,ftype=None,key=''):
def get_download_url(self, filename, expires=900, ftype=None, key=''):
if self.storage:
now = time.time()
down_key = "_".join([key.lower(),CACHE_KEY_TEMPLATE.get('download_url_key'),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.get("time") > now - 60:
return download_val.get("download_url")
download_url=self.storage.get_download_url(filename, expires,ftype)
cache.set(down_key,{"download_url":download_url,"time":now+expires},expires)
download_url = self.storage.get_download_url(filename, expires, ftype)
cache.set(down_key, {"download_url": download_url, "time": now + expires}, expires)
return download_url
def delete_file(self, filename, apptype=None):
@ -47,14 +47,22 @@ class Storage(object):
self.storage_obj = user.storage
if self.storage_obj:
auth = self.get_storage_auth(self.storage_obj)
storage_key = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'),
base64.b64encode(json.dumps(auth).encode("utf-8")).decode("utf-8")[0:64]])
storage_type = self.storage_obj.storage_type
new_storage_obj = cache.get(storage_key)
if new_storage_obj:
return new_storage_obj
else:
if storage_type == 1:
new_storage_obj = QiNiuOss(**auth)
elif storage_type == 2:
new_storage_obj = AliYunOss(**auth)
else:
new_storage_obj = LocalStorage(**auth)
new_storage_obj.storage_type = storage_type
cache.set(storage_key, new_storage_obj, 600)
return new_storage_obj
else:
return self.get_default_storage()
@ -66,25 +74,28 @@ class Storage(object):
else:
storage_lists = THIRD_PART_CONFIG.get('storage')
for storage in storage_lists:
if storage.get("active",None):
storage_type= storage.get('type',None)
auth = storage.get('auth',{})
if storage.get("active", None):
storage_type = storage.get('type', None)
auth = storage.get('auth', {})
storage_key = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'),
base64.b64encode(json.dumps(auth).encode("utf-8")).decode("utf-8")[0:64]])
new_storage_obj = cache.get(storage_key)
if new_storage_obj:
return new_storage_obj
else:
if storage_type == 1:
new_storage_obj = QiNiuOss(**auth)
new_storage_obj.storage_type=1
new_storage_obj.storage_type = 1
elif storage_type == 2:
new_storage_obj = AliYunOss(**auth)
new_storage_obj.storage_type=2
new_storage_obj.storage_type = 2
else:
new_storage_obj = LocalStorage(**auth)
new_storage_obj.storage_type=3
new_storage_obj.storage_type = 3
cache.set(storage_key, new_storage_obj, 600)
return new_storage_obj
return None
def get_storage_type(self):
if self.storage:
return self.storage.storage_type
@ -95,7 +106,7 @@ class Storage(object):
'secret_key': storage_obj.secret_key,
'bucket_name': storage_obj.bucket_name,
'domain_name': storage_obj.domain_name,
'is_https':storage_obj.is_https
'is_https': storage_obj.is_https
}
try:
additionalparameters = json.loads(storage_obj.additionalparameters)
@ -103,5 +114,3 @@ class Storage(object):
print(e)
additionalparameters = {}
return {**auth_dict, **additionalparameters}

@ -88,14 +88,7 @@ class ShortDownloadView(APIView):
#key的设置
def calculate_cache_key(self, view_instance, view_method,
request, args, kwargs):
id = "download"
rtn = '_'.join([
id,
"short",
kwargs.get("short",None)
])
# print( request.META)
return rtn
return "_".join([settings.CACHE_KEY_TEMPLATE.get("download_short_key"), kwargs.get("short", None)])
class InstallView(APIView):
'''

@ -10,6 +10,7 @@ from api.utils.TokenManager import DownloadToken,generateNumericTokenOfLength
from api.utils.auth import ExpiringTokenAuthentication
from api.utils.response import BaseResponse
from django.middleware import csrf
from fir_ser.settings import CACHE_KEY_TEMPLATE
def get_token(request):
token = csrf.get_token(request)
@ -39,7 +40,8 @@ class LoginView(APIView):
now = datetime.datetime.now()
user_info = UserInfo.objects.get(pk=user.pk)
cache.set(key,{'uid':user_info.uid,'username':user_info.username},3600*24*7)
auth_key = "_".join([CACHE_KEY_TEMPLATE.get('user_auth_token_key'),key])
cache.set(auth_key,{'uid':user_info.uid,'username':user_info.username},3600*24*7)
Token.objects.create(user=user, **{"access_token": key, "created": now})
serializer = UserInfoSerializer(user_info,)

@ -238,7 +238,9 @@ CACHE_KEY_TEMPLATE={
'make_token_key':'make_token',
'download_short_key':'download_short',
'app_instance_key':'app_instance',
'download_url_key':'download_url'
'download_url_key':'download_url',
'user_storage_key':'storage_auth',
'user_auth_token_key':'user_auth_token'
}
SYNC_CACHE_TO_DATABASE={

Loading…
Cancel
Save