firebase - 两个云函数在一小时内使用超过 10,000 次读取?
问题描述
目前,我正在运行两个云功能。一个向帖子添加信息,而另一个删除旧帖子,例如:
exports.reveal = functions.database.ref('/reveals/{postIDthatWasRevealed}/revealed').onUpdate((change, context) => {
const revealedValue = change.after.val()
if (revealedValue === true) {
var updates = {}
const postID = context.params.postIDthatWasRevealed
console.log(postID)
return admin.firestore().collection('posters').doc(postID).get().then(snapshot => {
const value = snapshot.data()
console.log(value)
// console.log(value)
const posterID = value.posterID
const posterName = value.posterName
const profileImage = value.profileImage
const postKey = value.key
return admin.database().ref('/convoID/' + postID).once('value', (snapshot) => {
if (snapshot.exists()) {
const convoIDCollection = snapshot.val()
for (var child in convoIDCollection) {
const convoID = child
updates["/conversations/"+convoID+"/information/reciever/Name"] = posterName
updates["/conversations/"+convoID+"/information/reciever/profileImage"] = profileImage
updates["/conversations/"+convoID+"/key"] = postKey
}
}
const currentTime = Date.now()
const addedTime = currentTime + 172800000
const batch = admin.firestore().batch()
const postFireStoreRef = admin.firestore().collection('posts').doc(postID)
batch.update(postFireStoreRef,{"revealedDate": currentTime})
batch.update(postFireStoreRef,{"timeOfDeletion": addedTime})
batch.update(postFireStoreRef,{"information": {"posterID":posterID,"posterName":posterName,"profileImage":profileImage} })
batch.update(postFireStoreRef,{"key":postKey})
return batch.commit(), admin.database().ref().update(updates)
})
})
}
else {
return null
}
})
^当获得足够的赞时,该帖子会在帖子中添加信息。帖子存储在 firestore 中,当与帖子关联的 firebase 节点获得足够的点赞数时,将下载一些附加到 firestore 帖子实体的信息。理论上,它甚至不应该读取一次,因为来自 firestore 实体的数据永远不会被下载,只会被修改。运行的第二个函数如下:
exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((change,context) => {
const currentTime = Date.now()
const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
return getPostsForDate.get().then(snapshot => {
const updates = {}
const batch = admin.firestore().batch()
snapshot.forEach((doc) => {
var key = doc.id
console.log(key)
const convos = database().ref('/convoID/' + key).once('value', (snapshot) => {
if (snapshot.exists){
for (var child in snapshot) {
const convoID = child
console.log(child+"shit")
updates["conversations/" + value] = null
}
}
})
updates["/convoID/"+ key] = null
updates["/reveals/" + key] = null
updates["/postDetails/" + key] = null
const postFireStoreRef = admin.firestore().collection('posts').doc(key)
const posterRef = admin.firestore().collection('posters').doc(key)
batch.delete(postFireStoreRef)
batch.delete(posterRef)
})
return admin.database().ref().update(updates), batch.commit()
})
})
每分钟,这都会查询旧帖子的火库。最多可能会返回两到三个帖子,从而导致阅读。然而,在对这些功能进行了一个小时的测试后,Google App Engine Quota 显示一万次读取,而我预计接近 20 到 50 次。此外,一整天只部署了这些功能,因此它们只运行了 87 次。这些功能没有优化吗?有没有办法监控读取操作的来源?
编辑:似乎每次我触发删除功能(实际上更改帖子的时间戳,以便在每分钟调用删除功能时将其删除)我的读取增加了几百...
解决方案
推荐阅读
- ios - swift 4 Google Map - 如何使用gesture.position更新标记位置
- android - 如何在android中从另一个应用程序启用文本到语音/对讲
- javascript - 如何在javascript中序列化地图?
- javascript - 在更改下拉列表时显示表中的行
- ios - 另一个计算变量设置器中的计算变量设置器使应用程序崩溃
- java - Java:字符串中的 PrintWriter 和换行符
- javascript - 根据变量选择数组
- javascript - 使用 jQuery 将选择选项 ID 值复制到隐藏字段中
- angularjs - 如何在angularjs中为GET http动词编写单元测试用例?
- php - Nginx 位置正则表达式