You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore缓存与读取计费问题:Flutter查询刷新场景解析

Firestore查询读取费用与缓存相关问题解答
Future<QuerySnapshot<Object?>> getPendingDiscoveryChats(
      String userId, DocumentSnapshot? lastDocument) async {
    QuerySnapshot result;
    if (lastDocument == null) {
      result = await FirebaseFirestore.instance
          .collection('discoveryChats')
          .where('senderId', isEqualTo: userId)
          .where('pending', isEqualTo: true)
          .orderBy('lastMessageSentDateTime', descending: true)
          .limit(10)
          .get();
    } else {
      result = await FirebaseFirestore.instance
          .collection('discoveryChats')
          .where('senderId', isEqualTo: userId)
          .where('pending', isEqualTo: true)
          .orderBy('lastMessageSentDateTime', descending: true)
          .startAfterDocument(lastDocument)
          .limit(10)
          .get();
    }
    return result;
  }

问题1:当已加载10个文档后新增1个文档,刷新时查询返回1个新文档和9个未变更文档,这9个未变更文档是来自缓存还是会产生读取费用?

  • 这9个未变更文档会产生读取费用。Firestore的get()方法默认从服务器拉取最新数据,即便本地缓存有这些文档的副本,只要没显式指定从缓存读取(比如get(GetOptions(source: Source.cache))),Firestore就会向服务器发起请求验证文档是否更新。只要服务器返回了这些文档内容(哪怕无变化),就会按读取次数收费。

问题2:当无任何变更时触发刷新,Firestore是否会收取10次读取费用以确认结果未发生变化?

  • 是的,会收取10次读取费用。get()请求会触发服务器端执行查询,服务器需要确认匹配的10个文档均无变更后返回结果(哪怕内容和缓存一致)。每一个被查询匹配到的文档,都会被计为一次读取操作,因此无变更时刷新也会产生10次读取费用。

内容的提问来源于stack exchange,提问作者Ruder Buster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 03:31:02