python动态全局缓存配置

在内存中缓存配置,但提供手动或自动刷新机制。

使用文件的修改时间戳(mtime)来判断文件是否更新,只有在文件更新时重新读取

python 复制代码
import os
import json

_cached_config = None
_cached_config_mtime = None

def read_config():
    global _cached_config, _cached_config_mtime
    config_file = os.path.expanduser('~/.magic-pdf.json')

    if not os.path.exists(config_file):
        raise FileNotFoundError(f'{config_file} not found')

    mtime = os.path.getmtime(config_file)
    if _cached_config is None or _cached_config_mtime != mtime:
        with open(config_file, 'r', encoding='utf-8') as f:
            _cached_config = json.load(f)
            _cached_config_mtime = mtime

    return _cached_config
相关推荐
郝学胜-神的一滴几秒前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
火白学安全28 分钟前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全
FreeCode38 分钟前
LangGraph1.0智能体开发:运行时系统
python·langchain·agent
青瓷程序设计1 小时前
植物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
q***31831 小时前
Window下Redis的安装和部署详细图文教程(Redis的安装和可视化工具的使用)
数据库·redis·缓存
习习.y1 小时前
关于python中的面向对象
开发语言·python
hmbbcsm1 小时前
练习python题目小记(六)
开发语言·python
wow_DG2 小时前
【Python✨】VS Code 秒开 Python 类型检查:一招 mypy + settings.json 让你的 Bug 原地现形!
python·json·bug
Aspect of twilight2 小时前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
空影星2 小时前
高效追踪电脑使用时间,Tockler助你优化时间管理
python·django·flask