diff --git a/config/urls.py b/config/urls.py index 9d1f69d..232d130 100644 --- a/config/urls.py +++ b/config/urls.py @@ -19,5 +19,6 @@ from django.urls import path, include from config.views import * urlpatterns = [ - path('zy_switch_svip', zy_switch_svip) + path('zy_switch_svip', zy_switch_svip), + path('zy_clear_cache', zy_clear_cache) ] diff --git a/config/views.py b/config/views.py index c81443c..c49346c 100644 --- a/config/views.py +++ b/config/views.py @@ -2,6 +2,8 @@ from django.http import JsonResponse from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt import pymysql +from pymysql.cursors import DictCursor +import redis zy_db_config = { 'host': '172.16.3.223', @@ -11,14 +13,21 @@ zy_db_config = { 'db': 'suyi_cinema', } +zy_redis_config = { + 'host': '172.16.3.223', + 'port': 6379, + 'db': 0, + 'decode_responses': True +} + @csrf_exempt def zy_switch_svip(request): """ 接口用于切换自营环境 http://172.16.1.168:8000/config/zy_switch_svip?svip=on - """ - # 通过Api model获取会员或非会员的api id + """ + # 切换自营环境 svip = request.GET.get('svip') if svip == 'on': result = zy_switch_svip_db('svip') @@ -27,25 +36,62 @@ def zy_switch_svip(request): else: result = False + # 清除缓存 + clear_result = False + if result: + clear_result = zy_clear_cache_redis() + + result_dict = { + 'status': 'success' if clear_result else 'error', + 'message': ('切换环境成功,' if result else '切换环境失败,') + ('清除缓存成功' if clear_result else '清除缓存失败'), + } + return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) + + +@csrf_exempt +def zy_clear_cache(request): + """ + 接口用于清除自营缓存 + http://172.16.1.168:8000/config/zy_clear_cache + """ + result = zy_clear_cache_redis() result_dict = { 'status': 'success' if result else 'error', - 'message': '切换环境成功' if result else '切换环境失败', + 'message': '清除缓存成功' if result else '清除缓存失败', } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) def zy_switch_svip_db(env_type): db_conn = pymysql.Connect(**zy_db_config) - cursor = db_conn.cursor() + cursor = db_conn.cursor(cursor=DictCursor) + + select_sql = "SELECT value FROM suyi_cinema.group_kv_config gkc WHERE gkc.key = 'svip_switch';" + cursor.execute(select_sql) + query_result = cursor.fetchone() + # value 0-非华谊 1-华谊 sql_str = "UPDATE suyi_cinema.group_kv_config gkc SET gkc.value = %s WHERE gkc.key = 'svip_switch';" if env_type == 'svip': - r = cursor.execute(sql_str, ('1',)) + if query_result['value'] == '1': + r = 1 + else: + r = cursor.execute(sql_str, ('1',)) + print('zy_switch_svip_db', r) else: - r = cursor.execute(sql_str, ('0',)) + if query_result['value'] == '1': + r = cursor.execute(sql_str, ('0',)) + print('zy_switch_svip_db', r) + else: + r = 1 db_conn.commit() db_conn.close() - print(r) - return True if r==1 else False + return True if r == 1 else False + +def zy_clear_cache_redis(): + r = redis.Redis(**zy_redis_config) + result = r.flushdb() + print('zy_clear_cache_redis', result) + return result # Create your views here.