优化dispatch,兼容分布式redis
parent
7d01d3ce8a
commit
4b35eb6d3c
|
@ -2,6 +2,9 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
|
dispatch_db_type = getattr(settings, 'DISPATCH_DB_TYPE', 'memory') # redis
|
||||||
|
|
||||||
|
|
||||||
def is_tenants_mode():
|
def is_tenants_mode():
|
||||||
|
@ -68,6 +71,9 @@ def init_dictionary():
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
cache.set(f"init_dictionary", _get_all_dictionary())
|
||||||
|
return
|
||||||
if is_tenants_mode():
|
if is_tenants_mode():
|
||||||
from django_tenants.utils import tenant_context, get_tenant_model
|
from django_tenants.utils import tenant_context, get_tenant_model
|
||||||
|
|
||||||
|
@ -88,7 +94,9 @@ def init_system_config():
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
cache.set(f"init_system_config", _get_all_system_config())
|
||||||
|
return
|
||||||
if is_tenants_mode():
|
if is_tenants_mode():
|
||||||
from django_tenants.utils import tenant_context, get_tenant_model
|
from django_tenants.utils import tenant_context, get_tenant_model
|
||||||
|
|
||||||
|
@ -107,6 +115,9 @@ def refresh_dictionary():
|
||||||
刷新字典配置
|
刷新字典配置
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
cache.set(f"init_dictionary", _get_all_dictionary())
|
||||||
|
return
|
||||||
if is_tenants_mode():
|
if is_tenants_mode():
|
||||||
from django_tenants.utils import tenant_context, get_tenant_model
|
from django_tenants.utils import tenant_context, get_tenant_model
|
||||||
|
|
||||||
|
@ -122,6 +133,9 @@ def refresh_system_config():
|
||||||
刷新系统配置
|
刷新系统配置
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
cache.set(f"init_system_config", _get_all_system_config())
|
||||||
|
return
|
||||||
if is_tenants_mode():
|
if is_tenants_mode():
|
||||||
from django_tenants.utils import tenant_context, get_tenant_model
|
from django_tenants.utils import tenant_context, get_tenant_model
|
||||||
|
|
||||||
|
@ -141,6 +155,11 @@ def get_dictionary_config(schema_name=None):
|
||||||
:param schema_name: 对应字典配置的租户schema_name值
|
:param schema_name: 对应字典配置的租户schema_name值
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
init_dictionary_data = cache.get(f"init_dictionary")
|
||||||
|
if not init_dictionary_data:
|
||||||
|
refresh_dictionary()
|
||||||
|
return cache.get(f"init_dictionary") or {}
|
||||||
if not settings.DICTIONARY_CONFIG:
|
if not settings.DICTIONARY_CONFIG:
|
||||||
refresh_dictionary()
|
refresh_dictionary()
|
||||||
if is_tenants_mode():
|
if is_tenants_mode():
|
||||||
|
@ -157,6 +176,12 @@ def get_dictionary_values(key, schema_name=None):
|
||||||
:param schema_name: 对应字典配置的租户schema_name值
|
:param schema_name: 对应字典配置的租户schema_name值
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
dictionary_config = cache.get(f"init_dictionary")
|
||||||
|
if not dictionary_config:
|
||||||
|
refresh_dictionary()
|
||||||
|
dictionary_config = cache.get(f"init_dictionary")
|
||||||
|
return dictionary_config.get(key)
|
||||||
dictionary_config = get_dictionary_config(schema_name)
|
dictionary_config = get_dictionary_config(schema_name)
|
||||||
return dictionary_config.get(key)
|
return dictionary_config.get(key)
|
||||||
|
|
||||||
|
@ -169,8 +194,8 @@ def get_dictionary_label(key, name, schema_name=None):
|
||||||
:param schema_name: 对应字典配置的租户schema_name值
|
:param schema_name: 对应字典配置的租户schema_name值
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
children = get_dictionary_values(key, schema_name) or []
|
res = get_dictionary_values(key, schema_name) or []
|
||||||
for ele in children:
|
for ele in res.get('children'):
|
||||||
if ele.get("value") == str(name):
|
if ele.get("value") == str(name):
|
||||||
return ele.get("label")
|
return ele.get("label")
|
||||||
return ""
|
return ""
|
||||||
|
@ -187,6 +212,11 @@ def get_system_config(schema_name=None):
|
||||||
:param schema_name: 对应字典配置的租户schema_name值
|
:param schema_name: 对应字典配置的租户schema_name值
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
init_dictionary_data = cache.get(f"init_system_config")
|
||||||
|
if not init_dictionary_data:
|
||||||
|
refresh_system_config()
|
||||||
|
return cache.get(f"init_system_config") or {}
|
||||||
if not settings.SYSTEM_CONFIG:
|
if not settings.SYSTEM_CONFIG:
|
||||||
refresh_system_config()
|
refresh_system_config()
|
||||||
if is_tenants_mode():
|
if is_tenants_mode():
|
||||||
|
@ -203,6 +233,12 @@ def get_system_config_values(key, schema_name=None):
|
||||||
:param schema_name: 对应系统配置的租户schema_name值
|
:param schema_name: 对应系统配置的租户schema_name值
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if dispatch_db_type == 'redis':
|
||||||
|
system_config = cache.get(f"init_system_config")
|
||||||
|
if not system_config:
|
||||||
|
refresh_system_config()
|
||||||
|
system_config = cache.get(f"init_system_config")
|
||||||
|
return system_config.get(key)
|
||||||
system_config = get_system_config(schema_name)
|
system_config = get_system_config(schema_name)
|
||||||
return system_config.get(key)
|
return system_config.get(key)
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,6 @@ LOGIN_NO_CAPTCHA_AUTH = True
|
||||||
# ================================================= #
|
# ================================================= #
|
||||||
|
|
||||||
ALLOWED_HOSTS = ["*"]
|
ALLOWED_HOSTS = ["*"]
|
||||||
|
# 系统配置存放位置:redis/memory(默认)
|
||||||
|
DISPATCH_DB_TYPE = 'redis'
|
||||||
|
|
||||||
# daphne启动命令
|
|
||||||
#daphne application.asgi:application -b 0.0.0.0 -p 8000
|
|
||||||
|
|
Loading…
Reference in New Issue