Skip to content

Commit

Permalink
fix: complementing README and defining keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Oct 8, 2023
1 parent 033a11c commit 99f7166
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![npm version](https://badge.fury.io/js/%40remembered-redis%2Fredis-semaphore.svg)](https://badge.fury.io/js/%40remembered-redis%2Fredis-semaphore)

Remembered redis semaphore implementation, to be used with [@remembered/redis](https://www.npmjs.com/package/@remembered/redis)

This packages uses [redis-semaphore](https://www.npmjs.com/package/redis-semaphore) under the hood.
## How to Install

```
Expand All @@ -19,7 +19,7 @@ npm i remembered-redis-semaphore
When instantiating **RememberedRedis**, just inform an instance os **RememberedRedisSemaphore**

```ts
const semaphore = new RememberedRedisSemaphore(IORedisIstanceForSemaphore, {
const semaphore = new RememberedRedisSemaphore(IORedisInstanceForSemaphore, {

})

Expand All @@ -30,6 +30,50 @@ const remembered = new RememberedRedis({
}, IORedisInstance);
```

Although this package is meant to use as a plugin for RememberedRedis, you can also use it as a completely standalone one.

It offers three different approaches to use semaphores.

The first and most basic one is through acquiring and releasing it manually, like this:

```ts
const release = await semaphore.acquire('my semaphore id');
try {
// Put here anything you want to do
} finally {
await release();
}
```

It's always recommended to put the release call into a finally statement, to guarantee it was released.
You can also use the callback approach, where it is executed within the semaphore, like this:

```ts
const result = await semaphore.run('my semaphroe id', () => {
// Put here anything you want to do
});
```

result here will be the result of the informed callback.
Finally, the last possible approach is to wrap a function, making a version of it that do the same job, but within a semaphore. Like this:

```ts
const wrapped = semaphore.wrap((arg1, arg2, arg3) => {
// Put here anything you want to do
}, (arg1, arg2, arg3) => {
// Put here the algorithm to select the key based on the callback parameters
});


const result = await wrapped(value1, value2, value3);
```

See that, the first callback is the one that will be executed within a semaphore, while the second one
must return the key to be used in the semaphore. The second callback will received the exact same
parameters of the first one. You can use wrapped to replace instance methods for versions wrapped in a semaphore, or to
create functions that will be passed as a callback to other ones, that will use a semaphore.


## License

Licensed under [MIT](https://en.wikipedia.org/wiki/MIT_License).
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@
"bugs": {
"url": "https://github.com/codibre/remembered-redis-semaphore/issues"
},
"keywords": [],
"keywords": [
"remembered",
"redis",
"semaphore",
"mutex",
"wrapper"
],
"license": "MIT",
"devDependencies": {
"@codibre/confs": "^1.1.2",
Expand Down

0 comments on commit 99f7166

Please sign in to comment.