Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

memoize-cache

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memoize-cache - npm Package Compare versions

Comparing version 5.0.1 to 5.0.2

.eslintrc.js

135

base-cache.js

@@ -1,5 +0,18 @@

var keyGetter = require('memoize-cache-utils/key-getter');
var keysGetter = require('memoize-cache-utils/keys-getter');
var callbackify = require('async-deco/utils/callbackify');
var keyGetter = require('memoize-cache-utils/key-getter')
var keysGetter = require('memoize-cache-utils/keys-getter')
function callbackify (func) {
return function _callbackify () {
// get arguments
var context = this
var args = Array.prototype.slice.call(arguments, 0, -1)
var cb = arguments[arguments.length - 1]
try {
var output = func.apply(context, args)
} catch (e) {
return cb(e)
}
cb(null, output)
}
}
/*

@@ -11,49 +24,55 @@

function BaseCache(opts) {
this.opts = opts = opts || {};
this.getCacheKey = keyGetter(opts.key);
this.getTags = keysGetter(opts.tags);
this.getMaxAge = typeof opts.maxAge !== 'function' ? function () { return opts.maxAge; } : opts.maxAge;
function BaseCache (opts) {
this.opts = opts = opts || {}
this.getCacheKey = keyGetter(opts.key)
this.getTags = keysGetter(opts.tags)
this.getMaxAge = typeof opts.maxAge !== 'function' ? function () { return opts.maxAge } : opts.maxAge
this.serialize = opts.serializeAsync || (opts.serialize && callbackify(opts.serialize)) || function (v, cb) { cb(null, v); };
this.deserialize = opts.deserializeAsync || (opts.deserialize && callbackify(opts.deserialize)) || function (v, cb) { cb(null, v); };
this.serialize = opts.serializeAsync || (opts.serialize && callbackify(opts.serialize)) || function (v, cb) { cb(null, v) }
this.deserialize = opts.deserializeAsync || (opts.deserialize && callbackify(opts.deserialize)) || function (v, cb) { cb(null, v) }
this.maxValidity = typeof opts.maxValidity === 'undefined' ?
function () {return Infinity;} :
(typeof opts.maxValidity === 'function' ? opts.maxValidity : function () {return opts.maxValidity;});
this.maxValidity = typeof opts.maxValidity === 'undefined'
? function () { return Infinity }
: (typeof opts.maxValidity === 'function' ? opts.maxValidity : function () { return opts.maxValidity })
}
BaseCache.prototype.push = function cache_push(args, output, next) {
next = next || function () {}; // next is optional
var serialize = this.serialize;
var set = this._set.bind(this);
BaseCache.prototype.push = function cachePush (args, output, next) {
next = next || function () {} // next is optional
var k = this.getCacheKey.apply(this, args);
var tags = this.getTags.apply(this, args);
var maxAge = this.getMaxAge.call(this, args, output); // undefined === forever
var maxValidity = (this.maxValidity.call(this, args, output) * 1000) + Date.now();
var k = this.getCacheKey.apply(this, args)
var tags = this.getTags.apply(this, args)
var maxAge = this.getMaxAge(args, output) // undefined === forever
var maxValidity = (this.maxValidity(args, output) * 1000) + Date.now()
if (k === null || maxAge === 0) {
next();
return;
next()
return
}
var keys = { key: k, tags: tags };
var keys = { key: k, tags: tags }
this.set(keys, maxValidity, maxAge, output, next)
return keys
}
BaseCache.prototype.set = function cacheSet (keys, maxValidity, maxAge, output, next) {
next = next || function () {} // next is optional
var set = this._set.bind(this)
var serialize = this.serialize
serialize(output, function (err, data) {
var jsonData, payload;
var payload
if (err) {
return next(err);
return next(err)
}
payload = { data: data, maxValidity: maxValidity };
set(keys, payload, maxAge, next);
});
return keys;
};
payload = { data: data, maxValidity: maxValidity }
set(keys, payload, maxAge, next)
})
}
BaseCache.prototype.query = function cache_query(args, next) {
var t0 = Date.now();
var key = this.getCacheKey.apply(this, args);
var deserialize = this.deserialize;
var get = this._get.bind(this);
var obj;
BaseCache.prototype.query = function cacheQuery (args, next) {
var t0 = Date.now()
var key = this.getCacheKey.apply(this, args)
var deserialize = this.deserialize
var get = this._get.bind(this)

@@ -66,9 +85,9 @@ if (key === null) {

key: key
});
})
}
get(key, function (err, payload) {
var data, maxValidity;
var data, maxValidity
if (err) {
return next(err);
return next(err)
}

@@ -80,9 +99,9 @@ if (!payload) {

key: key
});
})
}
maxValidity = payload.maxValidity;
data = payload.data;
maxValidity = payload.maxValidity
data = payload.data
deserialize(data, function (err, output) {
if (err) {
return next(err);
return next(err)
}

@@ -95,19 +114,19 @@ next(null, {

stale: Boolean(maxValidity && maxValidity < Date.now())
});
});
});
};
})
})
})
}
BaseCache.prototype.purgeByKeys = function cache_purgeByKeys(keys, next) {
throw new Error('Not implemented');
};
BaseCache.prototype.purgeByKeys = function cachePurgeByKeys (keys, next) {
throw new Error('Not implemented')
}
BaseCache.prototype.purgeByTags = function cache_purgeByTags(keys, next) {
throw new Error('Not implemented');
};
BaseCache.prototype.purgeByTags = function cachePurgeByTags (keys, next) {
throw new Error('Not implemented')
}
BaseCache.prototype.purgeAll = function cache_purgeAll(keys, next) {
throw new Error('Not implemented');
};
BaseCache.prototype.purgeAll = function cachePurgeAll (keys, next) {
throw new Error('Not implemented')
}
module.exports = BaseCache;
module.exports = BaseCache

@@ -1,86 +0,85 @@

var BaseCache = require('./base-cache');
var LRU = require('little-ds-toolkit/lib/lru-cache');
var Tags = require('./tags');
var BaseCache = require('./base-cache')
var LRU = require('little-ds-toolkit/lib/lru-cache')
var Tags = require('./tags')
function CacheRam(opts) {
BaseCache.call(this, opts);
this._maxLen = this.opts.maxLen;
this._maxSize = this.opts.maxSize;
this._reset();
function CacheRam (opts) {
BaseCache.call(this, opts)
this._maxLen = this.opts.maxLen
this._maxSize = this.opts.maxSize
this._reset()
}
CacheRam.prototype = Object.create(BaseCache.prototype);
CacheRam.prototype.constructor = CacheRam;
CacheRam.prototype = Object.create(BaseCache.prototype)
CacheRam.prototype.constructor = CacheRam
CacheRam.prototype._reset = function cache__reset() {
var tags = this.tags = new Tags();
CacheRam.prototype._reset = function _cacheReset () {
var tags = this.tags = new Tags()
var onDelete = function (item) {
var key = item.key;
tags.removeKey(key);
};
this.cache = new LRU({ maxSize: this._maxSize, maxLen: this._maxLen, onDelete: onDelete });
};
var key = item.key
tags.removeKey(key)
}
this.cache = new LRU({ maxSize: this._maxSize, maxLen: this._maxLen, onDelete: onDelete })
}
CacheRam.prototype._set = function cache__set(keys, payload, maxAge, next) {
var k = keys.key;
var tags = keys.tags;
CacheRam.prototype._set = function _cacheSet (keys, payload, maxAge, next) {
var k = keys.key
var tags = keys.tags
try {
this.cache.set(k, payload, maxAge * 1000);
this.tags.add(k, tags);
next();
this.cache.set(k, payload, maxAge * 1000)
this.tags.add(k, tags)
next()
} catch (e) {
next(e);
next(e)
}
};
}
CacheRam.prototype._get = function cache__get(key, next) {
var hit;
CacheRam.prototype._get = function _cacheGet (key, next) {
try {
next(null, this.cache.get(key));
next(null, this.cache.get(key))
} catch (e) {
next(e);
next(e)
}
};
}
CacheRam.prototype.purgeAll = function cache__purgeAll(next) {
next = next || function () {};
CacheRam.prototype.purgeAll = function cachePurgeAll (next) {
next = next || function () {}
try {
this._reset();
this._reset()
} catch (e) {
return next(e);
return next(e)
}
next();
};
next()
}
CacheRam.prototype.purgeByKeys = function cache__purgeKeys(keys, next) {
next = next || function () {};
keys = Array.isArray(keys) ? keys : [keys];
CacheRam.prototype.purgeByKeys = function cachePurgeKeys (keys, next) {
next = next || function () {}
keys = Array.isArray(keys) ? keys : [keys]
try {
for (var i = 0; i < keys.length; i++) {
this.cache.del(keys[i]);
this.tags.removeKey(keys[i]);
this.cache.del(keys[i])
this.tags.removeKey(keys[i])
}
next();
next()
} catch (e) {
next(e);
next(e)
}
};
}
CacheRam.prototype.purgeByTags = function cache__purgeTags(tags, next) {
next = next || function () {};
tags = Array.isArray(tags) ? tags : [tags];
var keys;
CacheRam.prototype.purgeByTags = function cachePurgeTags (tags, next) {
next = next || function () {}
tags = Array.isArray(tags) ? tags : [tags]
var keys
try {
for (var i = 0; i < tags.length; i++) {
keys = this.tags.getKeys(tags[i]);
this.purgeByKeys(keys);
keys = this.tags.getKeys(tags[i])
this.purgeByKeys(keys)
// not calling this.tags.removeTag(tags[i]); as this.tags.removeKey(keys[i]) should suffice
}
next();
next()
} catch (e) {
next(e);
next(e)
}
};
}
module.exports = CacheRam;
module.exports = CacheRam

@@ -1,6 +0,6 @@

var CacheRAM = require('./cache-ram');
var NoCache = require('./no-cache');
var BaseCache = require('./base-cache');
var keyGetter = require('memoize-cache-utils/key-getter');
var keysGetter = require('memoize-cache-utils/keys-getter');
var CacheRAM = require('./cache-ram')
var NoCache = require('./no-cache')
var BaseCache = require('./base-cache')
var keyGetter = require('memoize-cache-utils/key-getter')
var keysGetter = require('memoize-cache-utils/keys-getter')

@@ -12,3 +12,3 @@ module.exports = {

keyGetter: keyGetter,
keysGetter: keysGetter,
};
keysGetter: keysGetter
}

@@ -6,15 +6,15 @@ /*

*/
var keyGetter = require('memoize-cache-utils/key-getter');
var keyGetter = require('memoize-cache-utils/key-getter')
function Cache(opts) {
opts = opts || {};
this.getCacheKey = keyGetter(opts.key);
function Cache (opts) {
opts = opts || {}
this.getCacheKey = keyGetter(opts.key)
}
Cache.prototype.push = function cache_push(args, output) {};
Cache.prototype.push = function cachePush (args, output) {}
Cache.prototype.query = function cache_query(args, next) {
var key = this.getCacheKey.apply(this, args);
Cache.prototype.query = function cacheQuery (args, next) {
var key = this.getCacheKey.apply(this, args)
if (key === '__error__') {
return next(new Error('Error test'));
return next(new Error('Error test'))
}

@@ -25,5 +25,5 @@

key: key
});
};
})
}
module.exports = Cache;
module.exports = Cache
{
"name": "memoize-cache",
"version": "5.0.1",
"version": "5.0.2",
"description": "A cache support for memoized functions",
"main": "index.js",
"scripts": {
"test": "mocha tests/**/*.js",
"test": "mocha",
"watch": "npm run test -- -w",
"lint": "./node_modules/.bin/eslint --ext .js ./src ./tests",
"release:major": "./node_modules/.bin/npm-release major",
"release:minor": "./node_modules/.bin/npm-release minor",
"release:patch": "./node_modules/.bin/npm-release patch",
"lint": "eslint . --fix",
"precommit": "npm run lint",

@@ -25,10 +22,14 @@ "prepush": "npm run test"

"chai": "^1.10.0",
"eslint": "^1.10.3",
"eslint": "^5.8.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"husky": "^0.10.2",
"lzma-purejs": "^0.9.3",
"mocha": "^2.1.0",
"mocha": "^5.2.0",
"npm-release": "^1.0.0"
},
"dependencies": {
"async-deco": "^7.6.0",
"little-ds-toolkit": "0.4.0",

@@ -35,0 +36,0 @@ "memoize-cache-utils": "^0.1.1"

@@ -56,2 +56,3 @@ memoize-cache

```
This function signature is designed to simplify caching of a function call. So it takes the arguments used for the call and the output.
"args" is an array containing the arguments passed to the function that generated the output.

@@ -64,2 +65,15 @@ This function is a "fire and forget" caching request. So there is no need of waiting for an answer, but if you want you can use a callback as third argument.

Pushing a new cached value (2)
------------------------------
This function caches a value. It is as back door to push data in the cache.
```js
cache.set(keys, maxValidity, maxAge, data, next);
```
* keys is an object: { key: 'thisisthekey', tags: ['tag1', 'tag2'] }
* maxValidity is a timestamp for considering the data stale. For example: 10000 + Date.now(). Ten second from now.
* maxAge: after this number of milliseconds the data will be removed from the cache
* data: the data to cache. Not serialised
* next: optional callback
Querying for cache hit

@@ -66,0 +80,0 @@ ----------------------

@@ -1,7 +0,7 @@

function Bucket(arr) {
arr = arr || [];
this.data = {};
this.len = 0;
function Bucket (arr) {
arr = arr || []
this.data = {}
this.len = 0
for (var i = 0, len = arr.length; i < len; i++) {
this.add(arr[i]);
this.add(arr[i])
}

@@ -11,69 +11,69 @@ };

Bucket.prototype.has = function (key) {
return key in this.data;
};
return key in this.data
}
Bucket.prototype.add = function (key) {
this.len++;
this.data[key] = true;
};
this.len++
this.data[key] = true
}
Bucket.prototype.del = function (key) {
if (this.has(key)) {
delete this.data[key];
this.len--;
delete this.data[key]
this.len--
}
};
}
Bucket.prototype.toArray = function () {
return Object.keys(this.data);
};
return Object.keys(this.data)
}
function Tags() {
this.keysToTags = {};
this.tagsToKeys = {};
function Tags () {
this.keysToTags = {}
this.tagsToKeys = {}
}
Tags.prototype.add = function (key, tags) {
tags = tags || [];
var tag;
this.keysToTags[key] = new Bucket(tags);
tags = tags || []
var tag
this.keysToTags[key] = new Bucket(tags)
for (var i = 0; i < tags.length; i++) {
tag = tags[i];
tag = tags[i]
if (!(tag in this.tagsToKeys)) {
this.tagsToKeys[tag] = new Bucket();
this.tagsToKeys[tag] = new Bucket()
}
this.tagsToKeys[tag].add(key);
this.tagsToKeys[tag].add(key)
}
};
}
Tags.prototype.getTags = function (key) {
return key in this.keysToTags ? this.keysToTags[key].toArray() : [];
};
return key in this.keysToTags ? this.keysToTags[key].toArray() : []
}
Tags.prototype.getKeys = function (tag) {
return tag in this.tagsToKeys ? this.tagsToKeys[tag].toArray() : [];
};
return tag in this.tagsToKeys ? this.tagsToKeys[tag].toArray() : []
}
Tags.prototype.removeKey = function (key) {
var tags = this.getTags(key);
var tags = this.getTags(key)
for (var i = 0; i < tags.length; i++) {
this.tagsToKeys[tags[i]].del(key);
this.tagsToKeys[tags[i]].del(key)
if (this.tagsToKeys[tags[i]].len === 0) {
delete this.tagsToKeys[tags[i]];
delete this.tagsToKeys[tags[i]]
}
}
delete this.keysToTags[key];
};
delete this.keysToTags[key]
}
Tags.prototype.removeTag = function (tag) {
var keys = this.getKeys(tag);
var keys = this.getKeys(tag)
for (var i = 0; i < keys.length; i++) {
this.keysToTags[keys[i]].del(tag);
this.keysToTags[keys[i]].del(tag)
if (this.keysToTags[keys[i]].len === 0) {
delete this.keysToTags[keys[i]];
delete this.keysToTags[keys[i]]
}
}
delete this.tagsToKeys[tag];
};
delete this.tagsToKeys[tag]
}
module.exports = Tags;
module.exports = Tags
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc