Skip to content

Commit

Permalink
feat: cache pub/priv conf
Browse files Browse the repository at this point in the history
  • Loading branch information
mvayngrib committed Nov 26, 2017
1 parent bc98c52 commit 12c7b74
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
18 changes: 9 additions & 9 deletions lib/s3-utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions lib/samplebot/configure.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions src/s3-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import omit = require('object.omit')
import Errors = require('./errors')
import Logger from './logger'

Expand Down Expand Up @@ -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
}
Expand Down
27 changes: 25 additions & 2 deletions src/samplebot/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 12c7b74

Please sign in to comment.