ttl-mem-cache
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -6,17 +6,12 @@ 'use strict'; | ||
const Swarm = require('discovery-swarm'); | ||
const pump = require('pump'); | ||
const CHANNEL_NAME = 'test-swarm'; | ||
// https://github.com/mcollina/split2 | ||
const options = { | ||
udp: false, | ||
dht: false, | ||
maxConnections: 2, | ||
// maxConnections: 2, | ||
}; | ||
const cacheA = new Cache(); | ||
const cacheB = new Cache(); | ||
const toJson = new stream.Transform({ | ||
@@ -26,10 +21,4 @@ writableObjectMode: true, | ||
transform(chunk, enc, next) { | ||
/* | ||
const c = Buffer.from(JSON.stringify(chunk)); | ||
const b = Buffer.from('\n\n'); | ||
this.push(c); | ||
this.push(b); | ||
*/ | ||
const b = Buffer.from(chunk); | ||
this.push(b); | ||
const buff = Buffer.from(chunk); | ||
this.push(buff); | ||
next(); | ||
@@ -44,21 +33,8 @@ } | ||
const c = chunk.toString(); | ||
console.log('c', c); | ||
if (c !== undefined) { | ||
try { | ||
const obj = JSON.parse(c); | ||
this.push(obj); | ||
} catch (err) { | ||
try { | ||
const obj = JSON.parse(c); | ||
this.push(obj); | ||
} catch (err) { | ||
} | ||
} | ||
/* | ||
chunk.toString().split('\n\n').sort().filter((item, index, arr) => { | ||
return item && (!index || item != arr[index - 1]); | ||
}).forEach((entry, x) => { | ||
const obj = JSON.parse(entry); | ||
console.log(x, obj); | ||
this.push(obj); | ||
}); | ||
*/ | ||
next(); | ||
@@ -68,64 +44,65 @@ } | ||
const a = new Swarm(Object.assign({ | ||
id: Buffer.from(cacheA.id) | ||
const cacheA = new Cache(); | ||
const swarmA = new Swarm(Object.assign({ | ||
stream: (info) => { | ||
return pump(fromJson, cacheA, toJson); | ||
} | ||
}, options)); | ||
a.on('peer', (peer) => { | ||
console.log('A got peer', peer); | ||
swarmA.on('peer', (info) => { | ||
console.log('peer', info); | ||
}); | ||
a.on('connection', (connection, info) => { | ||
/* | ||
if (info.initiator) { | ||
connection.pipe(fromJson).pipe(cacheA).pipe(toJson).pipe(connection); | ||
console.log('A connection', info); | ||
} | ||
*/ | ||
connection.pipe(fromJson).pipe(cacheA).pipe(toJson).pipe(connection); | ||
swarmA.on('connection', (connection, info) => { | ||
console.log('connection', info); | ||
}); | ||
a.listen(0); | ||
a.join(CHANNEL_NAME); | ||
swarmA.listen(0); | ||
swarmA.join(CHANNEL_NAME); | ||
cacheA.on('set', (info) => { | ||
console.log('A set', info); | ||
console.log('set', info); | ||
}); | ||
setTimeout(() => { | ||
cacheA.set('foo', 'bar'); | ||
}, 2000); | ||
}, 10000); | ||
setTimeout(() => { | ||
console.log('get A', cacheA.get('foo')); | ||
}, 12000); | ||
const b = new Swarm(Object.assign({ | ||
id: Buffer.from(cacheB.id) | ||
const cacheB = new Cache(); | ||
const swarmB = new Swarm(Object.assign({ | ||
stream: (info) => { | ||
return pump(fromJson, cacheB, toJson); | ||
} | ||
}, options)); | ||
b.on('peer', (peer) => { | ||
console.log('B got peer', peer.port); | ||
swarmB.on('peer', (info) => { | ||
console.log('peer', info); | ||
}); | ||
b.on('connection', (connection, info) => { | ||
/* | ||
if (info.initiator) { | ||
connection.pipe(fromJson).pipe(cacheB).pipe(toJson).pipe(connection); | ||
console.log('B connection', info); | ||
} | ||
*/ | ||
connection.pipe(fromJson).pipe(cacheB).pipe(toJson).pipe(connection); | ||
swarmB.on('connection', (connection, info) => { | ||
console.log('connection', info); | ||
}); | ||
b.listen(0); | ||
b.join(CHANNEL_NAME); | ||
swarmB.listen(0); | ||
swarmB.join(CHANNEL_NAME); | ||
cacheB.on('set', (info) => { | ||
console.log('B set', info); | ||
console.log('set', info); | ||
}); | ||
setTimeout(() => { | ||
console.log('get B', cacheB.get('foo')); | ||
}, 12000); | ||
setTimeout(() => { | ||
console.log('B', cacheB.get('foo')); | ||
}, 3000); |
@@ -202,7 +202,12 @@ 'use strict'; | ||
_write(obj, enc, next) { | ||
if (utils.isNotEmpty(obj.origin) && (obj.origin === this.id)) { | ||
return next(); | ||
} | ||
if (Entry.assertLoose(obj)) { | ||
const item = this.store.get(obj.key); | ||
if ( | ||
utils.isNotEmpty(item) && | ||
obj.key === item.key && | ||
obj.expires === item.expires | ||
) { | ||
return next(); | ||
} | ||
if (Entry.assertLoose(obj)) { | ||
this[_set](obj); | ||
@@ -209,0 +214,0 @@ return next(); |
{ | ||
"name": "ttl-mem-cache", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A in memory time to live cache with streaming support.", | ||
@@ -5,0 +5,0 @@ "main": "lib/cache.js", |
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
20
121222
1925