improve memoize caching

This commit is contained in:
Josh Harvey 2017-12-31 10:13:22 +01:00
parent 70438b9796
commit 4214971cea
2 changed files with 15 additions and 6 deletions

View file

@ -6,7 +6,8 @@ const logger = require('./logger')
const lastRate = {} const lastRate = {}
const FETCH_INTERVAL = 10000 const FETCH_INTERVAL = 10000
function getRates (settings, fiatCode, cryptoCode) {
function _getRates (settings, fiatCode, cryptoCode) {
return Promise.resolve() return Promise.resolve()
.then(() => { .then(() => {
const config = settings.config const config = settings.config
@ -33,6 +34,9 @@ function getRates (settings, fiatCode, cryptoCode) {
}) })
} }
module.exports = { const getRates = mem(_getRates, {
getRates: mem(getRates, {maxAge: FETCH_INTERVAL}) maxAge: FETCH_INTERVAL,
} cacheKey: (settings, fiatCode, cryptoCode) => JSON.stringify([fiatCode, cryptoCode])
})
module.exports = {getRates}

View file

@ -40,7 +40,7 @@ function fetchWallet (settings, cryptoCode) {
const lastBalance = {} const lastBalance = {}
function balance (settings, cryptoCode) { function _balance (settings, cryptoCode) {
return fetchWallet(settings, cryptoCode) return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.balance(r.account, cryptoCode)) .then(r => r.wallet.balance(r.account, cryptoCode))
.then(balance => ({balance, timestamp: Date.now()})) .then(balance => ({balance, timestamp: Date.now()}))
@ -151,8 +151,13 @@ function cryptoNetwork (settings, cryptoCode) {
return wallet.cryptoNetwork(account, cryptoCode) return wallet.cryptoNetwork(account, cryptoCode)
} }
const balance = mem(_balance, {
maxAge: FETCH_INTERVAL,
cacheKey: (settings, cryptoCode) => cryptoCode
})
module.exports = { module.exports = {
balance: mem(balance, {maxAge: FETCH_INTERVAL}), balance,
sendCoins, sendCoins,
newAddress, newAddress,
getStatus, getStatus,