buffering-cache
Advanced tools
Comparing version 2.1.1 to 2.2.0
14
index.js
@@ -31,7 +31,13 @@ const BufferCache = require('./lib'); | ||
if (_.isNil(ttlMsec) || !_.isNumber(ttlMsec)) { | ||
throw new Error('ttlMsec must be provided and must be a number'); | ||
if (_.isNil(ttlMsec) || (!_.isNumber(ttlMsec) && !_.isFunction(ttlMsec))) { | ||
throw new Error('ttlMsec must be provided and must be a positive number or a function that returns a positive number'); | ||
} | ||
if (!_.isNil(bufferTtlMsec) && (!_.isNumber(bufferTtlMsec) || bufferTtlMsec < 0 || bufferTtlMsec > ttlMsec)) { | ||
const cacheTtl = _.isFunction(ttlMsec) ? ttlMsec() : ttlMsec | ||
if(!_.isNumber(cacheTtl) || cacheTtl < 0){ | ||
throw new Error('ttl must be a number greater than 0 or a function which returns a number greater than 0'); | ||
} | ||
if (!_.isNil(bufferTtlMsec) && (!_.isNumber(bufferTtlMsec) || bufferTtlMsec < 0 || bufferTtlMsec > cacheTtl)) { | ||
throw new Error('bufferTtlMsec, if provided, must be a number greater than 0 and less than or equal to ttlMsec'); | ||
@@ -55,3 +61,3 @@ } | ||
ttl: ttlMsec, | ||
bufferTtl: bufferTtlMsec || ttlMsec / 2, | ||
bufferTtl: bufferTtlMsec || cacheTtl / 2, | ||
}; | ||
@@ -58,0 +64,0 @@ |
const Promise = require('bluebird'); | ||
const log = require('../logger'); | ||
const _ = require('lodash'); | ||
@@ -23,7 +24,7 @@ const COMMAND_TIMEOUT_MS = 50; | ||
if (typeof ttl !== 'number' || ttl < 0) { | ||
throw new Error('ttl must be a number greater than 0'); | ||
if ((!_.isNumber(ttl) && !_.isFunction(ttl)) || ttl < 0) { | ||
throw new Error('ttl must be a number greater than 0 or a function which returns a number greater than 0'); | ||
} | ||
if (typeof bufferTtl !== 'number' || bufferTtl < 0 || bufferTtl > ttl) { | ||
if (!_.isNumber(bufferTtl) || bufferTtl < 0 || bufferTtl > ttl) { | ||
throw new Error('if provided, bufferTtl must be a number between 0 and ttl'); | ||
@@ -35,2 +36,11 @@ } | ||
self.getTtl = () => { | ||
const ttlToUse = _.isFunction(ttl) ? ttl() : ttl; | ||
if (!_.isNumber(ttlToUse) || ttlToUse < 0) { | ||
throw new Error('ttl must be a number greater than 0 or a function which returns a number greater than 0'); | ||
} | ||
return ttlToUse; | ||
} | ||
self.get = (key) => { | ||
@@ -44,3 +54,3 @@ return failSafeFunction('get', [key]); | ||
value, | ||
overrideTtl || ttl | ||
overrideTtl || self.getTtl() | ||
]); | ||
@@ -54,3 +64,3 @@ }; | ||
value, | ||
overrideTtl || ttl | ||
overrideTtl || self.getTtl() | ||
]); | ||
@@ -57,0 +67,0 @@ }; |
@@ -29,2 +29,3 @@ const stringify = require('json-stable-stringify'); | ||
get: (key) => { }, | ||
setpx: (key, ttl, value) => { }, | ||
@@ -37,2 +38,4 @@ delete: (key) => { }, | ||
const remoteCacheSpecTtl = (typeof remoteCacheSpec.ttl === 'function') ? remoteCacheSpec.ttl() : remoteCacheSpec.ttl; | ||
self.remoteCache = new Cache(remoteCacheSpec); | ||
@@ -45,3 +48,3 @@ self.localCache = localCacheSpec ? new Cache(localCacheSpec) : new Cache(defaultLocalCacheSpec); | ||
if (localCacheSpec && remoteCacheSpec.ttl < localCacheSpec.ttl) { | ||
if (localCacheSpec && remoteCacheSpecTtl < localCacheSpec.ttl) { | ||
throw new Error('local cache has higher ttl than remote cache'); | ||
@@ -163,3 +166,3 @@ } | ||
return self.remoteCache.pttl(key).then((ttl) => { | ||
const minTimeRemaining = remoteCacheSpec.ttl - remoteCacheSpec.bufferTtl; | ||
const minTimeRemaining = remoteCacheSpecTtl - remoteCacheSpec.bufferTtl; | ||
@@ -166,0 +169,0 @@ log.debug(`TTL: ${ttl} BufferTTL: ${remoteCacheSpec.bufferTtl} Min Time Remaining: ${minTimeRemaining}`); |
{ | ||
"name": "buffering-cache", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Node cache wrapper that keeps your cache warm", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "gulp test", | ||
"coverage:codacy": "cat ./coverage/lcov.info | codacy-coverage", | ||
"lint": "gulp lint" | ||
"lint": "eslint", | ||
"test": "mocha tests/**/*.spec.js" | ||
}, | ||
"precommit": [ | ||
"lint" | ||
], | ||
"repository": { | ||
@@ -25,3 +21,3 @@ "type": "git", | ||
"json-stable-stringify": "^1.0.1", | ||
"lodash": "^4.17.15", | ||
"lodash": "^4.17.21", | ||
"lru-cache": "^4.0.1", | ||
@@ -34,11 +30,5 @@ "uuid": "^3.0.1" | ||
"codacy-coverage": "^2.0.2", | ||
"eslint-plugin-no-only-tests": "^1.1.0", | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^3.0.1", | ||
"gulp-if": "^2.0.2", | ||
"gulp-istanbul": "^1.1.1", | ||
"gulp-mocha": "^3.0.1", | ||
"eslint": "^8.1.0", | ||
"ioredis": "^3.2.2", | ||
"istanbul": "^0.4.3", | ||
"pre-commit": "^1.1.3", | ||
"mocha": "^9.1.3", | ||
"supertest": "^3.0.0", | ||
@@ -45,0 +35,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) | ||
[![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) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/fef770286f884ecc9c221d8bb61ae083)](https://www.codacy.com/app/GroupByInc/buffering-cache?utm_source=github.com&utm_medium=referral&utm_content=groupby/buffering-cache&utm_campaign=Badge_Coverage) | ||
![example workflow](https://github.com/groupby/buffering-cache/actions/workflows/node.js.yml/badge.svg) | ||
@@ -37,3 +34,3 @@ Cache a little cold? Cache misses slowing you down? | ||
// Optional: | ||
bufferTtlMsec: 500, // Buffer TTL in msec. Defaults to ttlMsec / 2. Must be gt 0 and lt ttlMsec | ||
bufferTtlMsec: 2500, // Buffer TTL in msec. Defaults to ttlMsec / 2. Must be gt 0 and lt ttlMsec | ||
localCacheSize: 0, // Local LRU cache size. Defaults to 0 (disabled). Must be gt 0. | ||
@@ -40,0 +37,0 @@ localTtlMsec: 500 // Local LRU cache TTL. Defaults to 500 ms, or bufferTtlMsec, whichever is less |
@@ -13,8 +13,12 @@ const chai = require('chai'); | ||
const redisClient = new Redis({ | ||
host: 'localhost', | ||
port: 6379, | ||
}); | ||
describe('buffering cache', () => { | ||
const redisClient = new Redis({ | ||
host: process.env['REDIS_HOST'], | ||
port: +process.env['REDIS_PORT'], | ||
}); | ||
describe('buffering cache', () => { | ||
after(() => { | ||
redisClient.disconnect(); | ||
}); | ||
it('fetch value from function and cache locally and in redis', (done) => { | ||
@@ -487,2 +491,36 @@ const redisCache = new RedisCache(redisClient); | ||
it('ttl gets a value by calling the function it is assigned', () => { | ||
const sampleConfig = { | ||
redisClient, | ||
ttlMsec: () => 600, | ||
bufferTtlMsec: 450, | ||
localCacheSize: 20, | ||
}; | ||
const sampleCache = new Cache(sampleConfig); | ||
expect(sampleCache.remoteCache.getTtl()).to.eql(600); | ||
}) | ||
it('ttl gets a non number value by calling the function it is assigned', () => { | ||
const sampleConfig = { | ||
redisClient, | ||
ttlMsec: () => 'abc', | ||
bufferTtlMsec: 450, | ||
localCacheSize: 20, | ||
}; | ||
expect(() => new Cache(sampleConfig)).to.throw(); | ||
}) | ||
it('ttl is a negative number', () => { | ||
const sampleConfig = { | ||
redisClient, | ||
ttlMsec: -1, | ||
bufferTtlMsec: 450, | ||
localCacheSize: 20, | ||
}; | ||
expect(() => new Cache(sampleConfig)).to.throw(); | ||
}) | ||
}); |
@@ -152,2 +152,38 @@ const Promise = require('bluebird'); | ||
}); | ||
it('accepts the ttl as a function', () => { | ||
expect(() => new Cache({ | ||
store: { | ||
get: (key) => {}, | ||
setpx: (key, value, ttl) => {}, | ||
delete: (key) => {}, | ||
client: {} | ||
}, | ||
ttl: () => { return 30 } | ||
})).not.to.throw(); | ||
}); | ||
it('throws error when the ttl as a function does not return number', () => { | ||
expect(() => new Cache({ | ||
store: { | ||
get: (key) => {}, | ||
setpx: (key, value, ttl) => {}, | ||
delete: (key) => {}, | ||
client: {} | ||
}, | ||
ttl: () => { return 'abc' } | ||
}).getTtl()).to.throw(); | ||
}); | ||
it('throws error when the ttl is negative', () => { | ||
expect(() => new Cache({ | ||
store: { | ||
get: (key) => {}, | ||
setpx: (key, value, ttl) => {}, | ||
delete: (key) => {}, | ||
client: {} | ||
}, | ||
ttl: -1 | ||
}).getTtl()).to.throw(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances 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
73572
8
1707
15
66
2
Updatedlodash@^4.17.21