Socket
Socket
Sign inDemoInstall

lru-cache-for-clusters-as-promised

Package Overview
Dependencies
7
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

44

lru-cache-for-clusters-as-promised.js

@@ -47,3 +47,3 @@ /**

// constructor request
case '()':
case '()': {
// create a new lru-cache, give it a namespace, and save it locally

@@ -54,5 +54,21 @@ lru = caches[request.namespace] = new LRUCache(...request.arguments);

break;
}
case 'decr':
case 'incr': {
// get the current value
let value = lru.get(request.arguments[0]);
// maybe initialize and increment
value = (typeof value === 'number' ? value : 0) +
((request.arguments[1] || 1) * (request.func === 'decr' ? -1 : 1));
// set the new value
lru.set(request.arguments[0], value);
// send the new value
sendResponse({
value,
});
break;
}
// return the property value
case 'length':
case 'itemCount':
case 'itemCount': {
sendResponse({

@@ -62,4 +78,5 @@ value: lru[request.func],

break;
}
// return the function value
default:
default: {
sendResponse({

@@ -69,2 +86,3 @@ value: lru[request.func](...request.arguments),

break;
}
}

@@ -122,9 +140,23 @@ });

switch (func) {
case 'decr':
case 'incr': {
// get the current value default to 0
let value = lru.get(funcArgs[0]);
// maybe initialize and increment
value = (typeof value === 'number' ? value : 0) +
((funcArgs[1] || 1) * (func === 'decr' ? -1 : 1));
// set the new value
lru.set(funcArgs[0], value);
// resolve the new value
return Promise.resolve(value);
}
case 'itemCount':
case 'length':
case 'length': {
// return the property value
return Promise.resolve(lru[func]);
default:
}
default: {
// just call the function on the lru-cache
return Promise.resolve(lru[func](...funcArgs));
}
}

@@ -177,2 +209,4 @@ }

has: key => promiseTo('has', key),
incr: (key, amount) => promiseTo('incr', key, amount),
decr: (key, amount) => promiseTo('decr', key, amount),
reset: () => promiseTo('reset'),

@@ -179,0 +213,0 @@ keys: () => promiseTo('keys'),

2

package.json
{
"name": "lru-cache-for-clusters-as-promised",
"version": "1.1.0",
"version": "1.2.0",
"description": "LRU Cache that is safe for clusters",

@@ -5,0 +5,0 @@ "main": "./lru-cache-for-clusters-as-promised.js",

@@ -25,13 +25,13 @@ # lru-cache-for-clusters-as-promised

* `namespace: string`, default `"default"`;
* the namespace for this cache on the master thread as it is not aware of the worker instances
* The namespace for this cache on the master thread as it is not aware of the worker instances.
* `timeout: integer`, default `100`.
* The amount of time in milliseconds that a worker will wait for a response from the master before rejecting the Promise.
* The amount of time in milliseconds that a worker will wait for a response from the master before rejecting the `Promise`.
* `failsafe: string`, default `resolve`.
* When a request times out the Promise will return `resolve(undefined)` by default, or with a value of `reject` the return will be `reject(Error)`.
* When a request times out the `Promise` will return `resolve(undefined)` by default, or with a value of `reject` the return will be `reject(Error)`.
* `max: number`
* the maximum items that can be stored in the cache
* The maximum items that can be stored in the cache
* `maxAge: milliseconds`
* the maximum age for an item to be considered valid
* The maximum age for an item to be considered valid
* `stale: true|false`
* when true expired items are return before they are removed rather than undefined
* When `true` expired items are return before they are removed rather than `undefined`

@@ -43,25 +43,29 @@ > ! note that `length` and `dispose` are missing as it is not possible to pass `functions` via IPC messages.

* `set(key, value)`
* sets a value for a key
* Sets a value for a key.
* `get(key)`
* returns a value for a key
* Returns a value for a key.
* `peek(key)`
* return the value for a key without updating its last access time
* Returns the value for a key without updating its last access time.
* `del(key)`
* remove a value from the cache
* Removes a value from the cache.
* `has(key)`
* returns true if the key exists in the cache
* Returns true if the key exists in the cache.
* `incr(key, [amount])`
* Increments a numeric key value by the `amount`, which defaults to `1`. More atomic in a clustered environment.
* `decr(key, [amount])`
* Decrements a numeric key value by the `amount`, which defaults to `1`. More atomic in a clustered environment.
* `reset()`
* removes all values from the cache
* Removes all values from the cache.
* `keys()`
* returns an array of all the cache keys
* Returns an array of all the cache keys.
* `values()`
* returns an array of all the cache values
* Returns an array of all the cache values.
* `dump()`
* returns a serialized array of the cache contents
* Returns a serialized array of the cache contents.
* `prune()`
* manually removes items from the cache rather than on get
* Manually removes items from the cache rather than on get.
* `length()`
* return the number of items in the cache
* Return the number of items in the cache.
* `itemCount()`
* return the number of items in the cache. same as `length()`.
* Return the number of items in the cache - same as `length()`.

@@ -71,3 +75,5 @@ # example usage

// require the module in your master thread that creates workers to initialize
const LRUCache = require('lru-cache-for-clusters-as-promised').init();
const LRUCache = require('lru-cache-for-clusters-as-promised');
LRUCache.init();
```

@@ -79,5 +85,7 @@

const cache = new LRUCache({
namespace: 'users',
max: 50,
stale: false,
namespace: 'users',
timeout: 100,
failsafe: 'resolve',
});

@@ -84,0 +92,0 @@

@@ -93,2 +93,58 @@ const request = require('supertest');

it('should incr(key)', (done) => {
// run the request
request(`http://${config.server.host}:${config.server.port}`)
.get('/incr')
.expect(200)
.end((err, response) => {
if (err) {
return done(err);
}
should(response.text).eql('[1,2]');
return done();
});
});
it('should incr(key, 2)', (done) => {
// run the request
request(`http://${config.server.host}:${config.server.port}`)
.get('/incr2')
.expect(200)
.end((err, response) => {
if (err) {
return done(err);
}
should(response.text).eql('[2,4]');
return done();
});
});
it('should decr(key)', (done) => {
// run the request
request(`http://${config.server.host}:${config.server.port}`)
.get('/decr')
.expect(200)
.end((err, response) => {
if (err) {
return done(err);
}
should(response.text).eql('[-1,-2]');
return done();
});
});
it('should decr(key, 2)', (done) => {
// run the request
request(`http://${config.server.host}:${config.server.port}`)
.get('/decr2')
.expect(200)
.end((err, response) => {
if (err) {
return done(err);
}
should(response.text).eql('[-2,-4]');
return done();
});
});
it('should add four keys and have the first fall out', (done) => {

@@ -95,0 +151,0 @@ // run the request

@@ -69,2 +69,56 @@ const http = require('http');

app.get('/incr', (req, res) => {
const values = [];
return cache.incr(config.args.one)
.then((value) => {
values.push(value);
return cache.incr(config.args.one);
})
.then((value) => {
values.push(value);
res.send(values);
});
});
app.get('/incr2', (req, res) => {
const values = [];
const amount = 2;
return cache.incr(config.args.one, amount)
.then((value) => {
values.push(value);
return cache.incr(config.args.one, amount);
})
.then((value) => {
values.push(value);
res.send(values);
});
});
app.get('/decr', (req, res) => {
const values = [];
return cache.decr(config.args.one)
.then((value) => {
values.push(value);
return cache.decr(config.args.one);
})
.then((value) => {
values.push(value);
res.send(values);
});
});
app.get('/decr2', (req, res) => {
const values = [];
const amount = 2;
return cache.decr(config.args.one, amount)
.then((value) => {
values.push(value);
return cache.decr(config.args.one, amount);
})
.then((value) => {
values.push(value);
res.send(values);
});
});
app.get('/one-two-three-four', (req, res) => {

@@ -71,0 +125,0 @@ cache.set(config.args.one, config.args.one)

@@ -42,2 +42,56 @@ const should = require('should');

it('should incr(key)', (done) => {
cache.incr(config.args.one)
.then((value) => {
should(value).eql(1);
return cache.incr(config.args.one);
})
.then((value) => {
should(value).eql(2);
return done();
})
.catch(err => done(err));
});
it('should incr(key, 2)', (done) => {
const amount = 2;
cache.incr(config.args.one, amount)
.then((value) => {
should(value).eql(2);
return cache.incr(config.args.one, amount);
})
.then((value) => {
should(value).eql(4);
return done();
})
.catch(err => done(err));
});
it('should decr(key)', (done) => {
cache.decr(config.args.one)
.then((value) => {
should(value).eql(-1);
return cache.decr(config.args.one);
})
.then((value) => {
should(value).eql(-2);
return done();
})
.catch(err => done(err));
});
it('should decr(key, 2)', (done) => {
const amount = 2;
cache.decr(config.args.one, amount)
.then((value) => {
should(value).eql(-2);
return cache.decr(config.args.one, amount);
})
.then((value) => {
should(value).eql(-4);
return done();
})
.catch(err => done(err));
});
it('should add four keys and have the first fall out', (done) => {

@@ -44,0 +98,0 @@ cache.set(config.args.one, config.args.one)

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc