首页 > 解决方案 > 为 firebase-admin sdk 缓存 firestore 数据的最佳方法是什么?

问题描述

正如我可以找到的https://github.com/firebase/firebase-admin-python不允许enable offline data模式。即使它有,我认为它也不足以满足我的用例。由于我知道我的数据库每天更新一次但读取多次,因此我希望始终从缓存中读取,除非缓存的数据超过 24 小时。

这就是我现在正在做的

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("firebase_admin_json_name.json")
firebase_admin.initialize_app(cred)
fdb = firestore.client()
source_doc_snapshots = (
                    fdb.collection("users")
                    .document(user)
                    .collection("sources")
                    .stream()
                )

我正在寻找这样的东西(请为我的伪​​代码建议合适的代码)

if source_doc_snapshots in my cache > 24 hours old:
    source_doc_snapshots = (
                        fdb.collection("users")
                        .document(user)
                        .collection("sources")
                        .stream()
                    )
    save source_doc_snapshots to my cache
else:
    read source_doc_snapshots from my cache

标签: pythonfirebasegoogle-cloud-platformgoogle-cloud-firestorefirebase-admin

解决方案


推荐阅读