我正在將 Firestore DB 用于電子商務應用程式。我有一個產品集合,每個產品都有一個包含“標題”欄位和“search_keywords”欄位的檔案。搜索關鍵字欄位存盤一個陣列。例如,如果title="apple",那么"search_keywords" 欄位將存盤以下陣列:["a","ap","app","appl","apple"]。當用戶開始在搜索框中輸入“apple”時,我想向用戶顯示“search_keywords”包含“a”的所有產品,然后當他們輸入“p”時,我想顯示搜索關鍵字包含的所有產品“ap”……等等。這是每次輸入額外字母時呼叫的代碼片段:
firebaseFireStore.collection("Produce").whereArrayContains("search_keywords", toSearch).get()
例如,在每種情況下,在每次輸入附加字母的連續呼叫中回傳的檔案將是前一次呼叫中回傳的內容的子集——它只是一個較小的檔案串列——已讀取的檔案在上一個查詢中。我的問題是,由于在連續查詢中檢索到的檔案是在先前查詢中檢索到的檔案的子集,我是否會根據每個連續查詢回傳的檔案數量來收取讀取費用,還是 Firestore 將它們放在快取中并從那里讀取它們因為后續結果集是先前結果集的子集。這個問題在我的腦海里已經有一段時間了,每次我搜索它時,我似乎都找不到明確的答案。例如,根據我的研究,Stackoverflow 上的以下兩篇文章涉及類似的問題,以下是那里的相關參考,但它們似乎相互矛盾,因為@AlexMamo 說“它總是會閱讀檔案的在線版本......[在線時]”和@Doug Stevenson 說“如果您的客戶端上啟用了本地持久性(默認情況下)并且檔案尚未在服務器中更新......[它將從快取中獲取它們]”。如果有人知道答案,我將不勝感激。謝謝。
“如果 OP 啟用了離線持久性,這是 Cloud Firestore 中的默認設定,那么他將只能在離線時讀取快取。當 OP 具??有互聯網連接時,它將始終讀取檔案的在線版本。” – Alex Mamo ( https://stackoverflow.com/a/69320068/14556386 )
“根據道格史蒂文森的這個答案,讀取只在服務器上執行時收費,而不是本地快取。也就是說,如果您的客戶端上啟用了本地持久性(默認情況下)并且檔案尚未更新在服務器中。” ( https://stackoverflow.com/a/61381656/14556386 )
編輯:此外,如果對于 Firestore 搜索檢索到的每個產品檔案,我從 Firebase Storage 下載其相應的影像檔案。是否會因連續嘗試下載該檔案而向我收費,還是會識別出我以前下載過該影像并自動從快取中獲取它?
uj5u.com熱心網友回復:
首先,將 ["a", "ap", "app", "appl", "apple"] 存盤到陣列中并執行 whereArrayContains() 查詢,這聽起來不是一個可行的想法。為什么?想象一下,你有一個非常大的網上商店,有 100k 種產品,其中 5k 以“a”開頭。每次用戶鍵入“a”時,您是否愿意支付 5k 次讀取費用?這是一個非常昂貴的功能。
很可能您應該在用戶鍵入時回傳相應的檔案,例如,兩個甚至三個字符。您將大大降低成本。或者您可以考慮使用我在以下文章中解釋的解決方案:
- 如何更便宜地過濾 Firestore 資料?
我們前進吧。
例如,在每種情況下,在每次輸入附加字母的連續呼叫中回傳的檔案將是前一次呼叫中回傳內容的子集,它只是一個較小的檔案串列。
對,那是正確的。
我的問題是,由于在連續查詢中檢索到的檔案是在先前查詢中檢索到的檔案的子集,我是否會根據每個連續查詢回傳的檔案數量來收取讀取費用?
是的。您將始終需要支付等于查詢回傳的檔案數量的讀取次數。先前是否執行過查詢并不重要。每次執行新查詢時,都會向您收取與您獲得的檔案數量相等的讀取次數。
例如,假設您執行以下查詢:
.whereArrayContains("search_keywords", "a")
然后你得到100 個檔案,然后你執行:
.whereArrayContains("search_keywords", "ap")
而你只有30個檔案,你就必須支付130讀取,并且不只是100所以不要緊,如果由第二查詢回傳的檔案是由第一個查詢回傳的檔案的子集.
或者 Firestore 會將它們放在快取中并從那里讀取它們,因為連續結果集是先前結果集的子集。
不,不會。只有當用戶失去互聯網連接時,它才會從快取中讀取這些檔案,否則它將始終讀取 Firebase 服務器上存在的檔案的在線版本。檔案的快取版本僅在用戶離線時有效。我還寫了一篇關于這個主題的文章,名為:
- 在 Firestore 中未更改任何檔案時,如何大幅減少讀取次數?
在道格的回答中:
- 每次更改位置時,我都要負責讀取操作嗎?
他明確地說:
您需要為每次呼叫 get() 時在服務器上讀取的檔案數付費。
因此,如果您呼叫 get(),則必須按讀取次數付費,即回傳的檔案數。
以下陳述句可用:
如果在您的客戶端中啟用了本地持久性(默認情況下),那么如果檔案在服務器上也沒有更改,則檔案可能來自快取。
當您正在收聽實時更新時。根據檔案:
When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed.
And I would add, if nothing has changed, you don't have to pay anything. Again, according to the same docs:
Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.
So if the listener is active, you always read the documents from the cache. Bear in mind that a get() operation is different than listening for real-time updates.
if for each product document that was retrieved by the Firestore search, I download its corresponding image file from Firebase Storage. Would it charge me for downloading that file on successive attempts to download it or would it recognize that I had previously downloaded that image and fetch it from cache automatically?
You'll always be charged if you download the image over and over again unless you are using a library that helps you cache the images. For Android, there is a library called Glide:
Glide is a fast and efficient open-source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy-to-use interface.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/335943.html
