使用Firebase过期网址 [英] Expiring URLs with Firebase

查看:102
本文介绍了使用Firebase过期网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建指向Firebase数据的临时网址,但是数据(和网址)会在特定时间(即5分钟或15分钟)后被销毁?

How does someone create temporary URLs that point to Firebase data, but the data (and URLs) will be destroyed after a specific time, i.e., 5 minutes or 15 minutes?

推荐答案

根据数据的存储方式,可以通过时间戳删除数据。

Depending on exactly how the data is being stored, there are a few different options for removing the data by timestamp.

假设数据是未排序的,并且您已经将时间戳存储在每条记录中的一个字段中,那么您可以直接从第一条记录开始读取并删除它们,直到找到要保留的记录为止:

Assuming the data is unsorted, and you have stored the timestamp as a field in each record, then you may simply begin reading from the first record and deleting them until you find one you want to keep:

<script>
var FB = new Firebase(YOUR_URL);
var childRef = FB.child( TABLE_WITH_RECORDS_TO_DELETE );
var oneWeekAgo = new Date().getTime()-1000*60*60*24*7; // one week ago

var fx = function(snapshot) { // keep a ref to this so we can call off later
   var v = snapshot.val();
   if( v.expires_on_date < oneWeekAgo ) {
      // delete the record
      snapshot.ref().remove();
   }
   else {
      // we found the first keeper, so we are done
      childRef.off('child_added', fx);
   }
}

// fetched records and test to see how old they are
childRef.on('childAdded', fx);
</script>

如果数据按时间戳排序,您可以按如下方式检索和删除它们:

If the data is sorted by timestamp, you can retrieve and delete them as follows:

var FB = new Firebase(YOUR_URL);
var childRef = FB.child( TABLE_WITH_RECORDS_TO_DELETE );
var oneWeekAgoMinusOne = new Date().getTime()-1000*60*60*24*7-1; // one week ago

// fetched using endAt, so only retrieving older than 1 week
childRef.endAt( oneWeekAgoMinusOne ).on('childAdded', function(snapshot) {
    // delete the record
   snapshot.ref().remove();
});

这篇关于使用Firebase过期网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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