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
相关推荐
大翻哥哥10 小时前
Python 2025:异步革命与AI驱动下的开发新范式
开发语言·人工智能·python
hhzz10 小时前
Pythoner 的Flask项目实践-在web页面实现矢量数据转换工具集功能(附源码)
前端·python·flask
学习的学习者10 小时前
CS课程项目设计19:基于DeepFace人脸识别库的课堂签到系统
人工智能·python·深度学习·人脸识别算法
Terio_my10 小时前
Spring Boot 缓存集成实践
spring boot·后端·缓存
悠哉悠哉愿意11 小时前
【数据结构与算法学习笔记】双指针
数据结构·笔记·python·学习·算法
MoRanzhi120311 小时前
5. Pandas 缺失值与异常值处理
数据结构·python·数据挖掘·数据分析·pandas·缺失值处理·异常值处理
程序员的奶茶馆12 小时前
Python 字典速查:键值对操作与高频函数
python·面试
tryCbest12 小时前
Python 使用 Redis 详细教程
redis·python·bootstrap
小小毛毛虫~12 小时前
使用Cursor遇到的问题(一):cursor使用conda虚拟环境
python·conda·cursor
Savvy..13 小时前
Redis 黑马点评-商户查询缓存
数据库·redis·缓存