Skip to content

Commit

Permalink
feat: add compress decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 29, 2021
1 parent 266affb commit cd089b6
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 15 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,15 @@ await cache.get('foo') // 'cache'

You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.

The following example is using [compress-brotli](https://github.com/Kikobeats/compress-brotli) as serializer:

```js
const brotli = require('compress-brotli')()
const Keyv = require('keyv')

const serialize = async ({ value, expires }) => {
return brotli.serialize({ value: await brotli.compress(value), expires })
}
The following example is using [@keyvhq/compress](https://github.com/microlinkhq/keyv/tree/master/packages/compress) as serializer:

const deserialize = async data => {
const { value, expires } = brotli.deserialize(data)
return { value: await brotli.decompress(value), expires }
}
```js
const KeyvCompress = require('@keyvhq/compress')
const Keyv = require('@keyvhq/core')

const keyv = new Keyv({ serialize, deserialize })
const keyv = KeyvCompress(new Keyv({ serialize, deserialize })
```
## Storage Adapters
Expand Down Expand Up @@ -149,8 +142,8 @@ You should also set a [`namespace`](#optionsnamespace) for your module so you ca
### Decorators
- [@keyvhq/offline](https://github.com/microlinkhq/keyv/tree/master/packages/offline)SQLite storage adapter for Keyv.
- [@keyvhq/brotli](https://github.com/microlinkhq/keyv/tree/master/packages/brotli)Memoize any function using Keyv as storage backend.
- [@keyvhq/offline](https://github.com/microlinkhq/keyv/tree/master/packages/offline) – Adds offline capabilities for your keyv instance.
- [@keyvhq/compress](https://github.com/microlinkhq/keyv/tree/master/packages/compress) – Adds compression bindings for your Keyv instance.
- [@keyvhq/memoize](https://github.com/microlinkhq/keyv/tree/master/packages/memoize) – Memoize any function using Keyv as storage backend.
### Community
Expand All @@ -164,7 +157,6 @@ You should also set a [`namespace`](#optionsnamespace) for your module so you ca
- [keyv-lru](https://github.com/e0ipso/keyv-lru) – An in-memory LRU back-end for Keyv.
- [keyv-memcache](https://github.com/jaredwray/keyv-memcache) - Memcache storage adapter for Keyv.
- [keyv-mssql](https://github.com/pmorgan3/keyv-mssql) - Microsoft SQL Server adapter for Keyv.
- [keyv-offline](https://github.com/Kikobeats/keyv-offline) – Adding offline capabilities for your keyv instance.
- [keyv-s3](https://github.com/microlinkhq/keyv-s3) - Amazon S3 storage adapter for Keyv.
- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache.
Expand Down
13 changes: 13 additions & 0 deletions packages/compress/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### 1.0.3 (2021-07-25)

### 1.0.2 (2021-07-19)

### 1.0.1 (2021-07-19)

## [1.0.0](https://github.com/Kikobeats/keyv-offline/compare/v0.0.1...v1.0.0) (2021-07-16)

### 0.0.1 (2021-07-16)
52 changes: 52 additions & 0 deletions packages/compress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# @keyvhq/compress [<img width="100" align="right" src="https://keyv.js.org/media/logo-sunset.svg" alt="keyv">](https://github.com/microlinkhq/keyv)

> Adding offline capabilities for your keyv instance.
## Install

```bash
$ npm install @keyvhq/compress --save
```

## Usage

All you need to do is to wrap your [keyv](https://keyv.js.org) instance:

```js

const KeyvRedis = require('@keyvhq/redis')

const keyv = new KeyvRedis({
uri: 'redis://user:pass@localhost:6379',
maxRetriesPerRequest: 1,
emitErrors: false
})
```

Using `@keyvhq/compress` at the top level:

```js
const KeyvRedis = require('@keyvhq/redis')
const keyvOffline = require('@keyvhq/compress')

const keyv = keyvOffline(new KeyvRedis({
uri: 'redis://user:pass@localhost:6379',
maxRetriesPerRequest: 1,
emitErrors: false
}))
```

That's all!

In the next database downtime, your keyv set/get petitions will be temporarily bypassed, preventing your application to crash for that, being more resilient than the default keyv behavior.

As soon as the connection is re-established it will be work back as expected.

In case you need, you can see omitted errors enabling debug doing `DEBUG=@keyvhq/compress*`

## License

**@keyvhq/memoize** © [Kiko Beats](https://kikobeats.com), Released under the [MIT](https://github.com/microlinkhq/keyv/blob/master/LICENSE.md) License.<br/>
Maintained by [Microlink](https://microlink.io) with help from [contributors](https://github.com/microlinkhq/keyv/contributors).

> [microlink.io](https://microlink.io) · GitHub [@MicrolinkHQ](https://github.com/microlinkhq) · Twitter [@microlinkhq](https://twitter.com/microlinkhq)
56 changes: 56 additions & 0 deletions packages/compress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@keyvhq/compress",
"description": "Adds compression bindings for your Keyv instance, saving as much space as you can.",
"homepage": "https://keyv.js.org",
"version": "1.0.3",
"main": "src/index.js",
"author": {
"email": "[email protected]",
"name": "microlink.io",
"url": "https://microlink.io"
},
"repository": {
"directory": "packages/offline",
"type": "git",
"url": "git+https://github.com/microlinkhq/keyv.git"
},
"bugs": {
"url": "https://github.com/microlinkhq/keyv/issues"
},
"keywords": [
"brotli",
"cache",
"compress",
"compression",
"decompress",
"deserialize",
"key",
"keyv",
"serialize",
"store",
"ttl",
"value"
],
"dependencies": {
"compress-brotli": "~1.3.2"
},
"devDependencies": {
"@keyvhq/core": "latest",
"ava": "latest",
"delay": "latest",
"nyc": "latest"
},
"engines": {
"node": ">= 12"
},
"files": [
"src"
],
"scripts": {
"test": "nyc ava"
},
"license": "MIT",
"publishConfig": {
"access": "public"
}
}
22 changes: 22 additions & 0 deletions packages/compress/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const compressBrotli = require('compress-brotli')

function KeyvCompress (keyv, opts) {
if (!(this instanceof KeyvCompress)) return new KeyvCompress(keyv, opts)

const brotli = compressBrotli(opts)

keyv.serialize = async ({ value, expires }) => {
return brotli.serialize({ value: await brotli.compress(value), expires })
}

keyv.deserialize = async data => {
const { value, expires } = brotli.deserialize(data)
return { value: await brotli.decompress(value), expires }
}

return keyv
}

module.exports = KeyvCompress
34 changes: 34 additions & 0 deletions packages/compress/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const compressBrotli = require('compress-brotli')
const JSONB = require('json-buffer')
const Keyv = require('@keyvhq/core')
const test = require('ava')

const KeyvCompress = require('..')

test('pass compress options', async t => {
const store = new Map()
const keyv = KeyvCompress(new Keyv({ store, namespace: null }), {
enable: false
})

await keyv.set('foo', 'bar')
t.is(store.get('foo'), JSONB.stringify({ value: 'bar', expires: null }))
})

test('enable compression', async t => {
const brotli = compressBrotli()
const store = new Map()
const keyv = KeyvCompress(new Keyv({ store, namespace: null }))
await keyv.set('foo', 'bar')
const compressed = await brotli.compress('bar')

t.is(
store.get('foo'),
JSONB.stringify({
value: compressed,
expires: null
})
)
})

0 comments on commit cd089b6

Please sign in to comment.