diff --git a/fir_ser/api/utils/crontab/sync_cache.py b/fir_ser/api/utils/crontab/sync_cache.py index 5b3a6eb..c5f87d2 100644 --- a/fir_ser/api/utils/crontab/sync_cache.py +++ b/fir_ser/api/utils/crontab/sync_cache.py @@ -13,8 +13,19 @@ def sync_download_times(): down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") key = "_".join([down_tem_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].strip('_') Apps.objects.filter(app_id=app_id).update(count_hits=cache.get(app_download)) +def sync_download_times_by_app_id(app_ids): + app_id_lists = [] + for app_id in app_ids: + down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_times_key"),app_id.get("app_id")]) + app_id_lists.append(down_tem_key) + down_times_lists = cache.get_many(app_id_lists) + for k, v in down_times_lists.items(): + app_id = k.split(CACHE_KEY_TEMPLATE.get("download_times_key"))[1].strip('_') + Apps.objects.filter(app_id=app_id).update(count_hits=v) + + diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 9d5bce1..a93b939 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -7,8 +7,10 @@ from django.core.cache import cache from api.models import Apps,UserInfo import time +from django.utils import timezone from fir_ser.settings import CACHE_KEY_TEMPLATE from api.utils.storage.storage import Storage,LocalStorage +from api.utils.crontab.sync_cache import sync_download_times_by_app_id from api.utils.crontab import run def get_download_url_by_cache(app_obj, filename, limit, isdownload=True): @@ -35,16 +37,43 @@ def get_app_instance_by_cache(app_id, limit): return app_obj_cache -def get_app_download_by_cache(app_id): +def set_app_download_by_cache(app_id, limit=900): 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(down_tem_key, download_times + 1, 900) + cache.set(down_tem_key, download_times + 1, limit) else: cache.incr(down_tem_key) + cache.expire(down_tem_key, timeout=limit) + set_app_today_download_times(app_id) 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_key"),app_id])) + + +def set_app_today_download_times(app_id): + now = timezone.now() + down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_today_times_key"), + str(now.year),str(now.month),str(now.day),app_id]) + if cache.get(down_tem_key): + cache.incr(down_tem_key) + else: + cache.set(down_tem_key,1,3600*24) + +def get_app_today_download_times(app_ids): + sync_download_times_by_app_id(app_ids) + + now = timezone.now() + app_id_lists=[] + download_times_count=0 + for app_id in app_ids: + down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_today_times_key"), + str(now.year),str(now.month),str(now.day),app_id.get("app_id")]) + app_id_lists.append(down_tem_key) + down_times_lists = cache.get_many(app_id_lists) + for k, v in down_times_lists.items(): + download_times_count+=v + return download_times_count diff --git a/fir_ser/api/views/apps.py b/fir_ser/api/views/apps.py index 250839c..3fa1869 100644 --- a/fir_ser/api/views/apps.py +++ b/fir_ser/api/views/apps.py @@ -13,8 +13,7 @@ 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 - +from api.utils.storage.caches import del_cache_response_by_short,get_app_today_download_times from api.models import Apps, AppReleaseInfo from api.utils.serializer import AppsSerializer, AppReleaseSerializer, UserInfoSerializer from rest_framework.pagination import PageNumberPagination @@ -43,6 +42,13 @@ class AppsView(APIView): res.hdata["upload_domain"] = request.user.domain_name res.hdata["ios_count"] = Apps.objects.filter(type=1, user_id=request.user).values('app_id').count() res.hdata["android_count"] = Apps.objects.filter(type=0, user_id=request.user).values('app_id').count() + + android_app_ids = Apps.objects.filter(**{"user_id": request.user, "type": 0}).values('app_id') + res.hdata["android_today_hits_count"] = get_app_today_download_times(android_app_ids) + + ios_app_ids = Apps.objects.filter(**{"user_id": request.user, "type": 1}).values('app_id') + res.hdata["ios_today_hits_count"] = get_app_today_download_times(ios_app_ids) + all_hits_obj = Apps.objects.filter(user_id=request.user).aggregate(count_hits=Sum('count_hits')) if all_hits_obj: count_hits = all_hits_obj.get("count_hits", 0) @@ -55,8 +61,7 @@ class AppsView(APIView): res.hdata["all_hits_count"] = count_hits else: res.hdata["all_hits_count"] = 0 - res.hdata["android_today_hits_count"] = 0 - res.hdata["ios_today_hits_count"] = 0 + if app_type == "android": filter_data = {"user_id": request.user, "type": 0} diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index 55ff415..af8136c 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -11,7 +11,7 @@ from api.utils.TokenManager import DownloadToken from api.utils.app.randomstrings import make_random_uuid from api.utils.app.apputils import make_resigned from api.utils.storage.storage import Storage -from api.utils.storage.caches import get_app_instance_by_cache,get_download_url_by_cache,get_app_download_by_cache +from api.utils.storage.caches import get_app_instance_by_cache,get_download_url_by_cache,set_app_download_by_cache import os from rest_framework_extensions.cache.decorators import cache_response @@ -115,7 +115,7 @@ class InstallView(APIView): # Apps.objects.filter(app_id=app_id).update(count_hits=F('count_hits') + 1) # UserInfo.objects.filter(pk=app_obj.get("user_id")).update(all_download_times=F('all_download_times') + 1) - get_app_download_by_cache(app_id) + set_app_download_by_cache(app_id) # release_obj = AppReleaseInfo.objects.filter(app_id=app_obj.get('pk')).values("release_id") # if release_obj: if app_obj.get("type") == 0: diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index 1df4e27..ede2ce3 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -131,7 +131,7 @@ USE_I18N = True USE_L10N = True -USE_TZ = True +USE_TZ = False # Static files (CSS, JavaScript, Images) @@ -232,7 +232,6 @@ THIRD_PART_CONFIG = { ] } - CACHE_KEY_TEMPLATE={ 'download_times_key':'app_download_times', 'make_token_key':'make_token', @@ -240,7 +239,8 @@ CACHE_KEY_TEMPLATE={ 'app_instance_key':'app_instance', 'download_url_key':'download_url', 'user_storage_key':'storage_auth', - 'user_auth_token_key':'user_auth_token' + 'user_auth_token_key':'user_auth_token', + 'download_today_times_key':'download_today_times' } SYNC_CACHE_TO_DATABASE={