buffering-cache
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -27,2 +27,6 @@ /*eslint no-console: "off" */ | ||
.pipe(mocha({reporter: 'spec'})) | ||
.once('error', () => { | ||
console.error('tests failed'); | ||
process.exit(1); | ||
}) | ||
.pipe(istanbul.writeReports({ | ||
@@ -29,0 +33,0 @@ reporters: [ |
@@ -38,5 +38,11 @@ const stringify = require('json-stable-stringify'); | ||
self.wrapFunction = (functionToWrap, that = null, functionId = null) => { | ||
self.wrapFunction = (functionToWrap, that = null, functionId = null, postCallMutator = null) => { | ||
functionId = (typeof functionId === 'string' && functionId) || functionToWrap.name || uuid.v4(); | ||
if (postCallMutator && typeof(postCallMutator) !== 'function') { | ||
throw new Error(`postCallMutator must be a function. You passed a ${typeof(postCallMutator)}`); | ||
} | ||
postCallMutator = postCallMutator || ((r) => r); | ||
return function () { | ||
@@ -61,3 +67,3 @@ const args = arguments; | ||
log.debug('Found value in local cache'); | ||
return localValue; | ||
return (postCallMutator(localValue, 'localCache')); | ||
} | ||
@@ -70,3 +76,3 @@ | ||
log.debug('Found value in remote cache'); | ||
return self.localCache.setpx(key, remoteValue, localCacheSpec.ttl).return(remoteValue); | ||
return self.localCache.setpx(key, remoteValue, localCacheSpec.ttl).return( postCallMutator(remoteValue, 'remoteCache') ); | ||
} | ||
@@ -76,4 +82,4 @@ | ||
.then((response) => { | ||
log.debug('Fetched original value'); | ||
return updateCaches(key, response).return(response); | ||
log.debug('Fetched service value'); | ||
return updateCaches(key, response).return( (postCallMutator(response, 'service')) ); | ||
}); | ||
@@ -80,0 +86,0 @@ }); |
{ | ||
"name": "buffering-cache", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Node cache wrapper that keeps your cache warm", | ||
@@ -44,5 +44,5 @@ "main": "index.js", | ||
"pre-commit": "^1.1.3", | ||
"supertest": "^2.0.1", | ||
"supertest": "^3.0.0", | ||
"supertest-as-promised": "^4.0.2" | ||
} | ||
} |
# buffering-cache | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/groupby/buffering-cache.svg)](https://greenkeeper.io/) | ||
[![CircleCI](https://circleci.com/gh/groupby/buffering-cache.svg?style=svg)](https://circleci.com/gh/groupby/buffering-cache) | ||
@@ -3,0 +5,0 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/fef770286f884ecc9c221d8bb61ae083)](https://www.codacy.com/app/GroupByInc/buffering-cache?utm_source=github.com&utm_medium=referral&utm_content=groupby/buffering-cache&utm_campaign=Badge_Grade) |
@@ -190,6 +190,199 @@ const chai = require('chai'); | ||
expect(value).to.eql(getValue); | ||
expect(localArgs.get).to.match(new RegExp(functionArg)); | ||
expect(functionCalledWith).to.eql(null); | ||
expect(remoteArgs.get).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.ttl).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.setpx).to.eql({}); | ||
expect(remoteArgs.delete).to.eql(null); | ||
expect(localArgs.setpx).not.to.eql({}); | ||
expect(localArgs.setpx.key).to.match(new RegExp(functionArg)); | ||
expect(localArgs.setpx.value).to.eql(getValue); | ||
expect(localArgs.setpx.ttl).to.eql(10); | ||
expect(localArgs.delete).to.eql(null); | ||
done(); | ||
}); | ||
}); | ||
it('get value from local cache, mutated by postCallMutator function', (done) => { | ||
const remoteArgs = { | ||
get: null, | ||
setpx: {}, | ||
delete: null, | ||
ttl: null | ||
}; | ||
const remoteCache = { | ||
store: { | ||
get: (key) => { | ||
remoteArgs.get = key; | ||
}, | ||
setpx: (key, value, ttl) => { | ||
remoteArgs.setpx.key = key; | ||
remoteArgs.setpx.ttl = ttl; | ||
remoteArgs.setpx.value = value; | ||
}, | ||
delete: (key) => { | ||
remoteArgs.delete = key; | ||
}, | ||
pttl: (key) => { | ||
remoteArgs.ttl = key; | ||
return ttlMsec; | ||
}, | ||
client: {} | ||
}, | ||
ttl: 60, | ||
bufferTtl: 30 | ||
}; | ||
const localArgs = { | ||
get: null, | ||
setpx: {}, | ||
delete: null, | ||
ttl: null | ||
}; | ||
const getValue = 33; | ||
const ttlMsec = 60; | ||
const localCache = { | ||
store: { | ||
get: (key) => { | ||
localArgs.get = key; | ||
return getValue; | ||
}, | ||
setpx: (key, value, ttl) => { | ||
localArgs.setpx.key = key; | ||
localArgs.setpx.ttl = ttl; | ||
localArgs.setpx.value = value; | ||
}, | ||
delete: (key) => { | ||
localArgs.delete = key; | ||
}, | ||
pttl: (key) => { | ||
localArgs.ttl = key; | ||
}, | ||
client: {} | ||
}, | ||
ttl: 10 | ||
}; | ||
const callMe = (d) => { | ||
return d * 2; | ||
}; | ||
const bufferingCache = new BufferingCache(remoteCache, localCache); | ||
let functionCalledWith = null; | ||
const wrappedFunction = bufferingCache.wrapFunction((first) => { | ||
functionCalledWith = first; | ||
}, null, null, callMe); | ||
const functionArg = 'give me this'; | ||
wrappedFunction(functionArg).delay(10).then((value) => { | ||
expect(value).to.eql(getValue * 2); | ||
expect(localArgs.get).to.match(new RegExp(functionArg)); | ||
expect(functionCalledWith).to.eql(null); | ||
expect(remoteArgs.get).to.eql(null); | ||
expect(remoteArgs.ttl).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.setpx).to.eql({}); | ||
expect(remoteArgs.delete).to.eql(null); | ||
expect(localArgs.setpx).to.eql({}); | ||
expect(localArgs.delete).to.eql(null); | ||
done(); | ||
}); | ||
}); | ||
it('executes postCallMutator when passed and accessing from remote', (done) => { | ||
const remoteArgs = { | ||
get: null, | ||
setpx: {}, | ||
delete: null, | ||
ttl: null | ||
}; | ||
const remoteCache = { | ||
store: { | ||
get: (key) => { | ||
remoteArgs.get = key; | ||
return JSON.stringify({value: getValue}); | ||
}, | ||
setpx: (key, value, ttl) => { | ||
remoteArgs.setpx.key = key; | ||
remoteArgs.setpx.ttl = ttl; | ||
remoteArgs.setpx.value = value; | ||
}, | ||
delete: (key) => { | ||
remoteArgs.delete = key; | ||
}, | ||
pttl: (key) => { | ||
remoteArgs.ttl = key; | ||
return ttlMsec; | ||
}, | ||
client: {} | ||
}, | ||
ttl: 60, | ||
bufferTtl: 30 | ||
}; | ||
const localArgs = { | ||
get: null, | ||
setpx: {}, | ||
delete: null, | ||
ttl: null | ||
}; | ||
const getValue = 33; | ||
const ttlMsec = 60; | ||
const localCache = { | ||
store: { | ||
get: (key) => { | ||
localArgs.get = key; | ||
}, | ||
setpx: (key, value, ttl) => { | ||
localArgs.setpx.key = key; | ||
localArgs.setpx.ttl = ttl; | ||
localArgs.setpx.value = value; | ||
}, | ||
delete: (key) => { | ||
localArgs.delete = key; | ||
}, | ||
pttl: (key) => { | ||
localArgs.ttl = key; | ||
}, | ||
client: {} | ||
}, | ||
ttl: 10 | ||
}; | ||
const callMe = (d) => { | ||
return d * 2; | ||
}; | ||
const bufferingCache = new BufferingCache(remoteCache, localCache); | ||
let functionCalledWith = null; | ||
const wrappedFunction = bufferingCache.wrapFunction((first) => { | ||
functionCalledWith = first; | ||
}, null, 'first', callMe); | ||
const functionArg = 'give me this'; | ||
wrappedFunction(functionArg).delay(10).then((value) => { | ||
expect(value).to.eql(getValue * 2); | ||
//Everything below this is to make sure nothing else changed when we did a thing | ||
expect(localArgs.get).to.match(new RegExp(functionArg)); | ||
expect(functionCalledWith).to.eql(null); | ||
expect(remoteArgs.get).to.match(new RegExp(functionArg)); | ||
@@ -248,2 +441,104 @@ expect(remoteArgs.ttl).to.match(new RegExp(functionArg)); | ||
//getValue gets mutated | ||
const getValue = '1234'; | ||
const ttlMsec = 60; | ||
const localCache = { | ||
store: { | ||
get: (key) => { | ||
localArgs.get = key; | ||
}, | ||
setpx: (key, value, ttl) => { | ||
localArgs.setpx.key = key; | ||
localArgs.setpx.ttl = ttl; | ||
localArgs.setpx.value = value; | ||
}, | ||
delete: (key) => { | ||
localArgs.delete = key; | ||
}, | ||
pttl: (key) => { | ||
localArgs.ttl = key; | ||
}, | ||
client: {} | ||
}, | ||
ttl: 10 | ||
}; | ||
const callMe = (d) => { | ||
return d * 2; | ||
}; | ||
const bufferingCache = new BufferingCache(remoteCache, localCache); | ||
let functionCalledWith = null; | ||
const wrappedFunction = bufferingCache.wrapFunction((first) => { | ||
functionCalledWith = first; | ||
return getValue; | ||
}, null, null, callMe); | ||
const functionArg = 'give me this'; | ||
wrappedFunction(functionArg).delay(10).then((value) => { | ||
expect(value).to.eql(getValue * 2); | ||
expect(localArgs.get).to.match(new RegExp(functionArg)); | ||
expect(functionCalledWith).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.get).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.ttl).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.setpx).not.to.eql({}); | ||
expect(remoteArgs.setpx.key).to.match(new RegExp(functionArg)); | ||
expect(remoteArgs.setpx.value).to.eql(JSON.stringify({value: getValue})); | ||
expect(remoteArgs.setpx.ttl).to.eql(60); | ||
expect(remoteArgs.delete).to.eql(null); | ||
expect(localArgs.setpx).not.to.eql({}); | ||
expect(localArgs.setpx.key).to.match(new RegExp(functionArg)); | ||
expect(localArgs.setpx.value).to.eql(getValue); | ||
expect(localArgs.setpx.ttl).to.eql(10); | ||
expect(localArgs.delete).to.eql(null); | ||
done(); | ||
}); | ||
}); | ||
it('data not in local or remote cache, has to retrieve from original service AND THEN mutates the data', (done) => { | ||
const remoteArgs = { | ||
get: null, | ||
setpx: {}, | ||
delete: null, | ||
ttl: null | ||
}; | ||
const remoteCache = { | ||
store: { | ||
get: (key) => { | ||
remoteArgs.get = key; | ||
}, | ||
setpx: (key, value, ttl) => { | ||
remoteArgs.setpx.key = key; | ||
remoteArgs.setpx.ttl = ttl; | ||
remoteArgs.setpx.value = value; | ||
}, | ||
delete: (key) => { | ||
remoteArgs.delete = key; | ||
}, | ||
pttl: (key) => { | ||
remoteArgs.ttl = key; | ||
return ttlMsec; | ||
}, | ||
client: {} | ||
}, | ||
ttl: 60, | ||
bufferTtl: 30 | ||
}; | ||
const localArgs = { | ||
get: null, | ||
setpx: {}, | ||
delete: null, | ||
ttl: null | ||
}; | ||
const getValue = 'i got this'; | ||
@@ -250,0 +545,0 @@ const ttlMsec = 60; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56673
1470
77