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
相关推荐
小鸡吃米…20 小时前
Python PyQt6教程七-控件
数据库·python
忍冬行者20 小时前
清理三主三从redis集群的过期key和键值超过10M的key
数据库·redis·缓存
TimberWill20 小时前
使用Redis队列优化内存队列
数据库·redis·缓存
1916zz20 小时前
Extreme programing 方利喆 _ 江贤晟
python
长安牧笛20 小时前
智能鞋柜—脚气终结者,内置温湿度传感器和紫外线灯,晚上回家,把鞋放进去,自动检测湿度,湿度超标就启动烘干+紫外线杀菌,第二天穿鞋干燥无异味。
python
weixin_4577600021 小时前
PIL库将图片位深度是1、8、32统一转换为24的方法
python
Lucky高1 天前
Pandas库入门
python·pandas
小鸡吃米…1 天前
Python PyQt6教程三-菜单与工具栏
开发语言·python
Jack电子实验室1 天前
【杭电HDU】校园网(DeepL/Srun)自动登录教程
python·嵌入式硬件·计算机网络·自动化
木头左1 天前
二值化近似计算在量化交易策略中降低遗忘门运算复杂度
python