Firebase - 缓存是否可以提高性能? [英] Firebase - does caching improve performance?

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

问题描述

我正在使用Firebase构建一个Web应用程序,该应用程序采用相同的数据,并以两种不同的方式显示 - 在列表中以及在Google地图上作为标记。

现在,在每个视图(地图或列表)中,我都有代码来查询来自Firebase的数据,并将其合并并显示出来。相反,我正在考虑这个计划:在启动时,查询数据,将其合并,并将其全部保存到从视图传递到数组中的数组中。



从某种意义上说,我正在缓存一个数组中的firebase数据。从某种意义上说,这并不理想 - 缓存的数据不像直接查询Firebase那样是最新的。另一方面,我只调用Firebase一次。

这是否使性能感觉?从Firebase中读取数据的时间与从数组中读取数据的时间相同吗?解析方案

通常,缓存限制Firebase使用的数据是不必要的。 Firebase在客户端维护自己的活动数据缓存。 有效被定义为对其未接通呼叫的数据。因此,对于任何活动的数据,任何额外的开或一次呼叫将不需要网络流量,因为数据已经被加载。



我不完全确定你的意思是查询火力点。 Firebase没有传统意义上的查询。它只是有附加回调的方法。您是否使用once()函数定期从Firebase获取数据?如果是这样,这可能是非常低效的。一旦是一种方便的方法,一般只能用于非常少访问的数据,或者由于某种原因,开发人员不希望实时更新。如果在once()完成时没有活动的on调用未完成,Firebase将刷新该数据的缓存,并且任何后续调用once()都将需要往返于服务器的往返。



如果您想要以一种有效的方式同步访问最新版本数据的本地副本,我建议使用以下方法:

  var savedSnapshot = null; 
dataRef.on(value,function(snapshot){
savedSnapshot = snapshot;
});

//然后当你需要读取数据
var theData = savedSnapshot.val()



通过维护一个on()调用,Firebase可以随时更新数据,只需在事情发生变化时通过网络发送增量,而不是每次重新加载所有数据当你需要它的时候。


I'm building a web application using Firebase that takes the same data and presents it in two different ways - in a list and as an markers on a google map.

Right now, in each view - map or list - I have code to query data from firebase, merge it together, and display it. Instead, I'm considering this plan: on startup, query the data, merge it, and save it all into an array that I pass from view to view.

In a sense, I am "caching" the firebase data in an array. This isn't ideal in one sense - cached data isn't as up-to-date as directly querying Firebase. On the other hand, I only call Firebase once.

Does this make performance sense? Does reading data from a Firebase take within the same order of magnitude time as reading data from an array?

解决方案

In general, caching data to limit use of Firebase is unnecessary. Firebase maintains its own cache of "active" data on the client. "Active" is defined as data for which an "on" call is outstanding on it. Therefore for any active data, any additional "on" or "once" calls will require no network traffic as the data has already been loaded.

I'm not entirely sure what you mean by "querying firebase". Firebase does not have queries in the traditional sense. It simply has methods for attaching callbacks. Are you using the "once()" function to get data periodically from Firebase? If so this could potentially be very inefficient. Once is a convenience method and should generally only be used for data that is accessed extremely infrequently or that for some reason the developer does not want to update in real-time. If no active "on" calls are outstanding when the once() completes, Firebase will flush the cache of that data, and any subsequent calls to once() will require a roundtrip to the server.

If what you want is a way to synchronously access a local copy of the latest version of the data in an efficient way, I recommend this method:

var savedSnapshot = null;
dataRef.on("value", function(snapshot) {
  savedSnapshot = snapshot;
});

//and then when you need to read the data
var theData = savedSnapshot.val()

By maintaining one single on() call, Firebase is able to keep your data up-to-date by only sending deltas over the wire when things change, rather than reloading all of the data every time you need it.

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

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