# -*- coding: utf-8 -*- """ 性能优化配置 从 system_settings.json 读取,硬编码值仅作为 fallback """ import os import json import logging logger = logging.getLogger(__name__) # 默认值 _DEFAULTS = { "database": {"pool_size": 20, "max_overflow": 30, "pool_recycle": 1800, "pool_timeout": 10}, "cache": {"default_ttl": 60, "max_memory_size": 2000, "conversation_ttl": 60, "workorder_ttl": 30, "monitoring_ttl": 30}, "query": {"default_limit": 100, "conversations_limit": 1000, "workorders_limit": 100, "monitoring_limit": 1000}, "api": {"timeout": 10, "retry_count": 3, "batch_size": 50}, "monitoring": {"interval": 60, "slow_query_threshold": 1.0, "performance_log_enabled": True} } def _load(): try: path = os.path.join('data', 'system_settings.json') if os.path.exists(path): with open(path, 'r', encoding='utf-8') as f: return json.load(f).get('performance', {}) except Exception as e: logger.debug(f"加载性能配置失败: {e}") return {} class PerformanceConfig: """性能配置类 — 优先从配置文件读取""" @classmethod def _get(cls, section, key): cfg = _load() return cfg.get(section, {}).get(key, _DEFAULTS.get(section, {}).get(key)) @classmethod def get_database_config(cls): cfg = _load().get('database', {}) return {**_DEFAULTS['database'], **cfg} @classmethod def get_cache_config(cls): cfg = _load().get('cache', {}) return {**_DEFAULTS['cache'], **cfg} @classmethod def get_query_config(cls): cfg = _load().get('query', {}) return {**_DEFAULTS['query'], **cfg} @classmethod def get_api_config(cls): cfg = _load().get('api', {}) return {**_DEFAULTS['api'], **cfg} # 向后兼容的类属性(读取时动态获取) DATABASE_POOL_SIZE = property(lambda self: self._get('database', 'pool_size')) CACHE_DEFAULT_TTL = property(lambda self: self._get('cache', 'default_ttl')) CACHE_MAX_MEMORY_SIZE = property(lambda self: self._get('cache', 'max_memory_size')) QUERY_LIMIT_DEFAULT = property(lambda self: self._get('query', 'default_limit')) API_TIMEOUT = property(lambda self: self._get('api', 'timeout')) MONITORING_INTERVAL = property(lambda self: self._get('monitoring', 'interval'))