在锁定密钥时获取异常 redis-lock nodejs
Getting exception on locking the key redis-lock nodejs
当尝试如下锁定散列键时出现异常:
我指的是以下文档:
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)
请指出我的代码有什么问题。
没关系,这是我的错误。我错过了前缀 'locks:' 到键。
如果有人遇到问题,可以参考这个,顺便说一下,在文档中没有提到前缀 'locks:' 到 key.
当尝试如下锁定散列键时出现异常:
我指的是以下文档:
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)
请指出我的代码有什么问题。
没关系,这是我的错误。我错过了前缀 'locks:' 到键。
如果有人遇到问题,可以参考这个,顺便说一下,在文档中没有提到前缀 'locks:' 到 key.