https://github.com/cnuggets/nlockjs
Node.js distributed lock based on Redis
https://github.com/cnuggets/nlockjs
distributed nodejs redis-based-lock
Last synced: 4 months ago
JSON representation
Node.js distributed lock based on Redis
- Host: GitHub
- URL: https://github.com/cnuggets/nlockjs
- Owner: cnuggets
- License: mit
- Created: 2023-10-23T03:20:28.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-23T06:00:08.000Z (almost 3 years ago)
- Last Synced: 2024-04-23T22:10:21.477Z (over 2 years ago)
- Topics: distributed, nodejs, redis-based-lock
- Language: JavaScript
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nlockjs
Node.js distributed lock based on Redis
## Getting Started
### Download
```
npm install nlockjs
```
### Usage
#### Redis lock
```
var Lock = require("nlockjs").Lock;
// New redis lock
var l = new Lock(15, {
endpoints: "localhost:6379", // Redis endpoints
password: "" // Redis password
});
// Lock
l.lock(key).then(function (ok) {
// If has lock
l.hasLock(key).then(function (yes) {
assert.equal(yes, true);
// Unlock
l.unlock(key).then(function(ok) {
assert.equal(ok, true);
}, function(err) {
console.error(err.toString());
});
}, function (err) {
console.error(err.toString());
});
}, function (err) {
console.error(err.toString());
});
```
#### Lock with waiting time
```
// Lock waiting for 5 seconds
l.lockWait(key, 5).then(function (ok) {
assert.equal(ok, true);
}, function (err) {
console.error(err.toString());
});
```
### Methods
```
- lock(key) // lock
```
```
- lockWait(key, seconds) // lock with waiting time
```
```
- isLocked(key) // check if locked
```
```
- hasLock(key) // check if has/own lock
```
```
- Unlock(key) // unlock
```
## License
nlockjs is under the MIT license. See the [LICENSE](LICENSE) for detail.