@42technologies/node-redis-warlock 中文文档教程

发布于 3年前 浏览 22 项目主页 更新于 3年前

warlock

Join the chat at https://gitter.im/TheDeveloper/warlock

使用 redis 的久经考验的分布式锁定。

Requirements

  • node-redis compatible with v0.10
  • Redis v2.6.12 or above. If you're running a Redis version from v2.6.0 to v2.6.11 inclusive use v0.0.7 of this module.

Install

npm install node-redis-warlock

Usage

const Warlock = require('node-redis-warlock');
const Redis = require('redis');

// Establish a redis client and pass it to warlock
const redis = Redis.createClient();
const warlock = Warlock(redis);

// Set a lock
const key = 'test-lock';
const ttl = 10000; // Lifetime of the lock

warlock.lock(key, ttl, (err, unlock) => {
  if (err) {
    // Something went wrong and we weren't able to set a lock
    return;
  }

  if (typeof unlock === 'function') {
    // If the lock is set successfully by this process, an unlock function is passed to our callback.
    // Do the work that required lock protection, and then unlock() when finished...
    //
    // do stuff...
    //
    unlock();
  } else {
    // Otherwise, the lock was not established by us so we must decide what to do
    // Perhaps wait a bit & retry...
  }
});

// set a lock optimistically
const key = 'opt-lock';
const ttl = 10000;
const maxAttempts = 4; // Max number of times to try setting the lock before erroring
const wait = 1000; // Time to wait before another attempt if lock already in place
warlock.optimistic(key, ttl, maxAttempts, wait, (err, unlock) => {});

// unlock using the lock id
var key = 'test-lock-2';
var ttl = 10000;
let lockId;

warlock.lock(key, ttl, (err, _, id) => {
  lockId = id;
});

// each client who knows the lockId can release the lock
warlock.unlock(key, lockId, (err, result) => {
  if (result == 1) {
    // unlocked successfully
  }
});

// change a lock's ttl
var key = 'touch-lock';
var ttl = 10000;
var ttl2 = 20000;

warlock.lock(key, ttl, function(err, unlock, id) {
  warlock.touch(key, id, ttl2, function(err) {});
});

ProTips

warlock

Join the chat at https://gitter.im/TheDeveloper/warlock

Battle-hardened distributed locking using redis.

Requirements

  • node-redis compatible with v0.10
  • Redis v2.6.12 or above. If you're running a Redis version from v2.6.0 to v2.6.11 inclusive use v0.0.7 of this module.

Install

npm install node-redis-warlock

Usage

const Warlock = require('node-redis-warlock');
const Redis = require('redis');

// Establish a redis client and pass it to warlock
const redis = Redis.createClient();
const warlock = Warlock(redis);

// Set a lock
const key = 'test-lock';
const ttl = 10000; // Lifetime of the lock

warlock.lock(key, ttl, (err, unlock) => {
  if (err) {
    // Something went wrong and we weren't able to set a lock
    return;
  }

  if (typeof unlock === 'function') {
    // If the lock is set successfully by this process, an unlock function is passed to our callback.
    // Do the work that required lock protection, and then unlock() when finished...
    //
    // do stuff...
    //
    unlock();
  } else {
    // Otherwise, the lock was not established by us so we must decide what to do
    // Perhaps wait a bit & retry...
  }
});

// set a lock optimistically
const key = 'opt-lock';
const ttl = 10000;
const maxAttempts = 4; // Max number of times to try setting the lock before erroring
const wait = 1000; // Time to wait before another attempt if lock already in place
warlock.optimistic(key, ttl, maxAttempts, wait, (err, unlock) => {});

// unlock using the lock id
var key = 'test-lock-2';
var ttl = 10000;
let lockId;

warlock.lock(key, ttl, (err, _, id) => {
  lockId = id;
});

// each client who knows the lockId can release the lock
warlock.unlock(key, lockId, (err, result) => {
  if (result == 1) {
    // unlocked successfully
  }
});

// change a lock's ttl
var key = 'touch-lock';
var ttl = 10000;
var ttl2 = 20000;

warlock.lock(key, ttl, function(err, unlock, id) {
  warlock.touch(key, id, ttl2, function(err) {});
});

ProTips

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文