Skip to content

Commit

Permalink
Merge branch 'master' into greenkeeper/xo-0.25.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 20, 2020
2 parents 7f04d48 + 57c03a4 commit 6ff7ca2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
github:
- 'lukechilds'
custom:
- 'https://blockstream.info/address/1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj'
- 'https://blockstream.info/address/3Luke2qRn5iLj4NiFrvLBu2jaEj7JeMR6w'
- 'https://blockstream.info/address/bc1qlukeyq0c69v97uss68fet26kjkcsrymd2kv6d4'
- 'https://tippin.me/@lukechilds'
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ The following are third-party storage adapters compatible with Keyv:
- [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv
- [keyv-firestore ](https://github.com/goto-bus-stop/keyv-firestore) – Firebase Cloud Firestore adapter for Keyv
- [keyv-mssql](https://github.com/pmorgan3/keyv-mssql) - Microsoft Sql Server adapter for Keyv
- [keyv-memcache](https://github.com/jaredwray/keyv-memcache) - Memcache storage adapter for Keyv

## Add Cache Support to your Module

Expand Down Expand Up @@ -255,11 +256,11 @@ Set a value.

By default keys are persistent. You can set an expiry TTL in milliseconds.

Returns `true`.
Returns a promise which resolves to `true`.

#### .get(key, [options])

Returns the value.
Returns a promise which resolves to the retrieved value.

##### options.raw

Expand All @@ -268,19 +269,19 @@ Default: `false`

If set to true the raw DB object Keyv stores internally will be returned instead of just the value.

This contians the TTL timestamp.
This contains the TTL timestamp.

#### .delete(key)

Deletes an entry.

Returns `true` if the key existed, `false` if not.
Returns a promise which resolves to `true` if the key existed, `false` if not.

#### .clear()

Delete all entries in the current namespace.

Returns `undefined`.
Returns a promise which is resolved when the entries have been cleared.

## License

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keyv",
"version": "3.1.0",
"version": "4.0.0",
"description": "Simple key-value storage with support for multiple backends",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -44,6 +44,6 @@
"nyc": "^14.1.1",
"this": "^1.0.2",
"timekeeper": "^2.0.0",
"xo": "^0.25.0"
"xo": "^0.25.3"
}
}
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class Keyv extends EventEmitter {
return Promise.resolve()
.then(() => store.get(key))
.then(data => {
data = (typeof data === 'string') ? this.opts.deserialize(data) : data;
return (typeof data === 'string') ? this.opts.deserialize(data) : data;
})
.then(data => {
if (data === undefined) {
return undefined;
}
Expand Down Expand Up @@ -86,8 +88,9 @@ class Keyv extends EventEmitter {
.then(() => {
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
value = { value, expires };
return store.set(key, this.opts.serialize(value), ttl);
return this.opts.serialize(value);
})
.then(value => store.set(key, value, ttl))
.then(() => true);
}

Expand Down
19 changes: 19 additions & 0 deletions test/keyv.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,24 @@ test.serial('Keyv uses custom serializer when provided instead of json-buffer',
t.is(await keyv.get('foo'), 'bar');
});

test.serial('Keyv supports async serializer/deserializer', async t => {
t.plan(3);
const store = new Map();

const serialize = async data => {
t.pass();
return JSON.stringify(data);
};

const deserialize = async data => {
t.pass();
return JSON.parse(data);
};

const keyv = new Keyv({ store, serialize, deserialize });
await keyv.set('foo', 'bar');
t.is(await keyv.get('foo'), 'bar');
});

const store = () => new Map();
keyvTestSuite(test, Keyv, store);

0 comments on commit 6ff7ca2

Please sign in to comment.