Javascript 如何使用google云函数实例减少冷启动时间?

Javascript 如何使用google云函数实例减少冷启动时间?,javascript,firebase,google-cloud-platform,google-cloud-functions,Javascript,Firebase,Google Cloud Platform,Google Cloud Functions,有了firebase,您就可以了。 我有两个函数,分别名为“function1”和“function2”,位于两个单独的文件中 文件:function1.js const functions = require('firebase-functions');//This will be executed regardless of the function called exports.function1 = functions.https.onRequest((request, response

有了firebase,您就可以了。
我有两个函数,分别名为“function1”和“function2”,位于两个单独的文件中

文件:function1.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
文件:function2.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
现在我使用index.js导出这些文件,如图所示

文件:index.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
当我执行function1时,我可以从function2访问“admin”变量
显而易见的解决办法是不在全局范围内声明变量

修改文件:function2.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
现在,“admin”变量仅在调用function2而不是function1时初始化
云函数通常是前一次调用的执行环境。
如果在全局范围中声明变量,则其值可以在后续调用中重用,而无需重新计算。
但是现在“admin”变量将不会在后续调用中重用,因为它没有在全局范围中声明。

因此,我的问题是如何将“admin”变量存储在全局作用域中(以便它可以被多个实例重用),但在调用function1时不初始化它?

您尝试执行的操作是不可能的。根据定义,两个服务器实例不能共享内存。他们彼此完全隔绝。每个函数调用在它自己的实例中独立运行,两个不同的函数永远不能重用同一个实例。您必须接受function1的全局内存空间永远不会被function2看到


观看此视频了解更多信息:

经过一些研究,答案是。
如果在全局范围内初始化变量,初始化代码将始终通过冷启动调用执行,从而增加函数的延迟。
如果在所有的代码路径中不使用某些对象,请考虑在需求上懒洋洋地初始化它们:

文件:function1.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
文件:function2.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
现在我使用index.js导出这些文件,如图所示

文件:index.js

const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
const functions = require('firebase-functions');//This will be executed regardless of the function called
const admin = require('firebase-admin');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    // ...
});
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
const functions = require('firebase-functions');//This will be executed regardless of the function called
exports.function2 = functions.https.onRequest((request, response) => {
    const admin = require('firebase-admin');//This will only be executed when function2 is called
    // ...
});
exports.function1 = functions.https.onRequest((request, response) => {
    // ...
});
exports.function2 = functions.https.onRequest((request, response) => {
    admin = admin || require('firebase-admin');
    // ...
});
const functions = require('firebase-functions');
const admin;
const function1 = require('./function1');
const function2 = require('./function2');
exports.function1 = function1.function1;
exports.function2 = function2.function2;
如果在单个文件或多个文件中定义多个函数,并且不同的函数使用不同的变量,这一点尤为重要。
除非使用延迟初始化,否则可能会在已初始化但从未使用过的变量上浪费资源