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
相关产品推荐
相关产品推荐

