sequelize-simple-cache
Advanced tools
Comparing version 1.0.0-beta.17 to 1.0.1
{ | ||
"name": "sequelize-simple-cache", | ||
"version": "1.0.0-beta.17", | ||
"version": "1.0.1", | ||
"description": "A simple, transparent, client-side, in-memory cache for Sequelize", | ||
@@ -37,11 +37,11 @@ "main": "src/SequelizeSimpleCache.js", | ||
"coveralls": "^3.0.3", | ||
"eslint": "^5.15.3", | ||
"eslint": "^5.16.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-import": "^2.17.2", | ||
"eslint-plugin-promise": "^4.1.1", | ||
"eslint-plugin-should-promised": "^2.0.0", | ||
"mocha": "^6.0.2", | ||
"nyc": "^13.3.0", | ||
"sequelize": "^5.2.1", | ||
"sinon": "^7.3.1", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.0.0", | ||
"sequelize": "^5.8.2", | ||
"sinon": "^7.3.2", | ||
"sinon-chai": "^3.3.0" | ||
@@ -48,0 +48,0 @@ }, |
@@ -22,6 +22,6 @@ # sequelize-simple-cache | ||
In a project, we had a couple of database tables that were holding a sort of system configuration. | ||
Something like 4 or 5 tables with some 50 rows of data. | ||
In a project, we had a couple of database tables with a sort of configuration. | ||
Something like 4 or 5 tables with some 10 rows of data. | ||
Nearly every request needed this data, i.e., it was read all the time. | ||
But updated only very rarely, once a day maybe. | ||
But updated only very rarely, e.g, once a day. | ||
So, pre-fetching or simple in-memory caching would work for us. | ||
@@ -87,2 +87,16 @@ | ||
### Time-to-live (ttl) | ||
Each model has its individual time-to-live (ttl), i.e., | ||
all database requests on a model are cached for a particular number of seconds. | ||
Default is one hour. | ||
For eternal caching, i.e., no automatic cache invalidation, simply set the model's `ttl` to `false` (or any number less or equals `0`). | ||
```javascript | ||
const cache = new SequelizeSimpleCache({ | ||
User: { ttl: 5 * 60 }, // 5 minutes | ||
Page: { }, // default ttl is 1 hour | ||
Foo: { ttl: false } // cache forever | ||
}); | ||
``` | ||
### Clear cache | ||
@@ -89,0 +103,0 @@ |
@@ -100,3 +100,3 @@ const md5 = require('md5'); | ||
const { data, expires } = item; | ||
if (expires > Date.now()) { | ||
if (!expires || expires > Date.now()) { | ||
this.log('hit', { key, hash, expires }); | ||
@@ -111,3 +111,3 @@ return data; // resolve from cache | ||
if (data !== undefined && data !== null) { | ||
const expires = Date.now() + ttl * 1000; | ||
const expires = ttl > 0 ? Date.now() + ttl * 1000 : false; | ||
cache.set(hash, { data, expires }); | ||
@@ -158,3 +158,3 @@ this.log('load', { key, hash, expires }); | ||
cache.forEach(({ expires }, hash) => { | ||
if (expires <= now) { | ||
if (expires && expires <= now) { | ||
cache.delete(hash); | ||
@@ -161,0 +161,0 @@ this.log('purge', { hash, expires }); |
@@ -19,5 +19,10 @@ const chai = require('chai'); | ||
let stubConsoleDebug; | ||
let stubDateNow; | ||
let nowOffset = 0; | ||
beforeEach(() => { | ||
stubConsoleDebug = sinon.stub(console, 'debug'); | ||
nowOffset = 0; // reset | ||
const now = Date.now(); | ||
stubDateNow = sinon.stub(Date, 'now').callsFake(() => now + nowOffset); | ||
}); | ||
@@ -27,2 +32,3 @@ | ||
stubConsoleDebug.restore(); // eslint-disable-line no-console | ||
stubDateNow.restore(); | ||
}); | ||
@@ -219,3 +225,3 @@ | ||
const result2 = await User.findOne({ where: { username: 'fred' } }); | ||
await new Promise(resolve => setTimeout(() => resolve(), 1200)); | ||
nowOffset = 1200; | ||
const result3 = await User.findOne({ where: { username: 'fred' } }); | ||
@@ -228,2 +234,18 @@ expect(stub.calledTwice).to.be.true; | ||
it('should cache forever', async () => { | ||
const stub = sinon.stub().resolves({ username: 'fred' }); | ||
const model = { | ||
name: 'User', | ||
findOne: stub, | ||
}; | ||
const cache = new SequelizeSimpleCache({ User: { ttl: false } }, { ops: false }); | ||
const User = cache.init(model); | ||
const result1 = await User.findOne({ where: { username: 'fred' } }); | ||
nowOffset = 999999999; | ||
const result2 = await User.findOne({ where: { username: 'fred' } }); | ||
expect(stub.calledOnce).to.be.true; | ||
expect(result1).to.be.deep.equal({ username: 'fred' }); | ||
expect(result2).to.be.deep.equal({ username: 'fred' }); | ||
}); | ||
it('should not cache a value of `null`', async () => { | ||
@@ -495,7 +517,4 @@ const stub = sinon.stub().resolves(null); | ||
await User.findOne({ where: { username: 'john' } }); | ||
await new Promise(resolve => setTimeout(() => resolve(), 16)); | ||
await User.findOne({ where: { username: 'jim' } }); | ||
await new Promise(resolve => setTimeout(() => resolve(), 16)); | ||
await User.findOne({ where: { username: 'bob' } }); | ||
await new Promise(resolve => setTimeout(() => resolve(), 16)); | ||
expect(cache.size()).to.be.equal(3); | ||
@@ -502,0 +521,0 @@ await User.findOne({ where: { username: 'ron' } }); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
36267
688
1
189