Open opack opened 5 months ago
In the following code, the SDK is being initialized with memory local cache using eager garbage collection.
const db = initializeFirestore(app, { localCache: memoryLocalCache() })
Memory local cache means cached documents are being stored in memory rather than on disk (aka persistent local cache). Eager garbage collection is the default garbage collection for memory local cache, which means that documents will be cleared from the cache when the SDK no longer has an open query for them. Or in other words, with eager garbage collection, the document will be removed from cache immediately after a call to getDoc(...)
, because the query is complete. Alternatively, the document would be held in cache while there is an active onSnapshot(...)
listener that queries for the doc.
To change the garbage collection behavior for memoryLocalCache, you can pass settings. It should look like this.
const db = initializeFirestore(app, { localCache: memoryLocalCache({garbageCollector: memoryLruGarbageCollector()}) })
The LRU garbage collector will cause documents to stay in cache until the cache fills to the storage limit (default 40MB), even if there is not an active snapshot listener. Because it's memory persistence, the cache will still clear when the application reloads.
Thanks for the clarification! I do not have access to my project right now, so I'll have to wait until Tuesday to check this out, but I'll definitely try this! Is there a doc that I missed where this behavior is explained? Maybe it contains more info that I should read on this matter 😉
The relevant API reference docs are memoryLocalCache and memoryLruGarbageCollector.
The ability to configure LRU garbage collection for memory persistence is relatively new, so I'm going to look into updating this page about Accessing data offline.
Many thanks for the solution and explanations! 🙏
I don't know if you'd rather leave this issue opened to keep track of this doc update, so I won't close it, but feel free to close it if you will 😉
Operating System
Windows 11
Browser Version
N/A
Firebase SDK Version
10.7.2
Firebase SDK Product:
Firestore
Describe your project's tooling
Vitest 1.2.2, Firebase Emulator
Describe the problem
Hi! The
getDoc
method always try to retrieve up-to-date data from the server, and read the cache only if offline. I need a method that tries first to retrieve the data from cache. So I created a method namedgetDocPreferablyFromCache
to try the cache and fallback to server if the cache does not contain the data. When trying to test this method, I run into a strange problem when running the test with Vitest: cache seems to never be popuplated... However, it seems to work fine in the browser.The doc is not clear about when the cache is filled with data, but I expect it happens when I issue a
getDoc
orgetDocFromServer
. But it does not seem to work that way.Am I missing something or is there a bug?
Steps and code to reproduce issue
firestore.rules
A test reproducing the issue:
The output in the console:
I thought that maybe the cached was being filled only when the data was accessed often (as some sentence suggested that in the doc) so I put a loop aroud the
getDoc
, but even with 1000 operations, it does not change anything. I also went into the source code to try to understand how the SDK works, but I did not manage to see where the cache was written (I only found cache reads).