sequelize-simple-cache
Advanced tools
Comparing version 1.0.0-beta.10 to 1.0.0-beta.11
{ | ||
"name": "sequelize-simple-cache", | ||
"version": "1.0.0-beta.10", | ||
"version": "1.0.0-beta.11", | ||
"description": "A simple, transparent, client-side, in-memory cache for Sequelize", | ||
@@ -5,0 +5,0 @@ "main": "src/SequelizeSimpleCache.js", |
@@ -105,5 +105,7 @@ # sequelize-simple-cache | ||
### Debug output | ||
### Logging | ||
You can activate debug output to `console.debug()` like this: | ||
There is "debug" and "ops" logging -- both are off by default. | ||
Logging goes to `console.debug()` unless you set `delegate` to log somewhere else. | ||
`event` is one of: `init`, `hit`, `miss`, `load` or `ops`. | ||
```javascript | ||
@@ -114,2 +116,4 @@ const cache = new SequelizeSimpleCache({ | ||
debug: true, | ||
ops: 60, // seconds | ||
delegate: (event, details) => { ... }, | ||
}); | ||
@@ -116,0 +120,0 @@ ``` |
@@ -17,9 +17,16 @@ const md5 = require('md5'); | ||
const { | ||
debug = false, // eslint-disable-next-line no-console | ||
debug = false, | ||
ops = false, // eslint-disable-next-line no-console | ||
delegate = (event, details) => console.debug(`CACHE ${event.toUpperCase()}`, details), | ||
} = options; | ||
this.debug = debug; | ||
this.ops = ops; | ||
this.delegate = delegate; | ||
this.cache = new Map(); | ||
this.stats = { hit: 0, miss: 0, load: 0 }; | ||
if (this.ops > 0) { | ||
this.heart = setInterval(() => { | ||
this.log('ops'); | ||
}, this.ops * 1000); | ||
} | ||
} | ||
@@ -100,3 +107,3 @@ | ||
log(event, details) { | ||
log(event, details = {}) { | ||
// stats | ||
@@ -106,13 +113,10 @@ if (['hit', 'miss', 'load'].includes(event)) { | ||
} | ||
// debug logging | ||
if (!this.debug) return; | ||
const out = { | ||
// logging | ||
if (!this.debug && ['init', 'hit', 'miss', 'load'].includes(event)) return; | ||
this.delegate(event, { | ||
...details, | ||
stats: { | ||
...this.stats, | ||
ratio: this.stats.hit / (this.stats.hit + this.stats.miss), | ||
}, | ||
...this.stats, | ||
ratio: this.stats.hit / (this.stats.hit + this.stats.miss), | ||
size: this.cache.size, | ||
}; | ||
this.delegate(event, out); | ||
}); | ||
} | ||
@@ -119,0 +123,0 @@ } |
@@ -29,11 +29,11 @@ const chai = require('chai'); | ||
it('should create cache without crashing / no args', () => { | ||
expect(() => new SequelizeSimpleCache()).to.not.throw(); | ||
expect(() => new SequelizeSimpleCache({}, { ops: false })).to.not.throw(); | ||
}); | ||
it('should create cache without crashing / empty args', () => { | ||
expect(() => new SequelizeSimpleCache({})).to.not.throw(); | ||
expect(() => new SequelizeSimpleCache({}, { ops: false })).to.not.throw(); | ||
}); | ||
it('should create cache without crashing / dummy model', () => { | ||
expect(() => new SequelizeSimpleCache({ User: {} })).to.not.throw(); | ||
expect(() => new SequelizeSimpleCache({ User: {} }, { ops: false })).to.not.throw(); | ||
}); | ||
@@ -77,3 +77,3 @@ | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -91,3 +91,3 @@ expect(User).to.have.property('noCache').which.is.a('function'); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -107,3 +107,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -128,3 +128,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {}, Page: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {}, Page: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -147,3 +147,3 @@ const Page = cache.init(model2); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -164,3 +164,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -181,7 +181,7 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: { ttl: 1 } }); | ||
const cache = new SequelizeSimpleCache({ User: { ttl: 1 } }, { ops: false }); | ||
const User = cache.init(model); | ||
const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
const result2 = await User.findOne({ where: { username: 'fred' } }); | ||
await new Promise(resolve => setTimeout(() => resolve(), 1000)); | ||
await new Promise(resolve => setTimeout(() => resolve(), 1200)); | ||
const result3 = await User.findOne({ where: { username: 'fred' } }); | ||
@@ -200,3 +200,3 @@ expect(stub.calledTwice).to.be.true; | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -216,3 +216,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -232,3 +232,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: { methods: ['findFoo'] } }); | ||
const cache = new SequelizeSimpleCache({ User: { methods: ['findFoo'] } }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -249,3 +249,3 @@ const result1 = await User.findFoo({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: { methods: ['findFoo'] } }); | ||
const cache = new SequelizeSimpleCache({ User: { methods: ['findFoo'] } }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -265,3 +265,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -283,3 +283,3 @@ try { | ||
}; | ||
const cache = new SequelizeSimpleCache({ Foo: {} }); | ||
const cache = new SequelizeSimpleCache({ Foo: {} }, { ops: false }); | ||
const User = cache.init(model); // TODO: should this issue a warning? | ||
@@ -299,3 +299,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -317,3 +317,3 @@ const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }, { debug: true }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { debug: true, ops: false }); | ||
cache.init(model); | ||
@@ -329,3 +329,3 @@ expect(stubConsoleDebug.called).to.be.true; | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
cache.init(model); | ||
@@ -335,2 +335,26 @@ expect(stubConsoleDebug.called).to.be.false; | ||
it('should print ops output if ops>0', async () => { | ||
const stub = sinon.stub().resolves({ username: 'fred' }); | ||
const model = { | ||
name: 'User', | ||
findOne: stub, | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }, { debug: false, ops: 1 }); | ||
cache.init(model); | ||
await new Promise(resolve => setTimeout(() => resolve(), 1200)); | ||
clearInterval(cache.heart); | ||
expect(stubConsoleDebug.called).to.be.true; | ||
}); | ||
it('should not print ops output if ops=false', async () => { | ||
const stub = sinon.stub().resolves({ username: 'fred' }); | ||
const model = { | ||
name: 'User', | ||
findOne: stub, | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }, { debug: false, ops: false }); | ||
cache.init(model); | ||
expect(stubConsoleDebug.called).to.be.false; | ||
}); | ||
it('should work to stub model using Sinon in unit tests / pattern 1', async () => { | ||
@@ -341,3 +365,3 @@ const model = { | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -358,3 +382,3 @@ const stub = sinon.stub(User, 'findOne').resolves({ username: 'foo' }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -375,3 +399,3 @@ sinon.stub(User, 'findOne').resolves({ username: 'foo' }); | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: {} }); | ||
const cache = new SequelizeSimpleCache({ User: {} }, { ops: false }); | ||
const User = cache.init(model); | ||
@@ -378,0 +402,0 @@ sinon.stub(User, 'findOne').returns({ username: 'foo' }); // should be `resolves` |
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
26783
471
147