Prechádzať zdrojové kódy

AnsjerPush增加缓存

WuYiPei 5 rokov pred
rodič
commit
338afc8796
2 zmenil súbory, kde vykonal 262 pridanie a 52 odobranie
  1. 38 21
      Controller/DetectController.py
  2. 224 31
      requirements.txt

+ 38 - 21
Controller/DetectController.py

@@ -42,10 +42,10 @@ class NotificationView(View):
         # operation = kwargs.get('operation')
         return self.validation(request.GET)
 
-    def post(self, request, *args, **kwargs):
-        request.encoding = 'utf-8'
-        # operation = kwargs.get('operation')
-        return self.validation(request.POST)
+    # def post(self, request, *args, **kwargs):
+    #     request.encoding = 'utf-8'
+    #     # operation = kwargs.get('operation')
+    #     return self.validation(request.POST)
 
     def validation11(self, request_dict):
         response = ResponseObject()
@@ -71,9 +71,12 @@ class NotificationView(View):
             # 设置推送间隔60秒一次
             redisObj.set_data(key=pkey, val=1, expire=60)
         uid_set_qs = UidSetModel.objects.filter(uid=uid, detect_status=1)
+        # redisObj.set_data(key=pkey, val=list[0], expire=600)
+        return response.json(0)
         if uid_set_qs.exists():
             uid_set_id = uid_set_qs[0].id
             nickname = uid_set_qs[0].nickname
+            # if not is ==none
             if not nickname:
                 nickname = uid
             uid_push_qs = UidPushModel.objects.filter(uid_set__id=uid_set_id). \
@@ -136,31 +139,46 @@ class NotificationView(View):
                 'code': 444,
                 'msg': 'param is wrong'})
             # return response.json(444)
+        # 执行下面utko窗口会输出{'uid',""}
         utko = UidTokenObject(uidToken)
         uid = utko.UID
         redisObj = RedisObject(db=6)
         # pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
-        pkey = '{uid}_ptl'.format(uid=uid)
+        pkey = '{uid}_pptl'.format(uid=uid)
+        # 判断redisObj.get_data(key=pkey):不为空
         if redisObj.get_data(key=pkey):
             res_data = {'code': 0, 'msg': 'success,!'}
-            return JsonResponse(status=200, data=res_data)
+            # 查询key对应的value
+            res = redisObj.get_data(key=pkey)
+            # redis中的字符串转回list
+            redis_list = eval(res)
         else:
-            # 设置推送间隔60秒一次
-            redisObj.set_data(key=pkey, val=1, expire=60)
-        # uid_set_id = uid_set_qs[0].id
-
-        uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
-            values('token_val', 'app_type', 'appBundleId',
-                   'push_type', 'userID_id', 'userID__NickName',
-                   'lang', 'tz','uid_set__nickname')
-
-        if uid_push_qs.exists():
-            nickname = uid_push_qs[0]['uid_set__nickname']
+            # 从数据库查询出来
+            uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
+                values('token_val', 'app_type', 'appBundleId',
+                       'push_type', 'userID_id', 'userID__NickName',
+                       'lang', 'tz', 'uid_set__nickname')
+            # 新建一个list接收数据
+            uid_push_qs_list = []
+            print("从数据库查到数据")
+            # 把数据库数据追加进uid_push_qs_list
+            for qs in uid_push_qs:
+                uid_push_qs_list.append(qs)
+            print(str(uid_push_qs_list))
+            # 修改redis数据,并设置过期时间为10分钟
+            redisObj.set_data(key=pkey, val=str(uid_push_qs_list), expire=600)
+            # 查询key对应的value
+            res = redisObj.get_data(key=pkey)
+            # 把redis中的字符串转回list
+            redis_list = eval(res)
+            # return JsonResponse(status=200, data={'code': 0, 'msg': "Data has been saved in redis"})
+        if redis_list:
+            nickname = redis_list[0]['uid_set__nickname']
             if not nickname:
                 nickname = uid
             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
-            for up in uid_push_qs:
+            for up in redis_list:
                 push_type = up['push_type']
                 # ios apns
                 print(push_type)
@@ -175,7 +193,7 @@ class NotificationView(View):
                     self.do_jpush(request_dict, up, response, uid, channel, nickname)
                 # self.do_save_equipment_info(ua, n_time, channel, event_type, is_st)
             # 需求不一样,所以这么做的
-            self.do_bulk_create_info(uid_push_qs, n_time, channel, event_type, is_st, uid)
+            self.do_bulk_create_info(redis_list, n_time, channel, event_type, is_st, uid)
             if is_st == '0' or is_st == '2':
                 return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
             elif is_st == '1':
@@ -200,6 +218,7 @@ class NotificationView(View):
         else:
             return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
 
+######
     def do_jpush(self, request_dict, uaql, response, uid, channel, nickname):
         event_type = request_dict.get('event_type', None)
         n_time = request_dict.get('n_time', None)
@@ -307,7 +326,6 @@ class NotificationView(View):
         send_text = self.get_send_text(channel=channel, n_time=n_time, lang=lang, tz=tz, event_type=event_type)
         try:
             print('---')
-
             cli = apns2.APNSClient(mode=APNS_MODE,
                                    client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
             # password=APNS_CONFIG[appBundleId]['password'])
@@ -387,7 +405,6 @@ class PushNotificationView(View):
             redisObj = RedisObject(db=6)
             # pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
             pkey = '{uid}_ptl'.format(uid=uid)
-            # 推送时间限制
             if redisObj.get_data(key=pkey):
                 res_data = {'code': 0, 'msg': 'success,!'}
                 return JsonResponse(status=200, data=res_data)

+ 224 - 31
requirements.txt

@@ -1,31 +1,224 @@
-Django > 2
-django-cors-headers
-django-imagekit
-django-guardian
-django-rest-framework
-simplejson
-djangorestframework-jwt
-South
-qcloudsms_py
-itsdangerous
-openpyxl
-xlrd
-boto3
-requests_aws4auth
-ffmpy
-xmltodict
-var_dump
-django-middleware-global-request
-oss2
-aliyun-python-sdk-sts
-jpush
-pyipip
-boto
-django-ratelimit
-paypalrestsdk
-pymongo
-apns2-client
-pillow
-redis
-pyfcm
-ipip-ipdb
+alabaster==0.7.12
+anaconda-client==1.7.2
+anaconda-navigator==1.9.7
+anaconda-project==0.8.3
+asgiref==3.2.3
+asn1crypto==1.0.1
+astroid==2.3.1
+astropy==3.2.1
+atomicwrites==1.3.0
+attrs==19.2.0
+Babel==2.7.0
+backcall==0.1.0
+backports.functools-lru-cache==1.5
+backports.os==0.1.1
+backports.shutil-get-terminal-size==1.0.0
+backports.tempfile==1.0
+backports.weakref==1.0.post1
+beautifulsoup4==4.8.0
+bitarray==1.0.1
+bkcharts==0.2
+bleach==3.1.0
+bokeh==1.3.4
+boto==2.49.0
+Bottleneck==1.2.1
+certifi==2019.9.11
+cffi==1.12.3
+chardet==3.0.4
+Click==7.0
+cloudpickle==1.2.2
+clyent==1.2.2
+colorama==0.4.1
+comtypes==1.1.7
+conda==4.7.12
+conda-build==3.18.9
+conda-package-handling==1.6.0
+conda-verify==3.4.2
+contextlib2==0.6.0
+cryptography==2.7
+cycler==0.10.0
+Cython==0.29.13
+cytoolz==0.10.0
+dask==2.5.2
+decorator==4.4.0
+defusedxml==0.6.0
+distributed==2.5.2
+Django==3.0.3
+django-appconf==1.0.3
+django-cors-headers==3.2.1
+django-imagekit==4.0.2
+django-tinymce==2.8.0
+docutils==0.15.2
+entrypoints==0.3
+et-xmlfile==1.0.1
+fastcache==1.1.0
+filelock==3.0.12
+Flask==1.1.1
+fsspec==0.5.2
+future==0.17.1
+gevent==1.4.0
+glob2==0.7
+greenlet==0.4.15
+gunicorn==20.0.4
+h5py==2.9.0
+HeapDict==1.0.1
+html5lib==1.0.1
+idna==2.8
+imageio==2.6.0
+imagesize==1.1.0
+importlib-metadata==0.23
+ipykernel==5.1.2
+ipython==7.8.0
+ipython-genutils==0.2.0
+ipywidgets==7.5.1
+isort==4.3.21
+itsdangerous==1.1.0
+jdcal==1.4.1
+jedi==0.15.1
+Jinja2==2.10.3
+joblib==0.13.2
+json5==0.8.5
+jsonschema==3.0.2
+jupyter==1.0.0
+jupyter-client==5.3.3
+jupyter-console==6.0.0
+jupyter-core==4.5.0
+jupyterlab==1.1.4
+jupyterlab-server==1.0.6
+keyring==18.0.0
+kiwisolver==1.1.0
+lazy-object-proxy==1.4.2
+libarchive-c==2.8
+llvmlite==0.29.0
+locket==0.2.0
+lxml==4.4.1
+MarkupSafe==1.1.1
+matplotlib==3.1.1
+mccabe==0.6.1
+menuinst==1.4.16
+mistune==0.8.4
+mkl-fft==1.0.14
+mkl-random==1.1.0
+mkl-service==2.3.0
+mock==3.0.5
+more-itertools==7.2.0
+mpmath==1.1.0
+msgpack==0.6.1
+multipledispatch==0.6.0
+mysqlclient==1.4.6
+navigator-updater==0.2.1
+nbconvert==5.6.0
+nbformat==4.4.0
+networkx==2.3
+nltk==3.4.5
+nose==1.3.7
+notebook==6.0.1
+numba==0.45.1
+numexpr==2.7.0
+numpy==1.16.5
+numpydoc==0.9.1
+olefile==0.46
+openpyxl==3.0.0
+packaging==19.2
+pandas==0.25.1
+pandocfilters==1.4.2
+parso==0.5.1
+partd==1.0.0
+path.py==12.0.1
+pathlib2==2.3.5
+patsy==0.5.1
+pep8==1.7.1
+pickleshare==0.7.5
+pilkit==2.0
+Pillow==6.2.0
+pkginfo==1.5.0.1
+pluggy==0.13.0
+ply==3.11
+prometheus-client==0.7.1
+prompt-toolkit==2.0.10
+psutil==5.6.3
+py==1.8.0
+pycodestyle==2.5.0
+pycosat==0.6.3
+pycparser==2.19
+pycrypto==2.6.1
+pycurl==7.43.0.3
+pyflakes==2.1.1
+Pygments==2.4.2
+pylint==2.4.2
+PyMySQL==0.9.3
+pyodbc==4.0.27
+pyOpenSSL==19.0.0
+pyparsing==2.4.2
+pyreadline==2.1
+pyrsistent==0.15.4
+PySocks==1.7.1
+pytest==5.2.1
+pytest-arraydiff==0.3
+pytest-astropy==0.5.0
+pytest-doctestplus==0.4.0
+pytest-openfiles==0.4.0
+pytest-remotedata==0.3.2
+python-dateutil==2.8.0
+pytz==2019.3
+PyWavelets==1.0.3
+pywin32==223
+pywinpty==0.5.5
+PyYAML==5.1.2
+pyzmq==18.1.0
+QtAwesome==0.6.0
+qtconsole==4.5.5
+QtPy==1.9.0
+requests==2.22.0
+rope==0.14.0
+ruamel-yaml==0.15.46
+scikit-image==0.15.0
+scikit-learn==0.21.3
+scipy==1.3.1
+seaborn==0.9.0
+Send2Trash==1.5.0
+simplegeneric==0.8.1
+singledispatch==3.4.0.3
+six==1.12.0
+snowballstemmer==2.0.0
+sortedcollections==1.1.2
+sortedcontainers==2.1.0
+soupsieve==1.9.3
+Sphinx==2.2.0
+sphinxcontrib-applehelp==1.0.1
+sphinxcontrib-devhelp==1.0.1
+sphinxcontrib-htmlhelp==1.0.2
+sphinxcontrib-jsmath==1.0.1
+sphinxcontrib-qthelp==1.0.2
+sphinxcontrib-serializinghtml==1.1.3
+sphinxcontrib-websupport==1.1.2
+spyder==3.3.6
+spyder-kernels==0.5.2
+SQLAlchemy==1.3.9
+sqlparse==0.3.0
+statsmodels==0.10.1
+sympy==1.4
+tables==3.5.2
+tblib==1.4.0
+terminado==0.8.2
+testpath==0.4.2
+toolz==0.10.0
+tornado==6.0.3
+tqdm==4.36.1
+traitlets==4.3.3
+unicodecsv==0.14.1
+urllib3==1.24.2
+wcwidth==0.1.7
+webencodings==0.5.1
+Werkzeug==1.0.0
+widgetsnbextension==3.5.1
+win-inet-pton==1.1.0
+win-unicode-console==0.5
+wincertstore==0.2
+wrapt==1.11.2
+xlrd==1.2.0
+XlsxWriter==1.2.1
+xlwings==0.15.10
+xlwt==1.3.0
+zict==1.0.0
+zipp==0.6.0