Comparing version 1.2.0 to 1.3.0
# Changelog | ||
## [1.3.0](https://github.com/alexzel/iomem/compare/v1.2.0...v1.3.0) (2023-01-26) | ||
### Features | ||
* implement Mem.setDefaultExpiry for static methods expiry ([78929de](https://github.com/alexzel/iomem/commit/78929de1ea58701e84e81c89eb87dd4bddc9ab5f)) | ||
## [1.2.0](https://github.com/alexzel/iomem/compare/v1.1.2...v1.2.0) (2023-01-26) | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "iomem", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Memcached client implementing binary protocol with native multiple keys support", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -320,14 +320,10 @@ # `iomem` | ||
class Echo extends Writable { | ||
constructor (opts) { | ||
super({ objectMode: true, ...opts }) | ||
} | ||
// Set some data | ||
await iomem.set('test:a', 'a') | ||
_write (data, _, cb) { | ||
console.log(data) | ||
cb() | ||
} | ||
} | ||
// Writable stream | ||
const ws = new Writable({ objectMode: true, write (data, _, cb) { console.log(data); cb() } }) | ||
pipeline(iomem.get('test:a'), new Echo(), err => { | ||
// Pipeline | ||
pipeline(iomem.get('test:a'), ws, err => { | ||
if (err) { | ||
@@ -355,14 +351,13 @@ console.error(err) | ||
class Echo extends Writable { | ||
constructor (opts) { | ||
super({ objectMode: true, ...opts }) | ||
} | ||
// Set some data | ||
await iomem.set('test:a', 'a') | ||
_write (data, _, cb) { | ||
console.log(data) | ||
cb() | ||
} | ||
} | ||
// Readable stream | ||
const rs = Readable.from([Mem.get('test:a')][Symbol.iterator]()) | ||
pipeline(Readable.from([Mem.get('test:a')][Symbol.iterator]()), iomem.stream(), new Echo(), err => { | ||
// Writable stream | ||
const ws = new Writable({ objectMode: true, write (data, _, cb) { console.log(data); cb() } }) | ||
// Pipeline | ||
pipeline(rs, iomem.stream(), ws, err => { | ||
if (err) { | ||
@@ -388,14 +383,54 @@ console.error(err) | ||
class Echo extends Writable { | ||
constructor (opts) { | ||
super({ objectMode: true, ...opts }) | ||
} | ||
// Set some data | ||
await iomem.set('test:a', 'a') | ||
await iomem.set('test:b', 'b') | ||
_write (data, _, cb) { | ||
console.log(data) | ||
cb() | ||
// Readable stream | ||
const rs = Readable.from([Mem.get('test:a')][Symbol.iterator]()) | ||
// Writable stream | ||
const ws = new Writable({ objectMode: true, write (data, _, cb) { console.log(data); cb() } }) | ||
// Pipeline | ||
pipeline(rs, iomem.get('test:b'), ws, err => { | ||
if (err) { | ||
console.error(err) | ||
} | ||
} | ||
}) | ||
pipeline(Readable.from([Mem.get('test:a')][Symbol.iterator]()), iomem.get('test:b'), new Echo(), err => { | ||
... | ||
iomem.end() // call end() when your script or web server exits | ||
``` | ||
### Static methods for readable streams | ||
As you may have noticed from the above examples, in order to supply a readable stream with client methods you have to | ||
use a static version of the methods like `Mem.get('test:a')` instead of `iomem.get('test:a')`. | ||
The caveat here is that you may assume that for the methods accepting an expiry (like `set`, `add`, etc..) | ||
it will use the default expiry that you have passed into the client constructor in case you omit the method argument. | ||
But in reality it won't know anything about your client instances and therefore their configs as it's a | ||
static class method. So it will fall to using the default 1 day interval as an expiry. | ||
In order to supply static methods with default expiry, please use `Mem.setDefaultExpiry(expiry)` static method. | ||
```js | ||
const Mem = require('iomem') | ||
const iomem = new Mem(['127.0.0.1:11211'], { stream: true }) | ||
const { pipeline, Readable, Writable } = require('node:stream') | ||
// Set default expiry for static methods | ||
Mem.setDefaultExpiry(60 * 60) // 1 hour | ||
// Readable stream | ||
const rs = Readable.from([Mem.set('test:a', 'a'), Mem.get('test:a')][Symbol.iterator]()) | ||
// Writable stream | ||
const ws = new Writable({ objectMode: true, write (data, _, cb) { console.log(data); cb() } }) | ||
// Pipeline | ||
pipeline(rs, iomem.get('test:b'), ws, err => { | ||
if (err) { | ||
@@ -415,3 +450,2 @@ console.error(err) | ||
- Optional keys hashing | ||
- Optional KeepAlive for sockets | ||
- Add types for TypeScript |
@@ -27,2 +27,7 @@ 'use strict' | ||
static DEFAULT_EXPIRY = DEFAULT_EXPIRY | ||
static setDefaultExpiry (expiry = DEFAULT_EXPIRY) { | ||
Client.DEFAULT_EXPIRY = expiry | ||
} | ||
static get (key) { | ||
@@ -44,27 +49,27 @@ return ['get', key] | ||
static set (key, value, expiry = DEFAULT_EXPIRY) { | ||
static set (key, value, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['set', key, value, expiry] | ||
} | ||
static setk (key, expiry = DEFAULT_EXPIRY) { | ||
static setk (key, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['set', key, expiry] | ||
} | ||
static add (key, value, expiry = DEFAULT_EXPIRY) { | ||
static add (key, value, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['add', key, value, expiry] | ||
} | ||
static addk (key, expiry = DEFAULT_EXPIRY) { | ||
static addk (key, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['add', key, expiry] | ||
} | ||
static replace (key, value, expiry = DEFAULT_EXPIRY) { | ||
static replace (key, value, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['replace', key, value, expiry] | ||
} | ||
static replacek (key, expiry = DEFAULT_EXPIRY) { | ||
static replacek (key, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['replace', key, expiry] | ||
} | ||
static cas (key, value, cas, expiry = DEFAULT_EXPIRY) { | ||
static cas (key, value, cas, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['cas', key, value, expiry, cas] | ||
@@ -77,7 +82,7 @@ } | ||
static incr (key, initial, delta, expiry = DEFAULT_EXPIRY) { | ||
static incr (key, initial, delta, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['incr', key, initial, delta, expiry] | ||
} | ||
static decr (key, initial, delta, expiry = DEFAULT_EXPIRY) { | ||
static decr (key, initial, delta, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['decr', key, initial, delta, expiry] | ||
@@ -150,7 +155,7 @@ } | ||
static touch (key, expiry = DEFAULT_EXPIRY) { | ||
static touch (key, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['touch', key, expiry] | ||
} | ||
static gat (key, expiry = DEFAULT_EXPIRY) { | ||
static gat (key, expiry = Client.DEFAULT_EXPIRY) { | ||
return ['gat', key, expiry] | ||
@@ -157,0 +162,0 @@ } |
@@ -10,2 +10,4 @@ 'use strict' | ||
// Helpers | ||
// add, set, replace | ||
@@ -20,7 +22,2 @@ const setter = (opcode, key, value, expiry = 0, cas = DEFAULT_CAS, opaque = DEFAULT_OPAQUE) => { | ||
const midifier = (opcode, key, value, opaque = DEFAULT_OPAQUE) => { | ||
const [buffer] = serialize(value) | ||
return [opcode, key, buffer, DEFAULT_EXTRAS, DEFAULT_STATUS, DEFAULT_CAS, opaque] | ||
} | ||
// increment and decrement | ||
@@ -45,2 +42,8 @@ const counter = (opcode, key, initial, delta, expiry = 0, opaque = DEFAULT_OPAQUE) => { | ||
// append and prepend | ||
const midifier = (opcode, key, value, opaque = DEFAULT_OPAQUE) => { | ||
const [buffer] = serialize(value) | ||
return [opcode, key, buffer, DEFAULT_EXTRAS, DEFAULT_STATUS, DEFAULT_CAS, opaque] | ||
} | ||
// creates protocol method function and extend it with format(), result(), and bykeys flag | ||
@@ -54,2 +57,4 @@ const createMethod = (method, format, result, bykeys = false) => { | ||
// Commands | ||
// get or multi get with value or [value, ...] response | ||
@@ -56,0 +61,0 @@ const get = createMethod( |
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
58793
1171
448