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
相关推荐
斯普信专业组30 分钟前
Redis RESP3 协议规范:从入门到实战
数据库·redis·缓存
云和数据.ChenGuang31 分钟前
Python 集合(set)常用函数详解与实战案例
开发语言·python·深度学习·langchain
梯度下降者40 分钟前
python在哪些方面有应用,python可以用在哪些方面
人工智能·python·web开发·科学计算·应用领域
SHENGWEI胜为1 小时前
服务器 IP 地址冲突如何排查?从 DHCP 租约到 ARP 缓存定位故障
服务器·tcp/ip·缓存
烂蜻蜓1 小时前
Pandas入门教程(十二):读取 HTML 表格——read_html 参数详解与网页数据清洗
python·html·pandas
2601_967979121 小时前
Opteryx Python库 macOS版本0.0.0a12
python·macos·1024程序员节·wheel·opteryx·alpha
Hello_Pyhx1 小时前
SCSKiller v1.3.2 使用流程:从着色器预编译到检查缓存状态
缓存·着色器
Yolanda_20221 小时前
PyTorch 线性层nn.Linear底层数学原理
人工智能·pytorch·python
软件技术NINI1 小时前
基于JavaWeb 汽车租聘
java·vue.js·spring boot·后端·python·毕业设计·毕设
quantdash_cc2 小时前
如何从多只股票的历史行情构建收益率矩阵?Python 与 QuantDash 实战教程
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash