From 791a753162c2ae3808466eaa948c4e0264ca5e68 Mon Sep 17 00:00:00 2001 From: xianfuxing Date: Tue, 11 Aug 2020 16:20:17 +0800 Subject: [PATCH] =?UTF-8?q?#=20=E4=BC=98=E5=8C=96=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/counter/api/views.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/counter/api/views.py b/apps/counter/api/views.py index 4da39c9..732c307 100644 --- a/apps/counter/api/views.py +++ b/apps/counter/api/views.py @@ -97,6 +97,17 @@ class LatestStatisticAPIView(APIView, RoleMixin): permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): + username = request.user.username + cur_month_count_key = 'cur_month_count_{}'.format(username) + daily_count_key = 'daily_count_{}'.format(username) + cur_month_count = cache.get(cur_month_count_key) + daily_count = cache.get(daily_count_key) + + if cur_month_count and daily_count: + data = {'cur_month_count': cur_month_count, 'daily_count': daily_count} + return Response(data) + + # no cache devices = self.get_devices() total_count_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0] @@ -122,6 +133,8 @@ class LatestStatisticAPIView(APIView, RoleMixin): else: cur_month_count = total_count + # set month cache + cache.set(cur_month_count_key, cur_month_count, 60 * 10) midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) one_day_ago_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0] for x in DeviceCount.objects.filter( @@ -132,6 +145,9 @@ class LatestStatisticAPIView(APIView, RoleMixin): else: daily_count = 0 + # set daily cache + cache.set(daily_count_key, daily_count, 60 * 10) + data = {'cur_month_count': cur_month_count, 'daily_count': daily_count} return Response(data)