即使没有数据更改,Firestore缓存也不起作用

时间:2019-12-23 12:02:15

标签: firebase flutter dart google-cloud-firestore

根据Alex Momo's answer,默认情况下,Firestore将文档存储在缓存中并从那里加载,只有新文档或更改后,Firestore才会重新读取,并收取额外费用。

我正在使用StreamBuilder和FutureBuilder进行测试,并使用打印来显示数据的来源。

打印(document.metadata.isFromCache?“不是网络”:“从网络”);

即使我使用FutureBuilder(.getDocuments()),第一次打开应用程序时,它也会打印“ FROM NETWORK”,第二次它再次加载“ FROM NETWORK”,并且数据库没有更改,因此看起来缓存仅在应用程序真正脱机时才起作用,但是似乎如果始终有连接会从服务器加载,那么有人能对此进行解释吗?

我认为参数 Source.serverAndCache 是默认设置,说明中说“ 使Firestore尝试检索最新(服务器检索)的快照,但回退如果无法访问服务器,则返回缓存的数据。”,我认为这意味着:您有连接吗?从服务器加载,如果没有?从缓存加载。但这并没有多大帮助,因为每次我们关闭应用程序并重新打开时,都会再次从服务器加载所有文档。

 [2019-12-23T16:55:24,887][ERROR][logstash.agent           ] Failed to execute action {:action=>LogStash::PipelineAction::Create/pipeline_id:main, :exception=>"LogStash::ConfigurationError", :message=>"Expected one of #, => at line 14, column 25 (byte 243) after output {\r\nelasticsearch{\r\nhosts=>{\"localhost:9200\"", :backtrace=>["D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/compiler.rb:41:in `compile_imperative'", "D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/compiler.rb:49:in `compile_graph'", "D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/compiler.rb:11:in `block in compile_sources'", "org/jruby/RubyArray.java:2584:in `map'", "D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/compiler.rb:10:in `compile_sources'", "org/logstash/execution/AbstractPipelineExt.java:153:in `initialize'", "org/logstash/execution/JavaBasePipelineExt.java:47:in `initialize'", "D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/java_pipeline.rb:26:in `initialize'", "D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/pipeline_action/create.rb:36:in `execute'", "D:/elasticinstall/logstash-7.4.0/logstash-core/lib/logstash/agent.rb:326:in `block in converge_state'"]}
    [2019-12-23T16:55:25,982][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}
    [2019-12-23T16:55:30,292][INFO ][logstash.runner          ] Logstash shut down.


    logstash code
    input {
      http_poller {
        urls => {
         urlname =>"http://.."
         }
        }
        request_timeout => 60
        schedule => {every=>"20s"}
        codec => "line"
      }
    }


output {
elasticsearch{
hosts=>{"localhost:9200"}
index =>"logstash_http_poller"
}
  stdout {
    codec => rubydebug
  }
}

1 个答案:

答案 0 :(得分:0)

彼得的评论是正确的。每当您调用.getDocuments(source: Source.serverAndCache )时,Firestore都需要检查服务器以查看数据是否被修改。并且由于这需要服务器读取文档,因此将向您收取这些费用。

如果要强制Firestore从其本地缓存中读取,请使用.getDocuments(source: Source.Cache)。这意味着您冒着显示陈旧数据的风险,但这是您必须权衡的。

我通常建议使用onSnapshot侦听器,因为它们更适合仅将更改从服务器发送到客户端。

还可以检出: