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

Firestore JavaScript库默认是否使用本地缓存?缓存机制咨询

Firestore JavaScript Cache Behavior: Answers to Your Questions

Great question—this is a common point of confusion with Firestore's Web SDK, so let’s break it down step by step.

Default Local Cache Usage

Yes, the Firestore JavaScript library does use a local cache by default, but it’s critical to clarify the type:

  • Out of the box, Firestore relies on an in-memory cache that only persists for the duration of your page session. Refresh the page or close the browser, and this cache is wiped completely.
  • Persistent cache (stored in IndexedDB, surviving page reloads and browser restarts) is not enabled by default—you have to explicitly call enablePersistence() to activate this.

Why fromCache Returns True Without enablePersistence()

This is totally expected behavior, even without persistent storage turned on. Here’s the breakdown:

  • The in-memory cache activates as soon as you fetch any data from Firestore. If you run the same query or fetch the same document again during the same page session, Firestore will return the cached version from memory instead of hitting the server (as long as the backend data hasn’t changed).
  • When this happens, payload.doc.metadata.fromCache will be true to signal the data came from the local memory cache, not the server.
  • If you want to force a fresh server fetch (bypassing the memory cache), use the source option in your get call:
    db.collection("your-collection").doc("your-doc").get({ source: "server" })
    
    This will always pull the latest backend data, and fromCache will be false for that snapshot.

Documentation on Firestore's Cache Mechanism

Firestore’s official docs have a dedicated section covering cache behavior that explains these details thoroughly. Key topics include:

  • The core differences between in-memory cache and persistent cache
  • How to control data retrieval sources (server vs. cache)
  • The meaning of metadata fields like fromCache and hasPendingWrites
  • Limitations and default behaviors for each cache type

Look for sections focused on "Offline Data" and "Reading Data" in the Firestore Web SDK documentation to get the full, detailed picture.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:46:32