修复切换存储,缓存数据未删除

super_signature
nineven 5 years ago
parent 60b514fc0c
commit 15e88b9b59
  1. 23
      fir_ser/api/utils/storage/caches.py
  2. 8
      fir_ser/api/utils/storage/storage.py
  3. 8
      fir_ser/api/views/storage.py

@ -5,8 +5,8 @@
# date: 2020/4/7
from django.core.cache import cache
from api.models import Apps,UserInfo,AppReleaseInfo
import time,os
from api.models import Apps,UserInfo,AppReleaseInfo,AppStorage
import time,os,base64,json
from django.utils import timezone
from fir_ser.settings import CACHE_KEY_TEMPLATE
from api.utils.storage.storage import Storage,LocalStorage
@ -73,8 +73,27 @@ def del_cache_response_by_short(short,app_id):
download_val = CACHE_KEY_TEMPLATE.get('download_url_key')
cache.delete("_".join([key, download_val, os.path.basename(master_release_dict.get("icon_url"))]))
cache.delete("_".join([key,download_val, master_release_dict.get('release_id')]))
cache.delete("_".join([key, CACHE_KEY_TEMPLATE.get("make_token_key"), master_release_dict.get('release_id')]))
def del_cache_by_app_id(app_id,user_obj):
key=''
master_release_dict = AppReleaseInfo.objects.filter(app_id__app_id=app_id,is_master=True).values('icon_url','release_id').first()
download_val = CACHE_KEY_TEMPLATE.get('download_url_key')
cache.delete("_".join([key, download_val, os.path.basename(master_release_dict.get("icon_url"))]))
cache.delete("_".join([key,download_val, master_release_dict.get('release_id')]))
cache.delete("_".join([key.lower(), CACHE_KEY_TEMPLATE.get("make_token_key"), master_release_dict.get('release_id')]))
cache.delete("_".join([key,download_val,user_obj.head_img]))
def del_cache_storage(user_obj):
for app_obj in Apps.objects.filter(user_id=user_obj):
del_cache_response_by_short(app_obj.short, app_obj.app_id)
del_cache_by_app_id(app_obj.app_id,user_obj)
storage_keys = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'),user_obj.uid,'*'])
for storage_key in cache.iter_keys(storage_keys):
cache.delete(storage_key)
def set_app_today_download_times(app_id):
now = timezone.now()

@ -50,7 +50,7 @@ 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'),
storage_key = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'),user.uid,
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)
@ -68,9 +68,9 @@ class Storage(object):
cache.set(storage_key, new_storage_obj, 600)
return new_storage_obj
else:
return self.get_default_storage()
return self.get_default_storage(user)
def get_default_storage(self):
def get_default_storage(self,user):
admin_storage = UserInfo.objects.first().storage
if admin_storage:
return self.get_storage(UserInfo)
@ -80,7 +80,7 @@ class Storage(object):
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'),
storage_key = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'),user.uid,
base64.b64encode(json.dumps(auth).encode("utf-8")).decode("utf-8")[0:64]])
new_storage_obj = cache.get(storage_key)
if new_storage_obj:

@ -9,12 +9,7 @@ from api.utils.response import BaseResponse
from api.utils.auth import ExpiringTokenAuthentication
from rest_framework.response import Response
import json
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
from api.utils.storage.caches import del_cache_response_by_short, get_app_today_download_times
from api.utils.storage.caches import del_cache_storage
from api.models import AppStorage,UserInfo
from api.utils.serializer import StorageSerializer
@ -80,6 +75,7 @@ class StorageView(APIView):
UserInfo.objects.filter(pk=request.user.pk).update(storage=None)
else:
UserInfo.objects.filter(pk=request.user.pk).update(storage_id=use_storage_id)
del_cache_storage(request.user)
except Exception as e:
print(e)
res.code=1006

Loading…
Cancel
Save