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
相关推荐
流形填表9 小时前
题库迁移实战:Excel中间格式与列映射
python·excel
东方芷兰9 小时前
Agent 技术摘要 06 —— Harness、原生视觉、Jev、mmproj、dsh
人工智能·笔记·python·ai·langchain·ai编程
qq_2518364579 小时前
理发管理系统 —— 会员模块
开发语言·前端·python·flask
郝学胜-神的一滴9 小时前
AI 编程智能体 03:拆解当下主流智能体能力
开发语言·c++·人工智能·python·程序人生·游戏
言乐610 小时前
Python文字审核模型
开发语言·windows·python·django·pygame
wuyk55510 小时前
Python网络爬虫入门到实战 第02章:HTTP/HTTPS协议超通俗精讲(GET/POST、请求头、响应码、爬虫核心基础)
爬虫·python·http
昔我往昔10 小时前
init文件介绍
python·flask
虫无涯11 小时前
大模型联动 CodeQL + Coverity 完整落地方案
人工智能·python·大模型·llm·codeql·coverity
ShineWinsu11 小时前
对于Redis:Hash类型的解析
c++·redis·分布式·缓存·面试·hash·哈希表
段一凡-华北理工大学11 小时前
大模型与智能体在工业的应用~系列文章12:大模型 × 数字孪生 × 智能体的融合图景
大数据·人工智能·python·深度学习·大语言模型·python开发