fredriks/cloud-functions-runtime-config

Consider global scope to 'cache' deployment (!) config

DazWilkin opened this issue · 3 comments

This is neat solution, thanks!

It was pointed out to me by a colleague on the Cloud Functions team that you may also leverage the Functions' global scope to cache per-deployment (!) config.

So, augmenting your purely dynamic example for static config:

/*jshint esversion: 6 */
/*globals exports, require */
var runtimeConfig = require('cloud-functions-runtime-config');

const configName = 'dev-config';
const variableName = 'lunch-plans';

var instanceConfig = {};

exports.lunchPlanner = function(req, res) {
    if (instanceConfig.hasOwnProperty(variableName)) {
      console.log(`Get instance global variable: ${variableName} == ${instanceConfig[variableName]}`);
      res.status(200).send(instanceConfig[variableName]);
    } else {
      runtimeConfig.getVariable(configName, variableName)
      .then((val) => {
        instanceConfig[variableName] = val;
        console.log(`Set instance global variable: ${variableName} == ${instanceConfig[variableName]}`);
        res.status(200).send(instanceConfig[variableName]);
      })
      .catch((err) => res.status(500).send(err));
    }
};

Thats a great point!

The example could be a lot better at promoting good practices. We could even take it a step further and do the variable lookup at deploy time instead of at call time:

const runtimeConfig = require('cloud-functions-runtime-config');
const lunchPlans = runtimeConfig.getVariable('dev-config', 'lunch-plans');

exports.lunchPlanner = (req, res) => {
    return lunchPlans
        .then((val) =>  res.status(200).send(val))
        .catch((err) => res.status(500).send(err));
};

Of course! :-( I didn't think to do that. That's better.

I meant the README example, which has now been updates to cache values instead of fetching them up on each and every call.

Thank you @DazWilkin for taking the time to point out the issue, I greatly appreciate it! 😃