RODY/app/configs/global_var.py

37 lines
878 B
Python
Raw Normal View History

2022-11-15 17:59:06 +08:00
"""
@Time 2022/11/15 10:13
@Auth
@File global_var.py
@IDE PyCharm
@MottoABC(Always Be Coding)
@Desc
"""
2022-11-28 17:08:12 +08:00
import json
from app.utils.redis_config import redis_client
2022-11-29 16:12:13 +08:00
redis_client.init_redis_connect()
2022-11-15 17:59:06 +08:00
def _init(): # 初始化
2022-11-28 17:08:12 +08:00
dict = {}
2022-11-29 16:12:13 +08:00
redis_client.get_redis().set("_global_dict", json.dumps(dict))
2022-11-24 10:45:05 +08:00
2022-11-15 17:59:06 +08:00
def set_value(key, value):
# 定义一个全局变量
2022-11-28 17:08:12 +08:00
dict = redis_client.get_redis().get("_global_dict")
if dict is None:
dict = {}
2022-11-29 16:12:13 +08:00
else:
dict = json.loads(dict)
2022-11-28 17:08:12 +08:00
dict[key] = value
2022-11-29 16:12:13 +08:00
redis_client.get_redis().set("_global_dict", json.dumps(dict))
2022-11-15 17:59:06 +08:00
def get_value(key):
# 获得一个全局变量,不存在则提示读取对应变量失败
2022-11-28 17:57:26 +08:00
try:
2022-11-29 16:12:13 +08:00
dict = redis_client.get_redis().get("_global_dict")
return json.loads(dict)[key]
except:
2022-11-28 17:57:26 +08:00
print('读取' + key + '失败\r\n')