From 12c7b742a0a7dc0f50c1db0c50f1d72615370bcd Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Sun, 26 Nov 2017 17:56:19 -0500 Subject: [PATCH] feat: cache pub/priv conf --- lib/s3-utils.js | 18 +++++++++--------- lib/samplebot/configure.js | 22 ++++++++++++++++++++-- src/s3-utils.ts | 19 +++++++++++-------- src/samplebot/configure.ts | 27 +++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/lib/s3-utils.js b/lib/s3-utils.js index 228230839..eeb84e340 100644 --- a/lib/s3-utils.js +++ b/lib/s3-utils.js @@ -17,6 +17,7 @@ var __rest = (this && this.__rest) || function (s, e) { return t; }; Object.defineProperty(exports, "__esModule", { value: true }); +const omit = require("object.omit"); const Errors = require("./errors"); const logger_1 = require("./logger"); module.exports = function createUtils(aws) { @@ -59,16 +60,15 @@ module.exports = function createUtils(aws) { let cached; let etag; let cachedTime; - const maybeGet = (opts) => __awaiter(this, void 0, void 0, function* () { - if (typeof opts === 'string') { - opts = { key: opts }; - } - const age = Date.now() - cachedTime; - if (etag && age < ttl) { - logger.debug(`returning cached item for key ${key}, ttl: ${(ttl - age)}`); - return cached; + const maybeGet = (opts = {}) => __awaiter(this, void 0, void 0, function* () { + if (!opts.force) { + const age = Date.now() - cachedTime; + if (etag && age < ttl) { + logger.debug(`returning cached item for key ${key}, ttl: ${(ttl - age)}`); + return cached; + } } - opts = Object.assign({}, defaultOpts, opts); + opts = Object.assign({}, defaultOpts, omit(opts, ['force'])); if (etag) { opts.IfNoneMatch = etag; } diff --git a/lib/samplebot/configure.js b/lib/samplebot/configure.js index 924f7c0c9..e6f8a2d87 100644 --- a/lib/samplebot/configure.js +++ b/lib/samplebot/configure.js @@ -16,8 +16,16 @@ const Errors = require("../errors"); const { reinitializeOnConfChanged } = serverlessYml.custom; class Conf { constructor(bot) { - this.getPrivateConf = () => this.privateConfBucket.getJSON(constants_1.PRIVATE_CONF_KEY); - this.getPublicConf = () => this.publicConfBucket.getJSON(constants_1.PUBLIC_CONF_KEY); + this.getPrivateConf = (forceFetch) => { + return this.privateConf.get({ + force: forceFetch + }); + }; + this.getPublicConf = (forceFetch) => { + return this.publicConf.get({ + force: forceFetch + }); + }; this.savePublicConf = (value, reinitializeContainers = true) => __awaiter(this, void 0, void 0, function* () { yield this.putIfDifferent({ bucket: this.publicConfBucket, @@ -59,6 +67,16 @@ class Conf { const { buckets } = bot.resources; this.privateConfBucket = buckets.PrivateConf; this.publicConfBucket = buckets.PublicConf; + this.publicConf = this.publicConfBucket.getCacheable({ + ttl: 60000, + key: constants_1.PUBLIC_CONF_KEY, + parse: JSON.parse.bind(JSON) + }); + this.privateConf = this.privateConfBucket.getCacheable({ + ttl: 60000, + key: constants_1.PRIVATE_CONF_KEY, + parse: JSON.parse.bind(JSON) + }); } } exports.Conf = Conf; diff --git a/src/s3-utils.ts b/src/s3-utils.ts index c43157509..e35e47989 100644 --- a/src/s3-utils.ts +++ b/src/s3-utils.ts @@ -1,3 +1,4 @@ +import omit = require('object.omit') import Errors = require('./errors') import Logger from './logger' @@ -70,18 +71,20 @@ module.exports = function createUtils (aws) { let cached let etag let cachedTime - const maybeGet = async (opts) => { - if (typeof opts === 'string') { - opts = { key: opts } + const maybeGet = async (opts={}) => { + if (!opts.force) { + const age = Date.now() - cachedTime + if (etag && age < ttl) { + logger.debug(`returning cached item for key ${key}, ttl: ${(ttl - age)}`) + return cached + } } - const age = Date.now() - cachedTime - if (etag && age < ttl) { - logger.debug(`returning cached item for key ${key}, ttl: ${(ttl - age)}`) - return cached + opts = { + ...defaultOpts, + ...omit(opts, ['force']) } - opts = { ...defaultOpts, ...opts } if (etag) { opts.IfNoneMatch = etag } diff --git a/src/samplebot/configure.ts b/src/samplebot/configure.ts index 93e59d3a7..181ced12e 100644 --- a/src/samplebot/configure.ts +++ b/src/samplebot/configure.ts @@ -10,15 +10,38 @@ export class Conf { public bot: any public privateConfBucket: any public publicConfBucket: any + public publicConf: any + public privateConf: any constructor(bot) { this.bot = bot const { buckets } = bot.resources this.privateConfBucket = buckets.PrivateConf this.publicConfBucket = buckets.PublicConf + this.publicConf = this.publicConfBucket.getCacheable({ + ttl: 60000, + key: PUBLIC_CONF_KEY, + parse: JSON.parse.bind(JSON) + }) + + this.privateConf = this.privateConfBucket.getCacheable({ + ttl: 60000, + key: PRIVATE_CONF_KEY, + parse: JSON.parse.bind(JSON) + }) + } + + public getPrivateConf = (forceFetch:boolean) => { + return this.privateConf.get({ + force: forceFetch + }) + } + + public getPublicConf = (forceFetch:boolean) => { + return this.publicConf.get({ + force: forceFetch + }) } - public getPrivateConf = () => this.privateConfBucket.getJSON(PRIVATE_CONF_KEY) - public getPublicConf = () => this.publicConfBucket.getJSON(PUBLIC_CONF_KEY) public savePublicConf = async (value:any, reinitializeContainers:boolean=true) => { await this.putIfDifferent({ bucket: this.publicConfBucket,