Skip to content

Commit

Permalink
valkey - storage adapter for valkey (redis OSS) (#1153)
Browse files Browse the repository at this point in the history
* valkey - storage adapter for valkey (redis OSS)

* updating the class and type naming

* adding in its valkey instance

* removing tls as not needed

* adding in directory

* Update README.md
  • Loading branch information
jaredwray authored Oct 8, 2024
1 parent 2dcd2ca commit e6af350
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docker-compose-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ services:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: keyv_test
MYSQL_ROOT_HOST: '%'
keyv_valkey:
image: valkey/valkey:latest
command: redis-server --port 6370
environment:
REDIS_HOST: redis
ports:
- 6370:6370
keyv_redis:
image: redis:latest
environment:
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ services:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: keyv_test
MYSQL_ROOT_HOST: '%'
keyv_valkey:
image: valkey/valkey:latest
command: redis-server --port 6370
environment:
REDIS_HOST: redis
ports:
- 6370:6370
keyv_redis:
image: redis:latest
environment:
Expand Down
22 changes: 22 additions & 0 deletions packages/valkey/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2017-2021 Luke Childs
Copyright (c) 2021-2022 Jared Wray

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
101 changes: 101 additions & 0 deletions packages/valkey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# @keyv/valkey [<img width="100" align="right" src="https://jaredwray.com/images/keyv-symbol.svg" alt="keyv">](https://github.com/jaredwra/keyv)

> Valkey storage adapter for Keyv
[![build](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
[![codecov](https://codecov.io/gh/jaredwray/keyv/branch/main/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
[![npm](https://img.shields.io/npm/v/@keyv/valkey.svg)](https://www.npmjs.com/package/@keyv/valkey)
[![npm](https://img.shields.io/npm/dm/@keyv/valkey)](https://npmjs.com/package/@keyv/valkey)

[Valkey](https://valkey.io) storage adapter for [Keyv](https://github.com/jaredwray/keyv).

Valkey is the open source replacement to Redis which decided do a [dual license](https://redis.com/blog/redis-adopts-dual-source-available-licensing/) approach moving forward. Valkey is a drop-in replacement for Redis and is fully compatible with the Redis protocol.

We are using the [iovalkey](https://www.npmjs.com/package/iovalkey) which is a Node.js client for Valkey based on the `ioredis` client.

## Install

```shell
npm install --save keyv @keyv/valkey
```

## Usage

```js
import Keyv from 'keyv';
import KeyvValkey from '@keyv/valkey';

const keyv = new Keyv(new KeyvValkey('redis://user:pass@localhost:6379'));
keyv.on('error', handleConnectionError);
```

e.g:

```js
const keyv = new Keyv(new KeyvValkey('redis://user:pass@localhost:6379', { disable_resubscribing: true }));
```

Or you can manually create a storage adapter instance and pass it to Keyv:

```js
import Keyv from 'keyv';
import KeyvValkey from '@keyv/redis';

const KeyvValkey = new KeyvValkey('redis://user:pass@localhost:6379');
const keyv = new Keyv({ store: KeyvValkey });
```

Or reuse a previous Redis instance:

```js
import Keyv from 'keyv';
import Redis from 'ioredis';
import KeyvValkey from '@keyv/redis';

const redis = new Redis('redis://user:pass@localhost:6379');
const KeyvValkey = new KeyvValkey(redis);
const keyv = new Keyv({ store: KeyvValkey });
```

Or reuse a previous Redis cluster:

```js
import Keyv from 'keyv';
import Redis from 'ioredis';
import KeyvValkey from '@keyv/redis';

const redis = new Redis.Cluster('redis://user:pass@localhost:6379');
const KeyvValkey = new KeyvValkey(redis);
const keyv = new Keyv({ store: KeyvValkey });
```
## Options

### useRedisSets

The `useRedisSets` option lets you decide whether to use Redis sets for key management. By default, this option is set to `true`.

When `useRedisSets` is enabled (`true`):

- A namespace for the Redis sets is created, and all created keys are added to this. This allows for group management of keys.
- When a key is deleted, it's removed not only from the main storage but also from the Redis set.
- When clearing all keys (using the `clear` function), all keys in the Redis set are looked up for deletion. The set itself is also deleted.

**Note**: In high-performance scenarios, enabling `useRedisSets` might lead to memory leaks. If you're running a high-performance application or service, it is recommended to set `useRedisSets` to `false`.

If you decide to set `useRedisSets` as `false`, keys will be handled individually and Redis sets won't be utilized.

However, please note that setting `useRedisSets` to `false` could lead to performance issues in production when using the `clear` function, as it will need to iterate over all keys to delete them.

#### Example

Here's how you can use the `useRedisSets` option:

```js
import Keyv from 'keyv';

const keyv = new Keyv(new KeyvValkey('redis://user:pass@localhost:6379', { useRedisSets: false }));
```

## License

[MIT © Jared Wray](LISCENCE)
85 changes: 85 additions & 0 deletions packages/valkey/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"name": "@keyv/valkey",
"version": "1.0.0",
"description": "Valkey storage adapter for Keyv",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"scripts": {
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
"prepare": "yarn build",
"test": "xo --fix && vitest run --coverage",
"test:ci": "xo && vitest --run --sequence.setupFiles=list",
"clean": "rimraf ./node_modules ./coverage ./dist"
},
"xo": {
"rules": {
"import/no-named-as-default": "off",
"unicorn/prefer-module": "off",
"unicorn/prefer-event-target": "off",
"unicorn/prefer-node-protocol": "off",
"unicorn/no-typeof-undefined": "off",
"import/extensions": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-return": "off",
"unicorn/prefer-ternary": "off",
"unicorn/no-array-callback-reference": "off",
"import/no-extraneous-dependencies": "off",
"@typescript-eslint/no-confusing-void-expression": "off"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/jaredwray/keyv.git",
"directory": "packages/valkey"
},
"keywords": [
"redis",
"valkey",
"valkeyio",
"ioredis",
"keyv",
"storage",
"adapter",
"key",
"value",
"store",
"cache",
"ttl"
],
"author": "Jared Wray <[email protected]> (http://jaredwray.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/jaredwray/keyv/issues"
},
"homepage": "https://github.com/jaredwray/keyv",
"dependencies": {
"iovalkey": "^0.1.0"
},
"devDependencies": {
"@keyv/test-suite": "*",
"keyv": "^5.0.3",
"rimraf": "^6.0.1",
"timekeeper": "^2.3.1",
"tsd": "^0.31.2",
"xo": "^0.59.3"
},
"tsd": {
"directory": "test"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist",
"LICENSE"
]
}
Loading

0 comments on commit e6af350

Please sign in to comment.