Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在Firebase中为大型数据列表创建缓存计数器路径_List_Caching_Counter_Firebase - Fatal编程技术网

List 在Firebase中为大型数据列表创建缓存计数器路径

List 在Firebase中为大型数据列表创建缓存计数器路径,list,caching,counter,firebase,List,Caching,Counter,Firebase,使用Firebase计算总记录的方法如下: var table = new Firebase('http://beta.firebase.com/user/tablename'); table.on('value', function(snapshot) { var count = 0; snapshot.forEach(function() { count++; }); //count is now safe to use. }); 有没有办法通过在不

使用Firebase计算总记录的方法如下:

var table = new Firebase('http://beta.firebase.com/user/tablename');

table.on('value', function(snapshot) {
   var count = 0;
   snapshot.forEach(function() {
       count++;
   });
   //count is now safe to use.
});
有没有办法通过在不同路径中使用缓存计数器来避免枚举

我在思考一个反对象,它保存了变化的历史和最后计算的值

counter:
{   
    value: 672,
    history:    
    { 
         +2, -4, +1, +1, +1    
    }
}
在交易中,则:

选择一个历史记录项,更新值,删除历史记录项。 谁来负责这件事呢?

这里是。对于您的用例,您可以跳过ID部分,但原则仍然相同

其核心是在创建新记录时向计数器添加一条记录:

var fb = new Firebase('http://beta.firebase.com');

// stores incremental id before adding record
function incRecord(data) {
    // increment the counter
    fb.child('counter/value').transaction(function(currentValue) {
        return (currentValue||0) + 1
    }, function(err, committed, ss) {
        if( err ) {
           console.error(err);
        }
        else if( committed ) {
           // if you want to pass the counter into the data, 
           // just use ss.val() here to fetch it
           addRecord(data); 

           // could also store an audit history about changes to the counter, assuming we had a user ID or something to that effect with this:
           // but you don't need this history to increment it
           // fb.child('counter/history/'+ss.val()).set(userId);
        }
    });
}

// creates new record
function addRecord(data) {
   // you could pass the record value here, I just set the value to "record #<id>"
   fb.child('records').push(data, function(err) {
      err && console.error(err);
   });        
}
incRecord({ hello: 'world' });