You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
Adds transaction with watched key example script. (#2297)
* transction example improved * readme fixed * delay added for watched key changes * pooling error fixed on recursion * Minor comment update. Co-authored-by: Ajay <ajay.markana@yudiz.com> Co-authored-by: Simon Prickett <simon@redis.com> Closes #2280.
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
This folder contains example scripts showing how to use Node Redis in different scenarios.
|
This folder contains example scripts showing how to use Node Redis in different scenarios.
|
||||||
|
|
||||||
| File Name | Description |
|
| File Name | Description |
|
||||||
|-----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
|
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `blocking-list-pop.js` | Block until an element is pushed to a list |
|
| `blocking-list-pop.js` | Block until an element is pushed to a list |
|
||||||
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) |
|
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) |
|
||||||
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
|
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
|
||||||
@@ -26,6 +26,7 @@ This folder contains example scripts showing how to use Node Redis in different
|
|||||||
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |
|
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |
|
||||||
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
|
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
|
||||||
| `stream-consumer-group.js` | Reads entties from a [Redis Stream](https://redis.io/topics/streams-intro) as part of a consumer group using the blocking `XREADGROUP` command |
|
| `stream-consumer-group.js` | Reads entties from a [Redis Stream](https://redis.io/topics/streams-intro) as part of a consumer group using the blocking `XREADGROUP` command |
|
||||||
|
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking |
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
@@ -47,21 +48,21 @@ $ npm install
|
|||||||
|
|
||||||
When adding a new example, please follow these guidelines:
|
When adding a new example, please follow these guidelines:
|
||||||
|
|
||||||
* Add your code in a single JavaScript or TypeScript file per example, directly in the `examples` folder
|
- Add your code in a single JavaScript or TypeScript file per example, directly in the `examples` folder
|
||||||
* Do not introduce other dependencies in your example
|
- Do not introduce other dependencies in your example
|
||||||
* Give your `.js` file a meaningful name using `-` separators e.g. `adding-to-a-stream.js` / `adding-to-a-stream.ts`
|
- Give your `.js` file a meaningful name using `-` separators e.g. `adding-to-a-stream.js` / `adding-to-a-stream.ts`
|
||||||
* Indent your code using 2 spaces
|
- Indent your code using 2 spaces
|
||||||
* Use the single line `//` comment style and comment your code
|
- Use the single line `//` comment style and comment your code
|
||||||
* Add a comment at the top of your `.js` / `.ts` file describing what your example does
|
- Add a comment at the top of your `.js` / `.ts` file describing what your example does
|
||||||
* Add a comment at the top of your `.js` / `.ts` file describing any Redis commands that need to be run to set up data for your example (try and keep this minimal)
|
- Add a comment at the top of your `.js` / `.ts` file describing any Redis commands that need to be run to set up data for your example (try and keep this minimal)
|
||||||
* Use semicolons
|
- Use semicolons
|
||||||
* Use `async` and `await`
|
- Use `async` and `await`
|
||||||
* Use single quotes, `'hello'` not `"hello"`
|
- Use single quotes, `'hello'` not `"hello"`
|
||||||
* Use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) when embedding expressions in strings
|
- Use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) when embedding expressions in strings
|
||||||
* Unless your example requires a connection string, assume Redis is on the default localhost port 6379 with no password
|
- Unless your example requires a connection string, assume Redis is on the default localhost port 6379 with no password
|
||||||
* Use meaningful example data, let's not use `foo`, `bar`, `baz` etc!
|
- Use meaningful example data, let's not use `foo`, `bar`, `baz` etc!
|
||||||
* Leave an empty line at the end of your `.js` file
|
- Leave an empty line at the end of your `.js` file
|
||||||
* Update this `README.md` file to add your example to the table
|
- Update this `README.md` file to add your example to the table
|
||||||
|
|
||||||
Use [connect-as-acl-user.js](./connect-as-acl-user.js) as a guide to develop a well formatted example script.
|
Use [connect-as-acl-user.js](./connect-as-acl-user.js) as a guide to develop a well formatted example script.
|
||||||
|
|
||||||
@@ -87,5 +88,4 @@ await client.connect();
|
|||||||
// Add your example code here...
|
// Add your example code here...
|
||||||
|
|
||||||
await client.quit();
|
await client.quit();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
40
examples/transaction-with-watch.js
Normal file
40
examples/transaction-with-watch.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { createClient, WatchError } from 'redis';
|
||||||
|
|
||||||
|
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
const client = createClient();
|
||||||
|
await client.connect();
|
||||||
|
|
||||||
|
function restrictFunctionCalls(fn, maxCalls) {
|
||||||
|
let count = 1;
|
||||||
|
return function (...args) {
|
||||||
|
return count++ < maxCalls ? fn(...args) : false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn = restrictFunctionCalls(transaction, 4);
|
||||||
|
|
||||||
|
async function transaction() {
|
||||||
|
try {
|
||||||
|
await client.executeIsolated(async (isolatedClient) => {
|
||||||
|
await isolatedClient.watch('paymentId:1259');
|
||||||
|
const multi = isolatedClient
|
||||||
|
.multi()
|
||||||
|
.set('paymentId:1259', 'Payment Successfully Completed!')
|
||||||
|
.set('paymentId:1260', 'Refund Processed Successfully!');
|
||||||
|
await delay(5000); // Do some changes to the watched key during this time...
|
||||||
|
await multi.exec();
|
||||||
|
console.log('Transaction completed Successfully!');
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof WatchError) {
|
||||||
|
console.log('Transaction Failed Due To Concurrent Modification!');
|
||||||
|
fn();
|
||||||
|
} else {
|
||||||
|
console.log(`Error: ${error}`);
|
||||||
|
client.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction();
|
Reference in New Issue
Block a user