Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kane50613/redis-on-workers
Connect to your Redis server using cloudflare:sockets
https://github.com/kane50613/redis-on-workers
cloudflare cloudflare-pages-functions cloudflare-workers edge redis socket
Last synced: 2 months ago
JSON representation
Connect to your Redis server using cloudflare:sockets
- Host: GitHub
- URL: https://github.com/kane50613/redis-on-workers
- Owner: kane50613
- License: mit
- Created: 2024-03-03T14:01:46.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-11-01T07:47:30.000Z (2 months ago)
- Last Synced: 2024-11-01T08:25:58.233Z (2 months ago)
- Topics: cloudflare, cloudflare-pages-functions, cloudflare-workers, edge, redis, socket
- Language: TypeScript
- Homepage:
- Size: 207 KB
- Stars: 9
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-on-workers
Connect to your Redis server using `cloudflare:sockets`.
This package is designed to work with Cloudflare Workers, but it can also be used in node.js thanks to the implementation of [`cloudflare:sockets` for node.js](https://github.com/Ethan-Arrowood/socket).
## Installation
```sh
npm install redis-on-workers
```## Usage
### Minimal
This is the minimal example to connect to a Redis server.
```ts
import { createRedis } from "redis-on-workers";const redis = createRedis("redis://:@:");
await redis.send("SET", "foo", "bar");
const value = await redis.send("GET", "foo");
console.log(value); // bar
// remember to close the connection after use, or use `redis.sendOnce`.
await redis.close();
```### Raw Uint8Array
This is useful if you want to store binary data. For example, you can store protobuf messages in Redis.
```ts
import { createRedis } from "redis-on-workers";const redis = createRedis("redis://:@:");
await redis.sendRaw("SET", "foo", "bar");
const value = await redis.sendRawOnce("GET", "foo");
const decoder = new TextDecoder();
console.log(decoder.decode(value)); // bar
```### Node.js
Please install the node.js polyfill for `cloudflare:sockets` to use this package in node.js.
```sh
npm install @arrowood.dev/socket
```## API
### `createRedis(options: CreateRedisOptions | string): RedisInstance`
Create a new Redis client, does NOT connect to the server yet, the connection will be established when the first command is sent.
Or you can start connection immediately by using `redis.startConnection()`.
### `CreateRedisOptions`
- `url` (string): The URL of the Redis server.
- `tls` (boolean): Whether to use TLS. Default: `false`.
- `logger` (function): A function to log debug messages.
- `connectFn` (function): Polyfill for `cloudflare:sockets`'s `connect` function if you're using it in node.js. Default: `undefined`.