Firebase是否缓存数据? [英] Does Firebase cache the data?

查看:120
本文介绍了Firebase是否缓存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一些关于Firebase缓存数据的说法。

所以我跑了这个测试读取了大量的数据(大约400KB)。

以下是相关的代码。

  firebase.initializeApp(config); 

var counter = 0;

console.time('firebase回答');
firebase.database()。ref('texts')。once('value',onData);

函数onData(快照){
console.timeEnd('firebase回答');

counter ++;
if(counter> 20)return;
$ b $ setTimeout(function(){
console.time('firebase回答');
firebase.database()。ref('texts')。once('value ',onData);
},2000);





$ b

正如你所看到的,它第一次加载数据需要一段时间,接下来的调用花费的时间要少得多。


firebase回答于:1279.422ms

firebase回答了:236.378ms

firebase回答了:228.595ms

firebase回答了:202.700ms



firebase回答于:208.371ms

firebase回答于:214.807ms
$但是,如果数据缓存在本地〜200ms code>(有时更多)似乎有很多时间来访问本地数据。足以让用户在呈现UI时感觉到延迟。

那么Firebase缓存数据呢?这些〜200ms中发生了什么?

解决方案

Firebase缓存数据(在内存中),只要存在该数据的活动侦听器。

由于您的代码只使用 once()侦听器,当收到数据时(在调用回调之前)立即分离侦听器,并从缓存中清除数据。这意味着必须从服务器获取每个 once()的数据,这显然是200ms往返的情况。第一个加载速度较慢,因为连接可能在该调用中建立。

验证这一点的一个快速技巧是在启动循环之前添加一个永久侦听器:

p>

  firebase.initializeApp(config); 

var counter = 0;

console.time('firebase回答'); ('value',function(){});
firebase.database()。ref('texts')。
firebase.database()。ref('texts')。once('value',onData);

函数onData(快照){
console.timeEnd('firebase回答');

counter ++;
if(counter> 20)return;
$ b $ setTimeout(function(){
console.time('firebase回答');
firebase.database()。ref('texts')。once('value ',onData);
},2000);



$ b $ p
$ b

通过这个简单的改变,日志记录就变成了:


firebase回答了:580.575ms

firebase回答了:4.040ms

firebase的回答是:7.569ms

firebase的回答是:5.739ms


I read somewhere a claim that Firebase caches the data.

So I ran this test that reads a semi large volume of data (about 400KB).

Here is the relevant code.

firebase.initializeApp(config);

var counter = 0;

console.time('firebase answered in');
firebase.database().ref('texts').once('value',onData);

function onData(snapshot){
  console.timeEnd('firebase answered in');

  counter ++;
  if(counter > 20) return;

  setTimeout(function(){
    console.time('firebase answered in');
    firebase.database().ref('texts').once('value',onData);
  },2000);
}

As you can see, the first time it loads data it takes a while, and subsequent calls take much less time.

firebase answered in: 1279.422ms

firebase answered in: 236.378ms

firebase answered in: 228.595ms

firebase answered in: 202.700ms

firebase answered in: 208.371ms

firebase answered in: 214.807ms

etc

But, still, if the data is cached locally ~200ms (sometimes more) seems like a lot of time to access local data. Enough for the user to perceive a delay when rendering the UI.

So is Firebase caching the data? What is happening in those ~200ms?

解决方案

Firebase caches the data (in memory) for as long as there is an active listener for that data.

Since your code uses only once() listener, the listener is detached immediately when the data is received (before your callback is invoked) and the data is cleared from the cache. That means that is has to get the data from the servers for each once(), which apparently is a 200ms round-trip in your case. The first load is slower, because the connection is likely established in that call.

A quick trick to verify this is to add a permanent listener before starting your loop:

firebase.initializeApp(config);

var counter = 0;

console.time('firebase answered in');
firebase.database().ref('texts').on('value',function() {});
firebase.database().ref('texts').once('value',onData);

function onData(snapshot){
  console.timeEnd('firebase answered in');

  counter ++;
  if(counter > 20) return;

  setTimeout(function(){
    console.time('firebase answered in');
    firebase.database().ref('texts').once('value',onData);
  },2000);
}

With that simple change, the logging turns into:

firebase answered in: 580.575ms

firebase answered in: 4.040ms

firebase answered in: 7.569ms

firebase answered in: 5.739ms

这篇关于Firebase是否缓存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆