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

ttl-mem-cache

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ttl-mem-cache - npm Package Compare versions

Comparing version 1.5.0 to 2.0.0

21

lib/cache.js

@@ -112,2 +112,23 @@ 'use strict';

dump() {
return Array.from(this.store.entries());
}
load(items) {
if (!Array.isArray(items)) {
throw new Error('Argument "items" is not an Array');
}
return items.map((item) => {
if (item[0] && item[1] && item[1].value && item[1].expires) {
this.store.set(item[0], item[1]);
return item[0];
}
return undefined;
}).filter(item => item);
}
length() {
return this.store.size;
}
_write(obj, enc, next) {

@@ -114,0 +135,0 @@ if (obj.key && obj.value) {

14

package.json
{
"name": "ttl-mem-cache",
"version": "1.5.0",
"version": "2.0.0",
"description": "A in memory time to live cache with streaming support.",

@@ -12,3 +12,4 @@ "main": "lib/cache.js",

"test": "tap test/*.js",
"lint": "eslint ."
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},

@@ -21,2 +22,3 @@ "repository": {

"cache",
"memcache",
"ttl",

@@ -33,7 +35,7 @@ "time to live",

"devDependencies": {
"eslint": "4.7.2",
"eslint-config-airbnb-base": "12.0.1",
"eslint-plugin-import": "2.7.0",
"eslint": "4.10.0",
"eslint-config-airbnb-base": "12.1.0",
"eslint-plugin-import": "2.8.0",
"benchmark": "2.1.4",
"lolex": "2.1.2",
"lolex": "2.1.3",
"tap": "10.7.2"

@@ -40,0 +42,0 @@ },

@@ -187,4 +187,24 @@ # ttl-mem-cache

### .dump()
Returns an Array of all items in the cache ready to be used by `.load()`.
### .load()
Loads an Array of items, provided by `.dump()`, into the cache. If any of
the items in the loaded Array contains a key which already are in the cache
the entry in the cache will be overwritten.
If any of the entries in the loaded Array are not compatible with the format
which `.dump()` exports, they will not be inserted into the cache.
Returns and Array with the keys which was inserted into the cache.
### .length()
The number of items in the cache.
## Events

@@ -191,0 +211,0 @@

@@ -451,3 +451,213 @@ 'use strict';

/**
* .dump()
*/
tap.test('cache.dump() - dump cache - should return Array with all entries', (t) => {
const cache = new Cache();
cache.set('a', 'bar');
cache.set('b', 'foo');
const dump = cache.dump();
t.true(Array.isArray(dump));
t.equal(dump.length, 2);
t.end();
});
tap.test('cache.dump() - entries in the dumped Array - should be an Array with the "key" as first item and "value" as second item', (t) => {
const cache = new Cache();
cache.set('a', 'bar');
cache.set('b', 'foo');
const dump = cache.dump();
t.equal(dump[0].length, 2);
t.equal(dump[1].length, 2);
t.type(dump[0][0], 'string');
t.type(dump[1][0], 'string');
t.type(dump[0][1], 'object');
t.type(dump[1][1], 'object');
t.end();
});
tap.test('cache.dump() - dumped entries - should have "value" and "expires" attributes', (t) => {
const cache = new Cache();
cache.set('a', 'bar');
cache.set('b', 'foo');
const dump = cache.dump();
t.equal(dump[0][1].value, 'bar');
t.equal(dump[1][1].value, 'foo');
t.type(dump[0][1].expires, 'number');
t.type(dump[1][1].expires, 'number');
t.end();
});
/**
* .load()
*/
tap.test('cache.load() - load invalid value to "items" argument - should throw', (t) => {
const cache = new Cache();
t.throws(() => {
cache.load('fail');
}, new Error('Argument "items" is not an Array'));
t.end();
});
tap.test('cache.load() - load entries - should set entries in cache', (t) => {
const clock = lolex.install();
const dump = [
['a', { value: 'bar', expires: 2000 }],
['b', { value: 'foo', expires: 2000 }],
];
const cache = new Cache();
cache.load(dump);
t.equal(cache.get('a'), 'bar');
t.equal(cache.get('b'), 'foo');
clock.uninstall();
t.end();
});
tap.test('cache.load() - load entries - should Array of keys inserted into cache', (t) => {
const dump = [
['a', { value: 'bar', expires: 2000 }],
['b', { value: 'foo', expires: 2000 }],
];
const cache = new Cache();
const arr = cache.load(dump);
t.equal(arr[0], 'a');
t.equal(arr[1], 'b');
t.end();
});
tap.test('cache.load() - one entry is missing "key" - should set valid entries in cache', (t) => {
const dump = [
['a', { value: 'bar', expires: 2000 }],
[{ value: 'foo', expires: 2000 }],
['c', { value: 'xyz', expires: 2000 }],
];
const cache = new Cache();
const arr = cache.load(dump);
t.equal(arr.length, 2);
t.equal(arr[0], 'a');
t.equal(arr[1], 'c');
t.end();
});
tap.test('cache.load() - one entry is missing "values" - should set valid entries in cache', (t) => {
const dump = [
['a', { value: 'bar', expires: 2000 }],
['b'],
['c', { value: 'xyz', expires: 2000 }],
];
const cache = new Cache();
const arr = cache.load(dump);
t.equal(arr.length, 2);
t.equal(arr[0], 'a');
t.equal(arr[1], 'c');
t.end();
});
tap.test('cache.load() - one entry is missing "values.value" - should set valid entries in cache', (t) => {
const dump = [
['a', { value: 'bar', expires: 2000 }],
['b', { expires: 2000 }],
['c', { value: 'xyz', expires: 2000 }],
];
const cache = new Cache();
const arr = cache.load(dump);
t.equal(arr.length, 2);
t.equal(arr[0], 'a');
t.equal(arr[1], 'c');
t.end();
});
tap.test('cache.load() - one entry is missing "values.expires" - should set valid entries in cache', (t) => {
const dump = [
['a', { value: 'bar', expires: 2000 }],
['b', { value: 'foo' }],
['c', { value: 'xyz', expires: 2000 }],
];
const cache = new Cache();
const arr = cache.load(dump);
t.equal(arr.length, 2);
t.equal(arr[0], 'a');
t.equal(arr[1], 'c');
t.end();
});
/**
* .dump() > .load()
*/
tap.test('cache.dump().load() - dump entries from one cache - should import into secondary cache', (t) => {
const cacheA = new Cache();
const cacheB = new Cache();
cacheA.set('a', 'bar');
cacheA.set('b', 'foo');
cacheB.load(cacheA.dump());
t.equal(cacheB.get('a'), 'bar');
t.equal(cacheB.get('b'), 'foo');
t.end();
});
/**
* .length()
*/
tap.test('cache.length() - have entries in cache - should return number of entries in cache', (t) => {
const cache = new Cache();
cache.set('a', 'bar');
cache.set('b', 'foo');
t.equal(cache.length(), 2);
t.end();
});
tap.test('cache.length() - no entries in cache - should return 0', (t) => {
const cache = new Cache();
t.equal(cache.length(), 0);
t.end();
});
/**
* ._write() - Stream

@@ -454,0 +664,0 @@ */

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