Skip to content

Commit 0d8f3a2

Browse files
authored
docs: correct CAS/CAD parameter name and update example (redis#3156)
1 parent 34e526a commit 0d8f3a2

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ client.scanIterator({
230230
231231
```typescript
232232
// Conditionally update only if the current value matches
233-
await client.set("key", "new-value", { condition: "IFEQ", conditionValue: "old-value" });
233+
await client.set("key", "new-value", { condition: "IFEQ", matchValue: "old-value" });
234234

235235
// Conditionally delete only if the current value matches
236-
await client.delEx("key", { condition: "IFEQ", conditionValue: "expected-value" });
236+
await client.delEx("key", { condition: "IFEQ", matchValue: "expected-value" });
237237
```
238238

239239
#### Local Digest
@@ -251,7 +251,7 @@ import { digest } from "redis";
251251

252252
const hash = await digest("my-value");
253253

254-
await client.set("key", "new-value", { condition: "IFDEQ", conditionValue: hash });
254+
await client.set("key", "new-value", { condition: "IFDEQ", matchValue: hash });
255255
```
256256

257257
### Disconnecting

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This folder contains example scripts showing how to use Node Redis in different
1212
| `connect-to-cluster.js` | Connect to a Redis cluster. |
1313
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch. |
1414
| `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io). |
15+
| `cas-cad-digest.js` | Atomic compare-and-set (CAS) and compare-and-delete (CAD) using digests for single-key optimistic concurrency control. |
1516
| `dump-and-restore.js` | Demonstrates the use of the [`DUMP`](https://redis.io/commands/dump/) and [`RESTORE`](https://redis.io/commands/restore/) commands |
1617
| `get-server-time.js` | Get the time from the Redis server. |
1718
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog). |

examples/cas-cad-digest.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Atomic compare-and-set (CAS) and compare-and-delete (CAD) using digests
2+
// for single-key optimistic concurrency control.
3+
// Requires Redis 8.4+ and the @node-rs/xxhash package.
4+
5+
import { createClient, digest } from 'redis';
6+
7+
const client = createClient();
8+
9+
await client.connect();
10+
11+
// Basic Digest Usage: get digest from Redis and verify with client-side calculation
12+
await client.set('mykey', 'myvalue');
13+
const serverDigest = await client.digest('mykey');
14+
const clientDigest = await digest('myvalue');
15+
console.log(`Server digest: ${serverDigest}`);
16+
console.log(`Client digest: ${clientDigest}`);
17+
console.log(`Digests match: ${serverDigest === clientDigest}`);
18+
19+
// Optimistic Locking with IFDEQ: update only if digest matches
20+
await client.set('mycounter', '100');
21+
const currentDigest = await digest('100');
22+
23+
// Update only if digest matches
24+
const result1 = await client.set('mycounter', '150', {
25+
condition: 'IFDEQ',
26+
matchValue: currentDigest
27+
});
28+
console.log(`Update with matching digest: ${result1}`); // 'OK'
29+
30+
// IFDNE: update only if digest does NOT match
31+
await client.set('myversion', 'v1.0.0');
32+
const unwantedDigest = await digest('v0.9.0');
33+
34+
const result2 = await client.set('myversion', 'v2.0.0', {
35+
condition: 'IFDNE',
36+
matchValue: unwantedDigest
37+
});
38+
console.log(`Update when digest differs: ${result2}`); // 'OK'
39+
40+
// Conditional Delete with DELEX
41+
await client.set('mysession', 'sessiondata');
42+
const sessionDigest = await digest('sessiondata');
43+
44+
// Delete only if digest matches
45+
const deleted = await client.delEx('mysession', {
46+
condition: 'IFDEQ',
47+
matchValue: sessionDigest
48+
});
49+
console.log(`Deleted with correct digest: ${deleted}`); // 1
50+
51+
client.close();

0 commit comments

Comments
 (0)