RODY/app/configs/global_var.py

37 lines
865 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-24 10:56:11 +08:00
2022-11-15 17:59:06 +08:00
def _init(): # 初始化
2022-11-28 17:08:12 +08:00
dict = {}
2022-11-28 17:49:48 +08:00
redis_client.__setattr__("_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 = {}
dict[key] = value
2022-11-28 17:49:48 +08:00
# redis_client.get_redis().set("_global_dict", json.dumps(dict))
redis_client.__setattr__("_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:
return redis_client.get_redis().get("_global_dict")[key]
2022-11-28 17:57:54 +08:00
except Exception as e:
print(e)
2022-11-28 17:57:26 +08:00
print('读取' + key + '失败\r\n')