# cluster-key-slot

> Generates CRC hashes for strings - for use by node redis clients to determine key slots.

Latest version **1.1.2** (published 2022-11-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install cluster-key-slot
pnpm add cluster-key-slot
yarn add cluster-key-slot
bun add cluster-key-slot
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-11-01 |
| First published | 2016-05-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 0 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | Mike Diarmid |
| Maintainers | salakar |
| Keywords | redis, hash, crc, slot, calc, javascript, node, node_redis, ioredis |

## Links

- npm: https://www.npmjs.com/package/cluster-key-slot
- Repository: https://github.com/Salakar/cluster-key-slot
- Homepage: https://github.com/Salakar/cluster-key-slot#readme
- Issues: https://github.com/Salakar/cluster-key-slot/issues
- npm.io page: https://npm.io/package/cluster-key-slot

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2022-11-01
- 1.1.1 — 2022-09-27
- 1.1.0 — 2019-07-18
- 1.0.12 — 2018-05-24
- 1.0.11 — 2018-05-24
- 1.0.10 — 2018-05-24
- 1.0.9 — 2018-04-25
- 1.0.8 — 2016-09-15
- 1.0.6 — 2016-06-23
- 1.0.5 — 2016-06-05
- 1.0.4 — 2016-06-05
- 1.0.3 — 2016-06-05
- 1.0.2 — 2016-06-05
- 1.0.2-p — 2016-06-05
- 1.0.1 — 2016-05-31
- … 1 more at https://npm.io/package/cluster-key-slot/versions

## README

[![Coverage Status](https://coveralls.io/repos/github/Salakar/cluster-key-slot/badge.svg?branch=master)](https://coveralls.io/github/Salakar/cluster-key-slot?branch=master)
![Downloads](https://img.shields.io/npm/dt/cluster-key-slot.svg)
[![npm version](https://img.shields.io/npm/v/cluster-key-slot.svg)](https://www.npmjs.com/package/cluster-key-slot)
[![dependencies](https://img.shields.io/david/Salakar/cluster-key-slot.svg)](https://david-dm.org/Salakar/cluster-key-slot)
[![License](https://img.shields.io/npm/l/cluster-key-slot.svg)](/LICENSE)
<a href="https://twitter.com/mikediarmid"><img src="https://img.shields.io/twitter/follow/mikediarmid.svg?style=social&label=Follow" alt="Follow on Twitter"></a>

# Redis Key Slot Calculator

A high performance redis cluster key slot calculator for node redis clients e.g. [node_redis](https://github.com/NodeRedis/node_redis), [ioredis](https://github.com/luin/ioredis) and [redis-clustr](https://github.com/gosquared/redis-clustr/).

This also handles key tags such as `somekey{actualTag}`.

## Install

Install with [NPM](https://npmjs.org/):

```
npm install cluster-key-slot --save
```

## Usage

```js
const calculateSlot = require('cluster-key-slot');
const calculateMultipleSlots = require('cluster-key-slot').generateMulti;

// ...

// a single slot number
const slot = calculateSlot('test:key:{butOnlyThis}redis');
// Buffer is also supported
const anotherSlot = calculateSlot(Buffer.from([0x7b, 0x7d, 0x2a]));

// multiple keys - multi returns a single key slot number, returns -1 if any
// of the keys does not match the base slot number (base is defaulted to first keys slot)
// This is useful to quickly determine a singe slot for multi keys operations.
const slotForRedisMulti = calculateMultipleSlots([
  'test:key:{butOnlyThis}redis',
  'something:key45:{butOnlyThis}hello',
  'example:key46:{butOnlyThis}foobar',
]);
```

## Benchmarks

`OLD` in these benchmarks refers to the `ioredis` crc calc and many of the other calculators that use `Buffer`.

```text
node -v                                                                                                                                                                                                ✔  16.38G RAM  10:29:07
v10.15.3

NEW tags x 721,445 ops/sec ±0.44% (90 runs sampled)
OLD tags x 566,777 ops/sec ±0.97% (96 runs sampled)
NEW without tags x 2,054,845 ops/sec ±1.77% (92 runs sampled)
OLD without tags x 865,839 ops/sec ±0.43% (96 runs sampled)
NEW without tags singular x 6,354,097 ops/sec ±1.25% (94 runs sampled)
OLD without tags singular x 4,012,250 ops/sec ±0.96% (94 runs sampled)
NEW tags (Buffer) x 552,346 ops/sec ±1.35% (92 runs sampled)
```

---
_Source: https://npm.io/package/cluster-key-slot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
