缓存优化

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. 23
      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.release_apps = data.data.release_apps;
this.currentapp = data.data.currentapp; this.currentapp = data.data.currentapp;
} else if (data.code === 1003) { } else if (data.code === 1003) {
loading.close();
this.$router.push({name: 'FirApps'}); this.$router.push({name: 'FirApps'});
} }
loading.close(); loading.close();

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

@ -11,7 +11,7 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE
def sync_download_times(): def sync_download_times():
down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") 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): for app_download in cache.iter_keys(key):
app_id = app_download.split(down_tem_key)[1] app_id = app_download.split(down_tem_key)[1]
Apps.objects.filter(app_id=app_id).update(count_hits=cache.get(app_download)) 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): 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: if not app_obj_cache:
app_obj_cache = Apps.objects.filter(app_id=app_id).values("pk", 'user_id', 'type').first() 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 return app_obj_cache
def get_app_download_by_cache(app_id): def get_app_download_by_cache(app_id):
down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_times_key"),app_id])
key = "%s%s" %(down_tem_key,app_id) download_times = cache.get(down_tem_key)
download_times = cache.get(key)
if not download_times: if not download_times:
download_times=Apps.objects.filter(app_id=app_id).values("count_hits").first().get('count_hits') 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: else:
cache.incr(key) cache.incr(down_tem_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("_".join([CACHE_KEY_TEMPLATE.get("download_short_key"),short])) 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,7 +8,7 @@ from api.models import AppStorage, UserInfo
from .aliyunApi import AliYunOss 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, base64
from fir_ser.settings import THIRD_PART_CONFIG, CACHE_KEY_TEMPLATE from fir_ser.settings import THIRD_PART_CONFIG, CACHE_KEY_TEMPLATE
from django.core.cache import cache from django.core.cache import cache
@ -47,14 +47,22 @@ class Storage(object):
self.storage_obj = user.storage self.storage_obj = user.storage
if self.storage_obj: if self.storage_obj:
auth = self.get_storage_auth(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 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: if storage_type == 1:
new_storage_obj = QiNiuOss(**auth) new_storage_obj = QiNiuOss(**auth)
elif storage_type == 2: elif storage_type == 2:
new_storage_obj = AliYunOss(**auth) new_storage_obj = AliYunOss(**auth)
else: else:
new_storage_obj = LocalStorage(**auth) new_storage_obj = LocalStorage(**auth)
new_storage_obj.storage_type = storage_type new_storage_obj.storage_type = storage_type
cache.set(storage_key, new_storage_obj, 600)
return new_storage_obj return new_storage_obj
else: else:
return self.get_default_storage() return self.get_default_storage()
@ -69,6 +77,12 @@ class Storage(object):
if storage.get("active", None): if storage.get("active", None):
storage_type = storage.get('type', None) storage_type = storage.get('type', None)
auth = storage.get('auth', {}) 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: if storage_type == 1:
new_storage_obj = QiNiuOss(**auth) new_storage_obj = QiNiuOss(**auth)
new_storage_obj.storage_type = 1 new_storage_obj.storage_type = 1
@ -78,13 +92,10 @@ class Storage(object):
else: else:
new_storage_obj = LocalStorage(**auth) 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 new_storage_obj
return None return None
def get_storage_type(self): def get_storage_type(self):
if self.storage: if self.storage:
return self.storage.storage_type return self.storage.storage_type
@ -103,5 +114,3 @@ class Storage(object):
print(e) print(e)
additionalparameters = {} additionalparameters = {}
return {**auth_dict, **additionalparameters} return {**auth_dict, **additionalparameters}

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

@ -10,6 +10,7 @@ from api.utils.TokenManager import DownloadToken,generateNumericTokenOfLength
from api.utils.auth import ExpiringTokenAuthentication from api.utils.auth import ExpiringTokenAuthentication
from api.utils.response import BaseResponse from api.utils.response import BaseResponse
from django.middleware import csrf from django.middleware import csrf
from fir_ser.settings import CACHE_KEY_TEMPLATE
def get_token(request): def get_token(request):
token = csrf.get_token(request) token = csrf.get_token(request)
@ -39,7 +40,8 @@ class LoginView(APIView):
now = datetime.datetime.now() now = datetime.datetime.now()
user_info = UserInfo.objects.get(pk=user.pk) 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}) Token.objects.create(user=user, **{"access_token": key, "created": now})
serializer = UserInfoSerializer(user_info,) serializer = UserInfoSerializer(user_info,)

@ -238,7 +238,9 @@ CACHE_KEY_TEMPLATE={
'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' 'download_url_key':'download_url',
'user_storage_key':'storage_auth',
'user_auth_token_key':'user_auth_token'
} }
SYNC_CACHE_TO_DATABASE={ SYNC_CACHE_TO_DATABASE={

Loading…
Cancel
Save