effect-redis 0.0.26 → 0.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,7 +78,7 @@ The library is organized into several services, all of which can share a single
|
|
|
78
78
|
| :--- | :--- | :--- |
|
|
79
79
|
| `Redis` | `RedisLive` | Comprehensive Redis commands (Get, Set, Hash, List, etc.) |
|
|
80
80
|
| `RedisPubSub` | `RedisPubSubLive` | Specialized Pub/Sub operations |
|
|
81
|
-
| `RedisPersistence` | `RedisPersistenceLive` |
|
|
81
|
+
| `RedisPersistence` | `RedisPersistenceLive` | **Deprecated**: Use `Redis` service instead. Will be removed soon. |
|
|
82
82
|
| `RedisStream` | `RedisStreamLive` | Redis Streams operations (XADD, XREAD, XRANGE, etc.) |
|
|
83
83
|
| `RedisConnection` | `RedisConnectionLive` | The underlying shared connection manager |
|
|
84
84
|
|
|
@@ -121,7 +121,9 @@ The most comprehensive service, wrapping hundreds of Redis commands.
|
|
|
121
121
|
- `subscribe(key, options?)` -> Returns `Stream<StreamEntry>` (continuous polling)
|
|
122
122
|
- `xack(key, group, ...ids)`
|
|
123
123
|
|
|
124
|
-
### `RedisPersistence` Service
|
|
124
|
+
### `RedisPersistence` Service (**Deprecated**)
|
|
125
|
+
> **Note**: This service is being substituted by the `Redis` service and will be removed in a future version. Please migrate to the `Redis` service for all key-value operations.
|
|
126
|
+
|
|
125
127
|
A simplified service for basic storage needs.
|
|
126
128
|
- `setValue(key, value)`
|
|
127
129
|
- `getValue(key)`
|
|
@@ -131,6 +133,116 @@ A simplified service for basic storage needs.
|
|
|
131
133
|
|
|
132
134
|
---
|
|
133
135
|
|
|
136
|
+
## Usage Examples
|
|
137
|
+
|
|
138
|
+
### 1. Automatic Reconnection & Retries
|
|
139
|
+
In production, connections can drop. Use `Stream.retry` with a `Schedule` to handle this gracefully:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { Effect, Stream, Schedule, Duration } from "effect";
|
|
143
|
+
import { RedisPubSub } from "effect-redis";
|
|
144
|
+
|
|
145
|
+
const program = Effect.gen(function* () {
|
|
146
|
+
const pubsub = yield* RedisPubSub;
|
|
147
|
+
const subscription = yield* pubsub.subscribe("raw-data");
|
|
148
|
+
|
|
149
|
+
const monitoringStream = subscription.pipe(
|
|
150
|
+
Stream.tap(msg => Effect.log(`Received: ${msg}`)),
|
|
151
|
+
// Retry with exponential backoff if the connection fails
|
|
152
|
+
Stream.retry(
|
|
153
|
+
Schedule.exponential(Duration.seconds(1)).pipe(
|
|
154
|
+
Schedule.intersect(Schedule.recurs(5))
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
yield* Stream.runDrain(monitoringStream);
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 2. Stream Processing with Schema Validation
|
|
164
|
+
Integrate with `@effect/schema` to build type-safe data pipelines:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import { Effect, Stream, Schema, pipe } from "effect";
|
|
168
|
+
import { RedisPubSub } from "effect-redis";
|
|
169
|
+
|
|
170
|
+
const TradeSchema = Schema.Struct({
|
|
171
|
+
symbol: Schema.String,
|
|
172
|
+
price: Schema.Number,
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const program = Effect.gen(function* () {
|
|
176
|
+
const pubsub = yield* RedisPubSub;
|
|
177
|
+
const trades = yield* pubsub.subscribe("trades");
|
|
178
|
+
|
|
179
|
+
yield* pipe(
|
|
180
|
+
trades,
|
|
181
|
+
Stream.map(JSON.parse),
|
|
182
|
+
Stream.mapEffect(data =>
|
|
183
|
+
Schema.decodeUnknown(TradeSchema)(data).pipe(
|
|
184
|
+
Effect.tapError(err => Effect.logWarning(`Invalid trade: ${err}`)),
|
|
185
|
+
Effect.option // Filter out invalid entries
|
|
186
|
+
)
|
|
187
|
+
),
|
|
188
|
+
Stream.filterMap(opt => opt),
|
|
189
|
+
Stream.tap(trade => Effect.log(`Valid trade: ${trade.symbol} @ ${trade.price}`)),
|
|
190
|
+
Stream.runDrain
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### 3. Custom Commands & Transactions
|
|
196
|
+
Escalate to the raw `node-redis` client for features not natively wrapped:
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { Effect } from "effect";
|
|
200
|
+
import { Redis } from "effect-redis";
|
|
201
|
+
|
|
202
|
+
const program = Effect.gen(function* () {
|
|
203
|
+
const redis = yield* Redis;
|
|
204
|
+
|
|
205
|
+
// Execute a transaction using the raw client
|
|
206
|
+
const results = yield* redis.use((client) =>
|
|
207
|
+
client
|
|
208
|
+
.multi()
|
|
209
|
+
.set("key1", "val1")
|
|
210
|
+
.set("key2", "val2")
|
|
211
|
+
.exec()
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
yield* Effect.log(`Transaction results: ${JSON.stringify(results)}`);
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 4. Continuous Redis Stream Polling
|
|
219
|
+
Use `RedisStream.subscribe` for continuous polling. You can track the last seen ID to ensure continuity across restarts:
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
import { Effect, Stream } from "effect";
|
|
223
|
+
import { RedisStream } from "effect-redis";
|
|
224
|
+
|
|
225
|
+
const program = Effect.gen(function* () {
|
|
226
|
+
const redisStream = yield* RedisStream;
|
|
227
|
+
|
|
228
|
+
// Track the last seen ID in a Ref for continuity
|
|
229
|
+
const lastId = yield* Effect.makeRef("$");
|
|
230
|
+
|
|
231
|
+
const events = redisStream.subscribe("app-events", {
|
|
232
|
+
id: yield* lastId.get,
|
|
233
|
+
block: 5000 // Wait up to 5s if idle
|
|
234
|
+
}).pipe(
|
|
235
|
+
Stream.tap(entry => lastId.set(String(entry.id)))
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
yield* Stream.runForEach(events, entry =>
|
|
239
|
+
Effect.log(`Event ID: ${entry.id}, Data: ${JSON.stringify(entry.message)}`)
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
134
246
|
## Error Model
|
|
135
247
|
|
|
136
248
|
Every operation can fail with a `RedisError`, which is a union of specific error types:
|
|
@@ -148,14 +260,3 @@ const program = Redis.pipe(
|
|
|
148
260
|
)
|
|
149
261
|
);
|
|
150
262
|
```
|
|
151
|
-
|
|
152
|
-
---
|
|
153
|
-
|
|
154
|
-
## Separate CLI Tool
|
|
155
|
-
|
|
156
|
-
Looking for a CLI tool to monitor Redis? Check out [@ceschiatti/redistail](https://www.npmjs.com/package/@ceschiatti/redistail), which is built using this library!
|
|
157
|
-
|
|
158
|
-
```bash
|
|
159
|
-
npx @ceschiatti/redistail --help
|
|
160
|
-
```
|
|
161
|
-
|