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
相关推荐
程序员爱钓鱼2 小时前
Python编程实战 · 基础入门篇 | 元组(tuple)
后端·python·ipython
程序员爱钓鱼2 小时前
Python编程实战 · 基础入门篇 | 列表(list)
后端·python·ipython
御承扬4 小时前
编程素养提升之EffectivePython(Builder篇)
python·设计模式·1024程序员节
chenchihwen5 小时前
AI代码开发宝库系列:FAISS向量数据库
数据库·人工智能·python·faiss·1024程序员节
AI视觉网奇6 小时前
json 可视化 2025 coco json
python·1024程序员节
mit6.8246 小时前
[nanoGPT] ChatGPT 的 LLM 的全栈实现 | 快速上手
python
DKunYu6 小时前
2.1线性回归
pytorch·python·深度学习·1024程序员节
大飞记Python7 小时前
实战分享:一键自动化下载指定版本的Chrome及Chromedriver(附Python源码)
chrome·python·自动化
程序员杰哥8 小时前
如何使用Postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·python·测试工具·jenkins·postman·1024程序员节
老歌老听老掉牙8 小时前
参数曲线切向量与叉乘向量的精确计算与分析
python·sympy·1024程序员节