Sitelet https://github.com/redis/node-redis/pull/3156/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ client.scanIterator({

```typescript
// Conditionally update only if the current value matches
await client.set("key", "new-value", { condition: "IFEQ", conditionValue: "old-value" });
await client.set("key", "new-value", { condition: "IFEQ", matchValue: "old-value" });

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

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

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

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

### Disconnecting
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This folder contains example scripts showing how to use Node Redis in different
| `connect-to-cluster.js` | Connect to a Redis cluster. |
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch. |
| `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io). |
| `cas-cad-digest.js` | Atomic compare-and-set (CAS) and compare-and-delete (CAD) using digests for single-key optimistic concurrency control. |
| `dump-and-restore.js` | Demonstrates the use of the [`DUMP`](https://redis.io/commands/dump/) and [`RESTORE`](https://redis.io/commands/restore/) commands |
| `get-server-time.js` | Get the time from the Redis server. |
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog). |
Expand Down
51 changes: 51 additions & 0 deletions examples/cas-cad-digest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Atomic compare-and-set (CAS) and compare-and-delete (CAD) using digests
// for single-key optimistic concurrency control.
// Requires Redis 8.4+ and the @node-rs/xxhash package.

import { createClient, digest } from 'redis';

const client = createClient();

await client.connect();

// Basic Digest Usage: get digest from Redis and verify with client-side calculation
await client.set('mykey', 'myvalue');
const serverDigest = await client.digest('mykey');
const clientDigest = await digest('myvalue');
console.log(`Server digest: ${serverDigest}`);
console.log(`Client digest: ${clientDigest}`);
console.log(`Digests match: ${serverDigest === clientDigest}`);

// Optimistic Locking with IFDEQ: update only if digest matches
await client.set('mycounter', '100');
const currentDigest = await digest('100');

// Update only if digest matches
const result1 = await client.set('mycounter', '150', {
condition: 'IFDEQ',
matchValue: currentDigest
});
console.log(`Update with matching digest: ${result1}`); // 'OK'

// IFDNE: update only if digest does NOT match
await client.set('myversion', 'v1.0.0');
const unwantedDigest = await digest('v0.9.0');

const result2 = await client.set('myversion', 'v2.0.0', {
condition: 'IFDNE',
matchValue: unwantedDigest
});
console.log(`Update when digest differs: ${result2}`); // 'OK'

// Conditional Delete with DELEX
await client.set('mysession', 'sessiondata');
const sessionDigest = await digest('sessiondata');

// Delete only if digest matches
const deleted = await client.delEx('mysession', {
condition: 'IFDEQ',
matchValue: sessionDigest
});
console.log(`Deleted with correct digest: ${deleted}`); // 1

client.close();
Loading