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
相关推荐
曲幽3 小时前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
今天也是元气满满的一天呢3 小时前
计算机底层存储、CPU 运行与程序执行原理详解
缓存
装不满的克莱因瓶4 小时前
掌握 RNN 与 LSTM 模型结构
人工智能·python·rnn·深度学习·神经网络·ai·lstm
何以解忧,唯有..4 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
金銀銅鐵4 小时前
用 Tkinter 实现简单的猜数字游戏
后端·python
copyer_xyf4 小时前
Python 模块与包的导入导出
前端·后端·python
ice8130331815 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
copyer_xyf5 小时前
Python venv 虚拟环境
前端·后端·python
basketball6165 小时前
Redis基础:1. Redis介绍
数据库·redis·缓存
林爷万福6 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪