sequelize-simple-cache
Advanced tools
Comparing version 1.0.0-beta.9 to 1.0.0-beta.10
{ | ||
"name": "sequelize-simple-cache", | ||
"version": "1.0.0-beta.9", | ||
"version": "1.0.0-beta.10", | ||
"description": "A simple, transparent, client-side, in-memory cache for Sequelize", | ||
@@ -5,0 +5,0 @@ "main": "src/SequelizeSimpleCache.js", |
@@ -26,3 +26,3 @@ const md5 = require('md5'); | ||
static stringify(obj) { | ||
static key(obj) { | ||
// Unfortunately, there seam to be no stringifyers or object hashers that work correctly | ||
@@ -34,6 +34,2 @@ // with ES6 symbols and function objects. But this is important for Sequelize queries. | ||
static hash(obj) { | ||
return md5(SequelizeSimpleCache.stringify(obj)); | ||
} | ||
init(model) { // Sequelize model object | ||
@@ -59,3 +55,4 @@ const { name: type } = model; | ||
const fn = async (...args) => { | ||
const hash = SequelizeSimpleCache.hash({ type, prop, args }); | ||
const key = SequelizeSimpleCache.key({ type, prop, args }); | ||
const hash = md5(key); | ||
const item = this.cache.get(hash); | ||
@@ -65,11 +62,7 @@ if (item) { // hit | ||
if (expires > Date.now()) { | ||
this.log('hit', { | ||
type, method: prop, args, hash, data, expires, | ||
}); | ||
this.log('hit', { key, hash, expires }); | ||
return data; // resolve from cache | ||
} | ||
} | ||
this.log('miss', { | ||
type, method: prop, args, hash, | ||
}); | ||
this.log('miss', { key, hash }); | ||
const promise = target[prop](...args); | ||
@@ -81,5 +74,3 @@ assert(promise.then, `${type}.${prop}() did not return a promise but should`); | ||
this.cache.set(hash, { data, expires, type }); | ||
this.log('load', { | ||
type, method: prop, args, hash, data, expires, | ||
}); | ||
this.log('load', { key, hash, expires }); | ||
} | ||
@@ -120,12 +111,10 @@ return data; // resolve from database | ||
if (!this.debug) return; | ||
const { args, data } = details; | ||
const out = details; | ||
if (args) { | ||
out.args = SequelizeSimpleCache.stringify(args); | ||
} | ||
if (data) { | ||
out.data = JSON.stringify(data); | ||
} | ||
out.stats = { ...this.stats, ratio: this.stats.hit / (this.stats.hit + this.stats.miss) }; | ||
out.size = this.cache.size; | ||
const out = { | ||
...details, | ||
stats: { | ||
...this.stats, | ||
ratio: this.stats.hit / (this.stats.hit + this.stats.miss), | ||
}, | ||
size: this.cache.size, | ||
}; | ||
this.delegate(event, out); | ||
@@ -132,0 +121,0 @@ } |
@@ -6,2 +6,3 @@ const chai = require('chai'); | ||
const { Op, fn } = require('sequelize'); | ||
const md5 = require('md5'); | ||
const SequelizeSimpleCache = require('..'); | ||
@@ -62,4 +63,4 @@ | ||
const hashes2 = new Set(); | ||
queries.forEach(q => hashes.add(SequelizeSimpleCache.hash(q))); | ||
queries.forEach(q => hashes2.add(SequelizeSimpleCache.hash(q))); | ||
queries.forEach(q => hashes.add(md5(SequelizeSimpleCache.key(q)))); | ||
queries.forEach(q => hashes2.add(md5(SequelizeSimpleCache.key(q)))); | ||
const union = new Set([...hashes, ...hashes2]); | ||
@@ -66,0 +67,0 @@ expect(hashes.size).to.be.equal(queries.length); |
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
25263
445