gpt4 book ai didi

firebase - 加速云功能

转载 作者:行者123 更新时间:2023-12-04 01:59:51 25 4
gpt4 key购买 nike

我有一个简单的函数,它只执行一个事务来帮助保持一个列表的计数。

但是,当我运行它时,我注意到它需要将近 5 秒的时间来执行,对于函数的简单性来说,这似乎很慢。有什么我可以做的或更快的方法来保留计数器吗?

exports.CountCommentsUp = functions.firestore.document('Groups/{groupID}/TextFeedActive/{postID}/Comments/{commentID}').onCreate(event => {
// ref to the parent document
const docRef = admin.firestore().collection('Groups/' + event.params.groupID+ '/Feed/').doc(event.params.postID);

//Along with Creating Counter, We need to create Notification REF
return admin.firestore().runTransaction(function(transaction) {
return transaction.get(docRef).then(function(sfDoc) {
var newCC = sfDoc.data().CommentCount + 1;
transaction.update(docRef, { CommentCount: newCC });
return newCC;

});
})
});

我看了很多遍,它确实有效,只是感觉很慢。有没有其他方法可以做到这一点?为了让数据库感觉实时,拥有更快的计数器变量会很棒

最佳答案

对于 future 的读者:

section of the GCP documentation ,他们讨论了提高云功能性能的方法。

引用文档:

Use dependencies wisely

Because functions are stateless, the execution environment is often initialized from scratch (during what is known as a cold start). When a cold start occurs, the global context of the function is evaluated.

If your functions import modules, the load time for those modules can add to the invocation latency during a cold start. You can reduce this latency, as well as the time needed to deploy your function, by loading dependencies correctly and not loading dependencies your function doesn't use.

Use global variables to reuse objects in future invocations

There is no guarantee that the state of a Cloud Function will be preserved for future invocations. However, Cloud Functions often recycles the execution environment of a previous invocation. If you declare a variable in global scope, its value can be reused in subsequent invocations without having to be recomputed.

This way you can cache objects that may be expensive to recreate on each function invocation. Moving such objects from the function body to global scope may result in significant performance improvements. The following example creates a heavy object only once per function instance, and shares it across all function invocations reaching the given instance: It is particularly important to cache network connections, library references, and API client objects in global scope. See Optimizing Networking for examples.

Do lazy initialization of global variables

If you initialize variables in global scope, the initialization code will always be executed via a cold start invocation, increasing your function's latency. If some objects are not used in all code paths, consider initializing them lazily on demand:

This is particularly important if you define several functions in a single file, and different functions use different variables. Unless you use lazy initialization, you may waste resources on variables that are initialized but never used.



您还可以阅读 Google 开发者倡导者撰写的文章: Improving Cloud Function cold start time .
总结文章中提出的要点(用于加速云功能)

  • Trimming dependencies.
  • Using dependencies cache.
  • Lazy loading

关于firebase - 加速云功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48064786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com