Skip to content

Commit

Permalink
移除配置信息的缓存依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyo committed Oct 31, 2020
1 parent 8aca672 commit 7fd62b2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 54 deletions.
71 changes: 23 additions & 48 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from common.utils.permission import superuser_required
from sql.models import Config
from django.db import transaction
from django.core.cache import cache

logger = logging.getLogger('default')

Expand All @@ -19,37 +18,20 @@ def __init__(self):
self.get_all_config()

def get_all_config(self):
# 优先获取缓存数据
try:
sys_config = cache.get('sys_config')
except Exception as m:
sys_config = None
logger.error(f"读取缓存失败:{m}{traceback.format_exc()}")

# 缓存获取失败从数据库获取并且尝试更新缓存
if sys_config:
# 获取系统配置信息
all_config = Config.objects.all().values('item', 'value')
sys_config = {}
for items in all_config:
if items['value'] in ('true', 'True'):
items['value'] = True
elif items['value'] in ('false', 'False'):
items['value'] = False
sys_config[items['item']] = items['value']
self.sys_config = sys_config
else:
try:
# 获取系统配置信息
all_config = Config.objects.all().values('item', 'value')
sys_config = {}
for items in all_config:
if items['value'] in ('true', 'True'):
items['value'] = True
elif items['value'] in ('false', 'False'):
items['value'] = False
sys_config[items['item']] = items['value']
self.sys_config = sys_config
except Exception as m:
logger.error(f"获取系统配置信息失败:{m}{traceback.format_exc()}")
self.sys_config = {}
else:
try:
# 更新缓存
cache.set('sys_config', self.sys_config, timeout=None)
except Exception as m:
logger.error(f"更新缓存失败:{m}{traceback.format_exc()}")
except Exception as m:
logger.error(f"获取系统配置信息失败:{m}{traceback.format_exc()}")
self.sys_config = {}

def get(self, key, default_value=None):
value = self.sys_config.get(key, default_value)
Expand All @@ -60,27 +42,21 @@ def get(self, key, default_value=None):

def set(self, key, value):
if value is True:
value = 'true'
db_value = 'true'
elif value is False:
value = 'false'
config_item, created = Config.objects.get_or_create(item=key)
config_item.value = value
config_item.save()
# 删除并更新缓存
try:
cache.delete('sys_config')
except Exception as m:
logger.error(f"删除缓存失败:{m}{traceback.format_exc()}")
finally:
self.get_all_config()
db_value = 'false'
else:
db_value = value
obj, created = Config.objects.update_or_create(item=key, defaults={"value": db_value})
if created:
self.sys_config.update({key: value})

def replace(self, configs):
result = {'status': 0, 'msg': 'ok', 'data': []}

# 清空并替换
try:
self.purge()
with transaction.atomic():
self.purge()
Config.objects.bulk_create(
[Config(item=items['key'].strip(),
value=str(items['value']).strip()) for items in json.loads(configs)])
Expand All @@ -95,12 +71,11 @@ def replace(self, configs):
def purge(self):
"""清除所有配置, 供测试以及replace方法使用"""
try:
self.sys_config = {}
cache.delete('sys_config')
with transaction.atomic():
Config.objects.all().delete()
self.sys_config = {}
except Exception as m:
logger.error(f"删除缓存失败:{m}{traceback.format_exc()}")
with transaction.atomic():
Config.objects.all().delete()


# 修改系统配置
Expand Down
10 changes: 5 additions & 5 deletions common/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ <h4 style="color: darkgrey"><b>其他配置</b></h4>
</div>
</div>
<div class="form-group">
<label for="lock_time_threshold"
<label for="binlog2sql"
class="col-sm-4 control-label">BINLOG2SQL</label>
<div class="col-sm-5">
<input type="text" class="form-control"
Expand Down Expand Up @@ -953,7 +953,7 @@ <h5 class="control-label text-bold">当前审批流程:<b id="workflow_auditor
$('#group_auditors').selectpicker('val', []);
$(this).children().removeAttr("disabled");
$(this).selectpicker("refresh");
})
});

// 清空审核人信息
$("#btn-workflow-config-clean").click(function () {
Expand Down Expand Up @@ -1052,7 +1052,7 @@ <h5 class="control-label text-bold">当前审批流程:<b id="workflow_auditor
},
success: function (data) {
if (data.status === 0) {
alert('Inception和备份库测试连接成功,请点击保存生效!')
alert('Inception和备份库测试连接成功,请点击页面下方的保存按钮生效!')
} else {
alert(data.msg);
}
Expand Down Expand Up @@ -1085,7 +1085,7 @@ <h5 class="control-label text-bold">当前审批流程:<b id="workflow_auditor
},
success: function (data) {
if (data.status === 0) {
alert('goInception和备份库测试连接成功,请点击保存生效!')
alert('goInception和备份库测试连接成功,请点击页面下方的保存按钮生效!')
} else {
alert(data.msg);
}
Expand Down Expand Up @@ -1118,7 +1118,7 @@ <h5 class="control-label text-bold">当前审批流程:<b id="workflow_auditor
},
success: function (data) {
if (data.status === 0) {
alert('测试邮件已成功发送给当前用户,注意查收,请点击保存生效!')
alert('测试邮件已成功发送给当前用户,注意查收,请点击页面下方的保存按钮生效!')
} else {
alert(data.msg);
}
Expand Down
2 changes: 1 addition & 1 deletion sql/templates/binlog2sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ <h5 class="control-label text-bold">对象过滤:</h5>
<h5 class="control-label text-bold" style="color: red">
<b>前端展示数量由解析范围参数控制如果勾选了保存到文件则会异步获取完整的SQL文件
保存到项目downloads目录起始时间过滤存在性能问题建议使用position进行过滤
在开启邮件通知后执行结束会通知给操作人</b>
在开启消息通知后执行结束会发送通知给操作人</b>
</h5>
<br>
<table id="tb-binlog2sql" data-toggle="table" class="table table-condensed"
Expand Down

0 comments on commit 7fd62b2

Please sign in to comment.