锁定密钥redis-lock nodejs时获得异常

时间:2017-10-02 05:59:40

标签: node.js redis node-redis

尝试锁定哈希键时,如下所示:

我指的是以下文档:

https://redis.io/topics/distlock

https://github.com/mike-marcacci/node-redlock

var redis = require('redis');
var Redlock = require('redlock');
var redisClient = redis.createClient(settings.redisport, settings.redishost);
var redlock = new Redlock(
// you should have one client for each independent redis node
// or cluster
[redisClient],
{
    // the expected clock drift; for more details
    // see http://redis.io/topics/distlock
    driftFactor: 0.01, // time in ms

    // the max number of times Redlock will attempt
    // to lock a resource before erroring
    retryCount: 10,

    // the time in ms between attempts
    retryDelay: 200, // time in ms

    // the max time in ms randomly added to retries
    // to improve performance under high contention
    // see https://www.awsarchitectureblog.com/2015/03/backoff.html
    retryJitter: 200 // time in ms
 }
);

redlock.lock([Key], settings.lockttl).then(function (lock) {
    return lock.unlock();
}).catch(function (err) {

    return lock.unlock();
});


ERR Error running script (call to f_cf0e94b2e9ffc7e04395cf88f7583fc309985910): @user_script:1: WRONGTYPE Operation against a key holding the wrong kind of value
Exceeded 10 attempts to lock the resource "Room:1111".
Unhandled rejection ReferenceError: lock is not defined
    at app.js:559:17
    at tryCatcher (bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (bluebird\js\release\promise.js:689:18)
    at Async._drainQueue (bluebird\js\release\async.js:133:16)
    at Async._drainQueues (bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)

请告诉我的代码有什么问题。

1 个答案:

答案 0 :(得分:1)

没关系,这是我的错。我错过了将'lock:'添加到密钥前缀。

如果有人遇到问题,那么可以参考一下,顺便提一下,文档中没有提到前缀'lock:'来键入。