0

I am using the the following code for redis lock and release

var key = "test-x";
RedisValue token = (RedisValue) Guid.NewGuid().ToString();
if(db.LockTake(key, token, duration)) {
    try {
        // you have the lock do work
    } finally {
        db.LockRelease(key, token);
    }
}

My problem:

In a unit test I am calling this method 2 times. The first time always work, but the second time I want to obtain the lock on this specific key, it does not work. From my understanding the db.LockRelease should release the lock, making it available for the second request. I did notice that db.LockRelease returns false.

Any idea what might be happening?

Community
  • 1
  • 1
Blottt
  • 119
  • 1
  • 13

1 Answers1

1

The lock key needs to be unique. You are probably using the same lock key as the cache key in you code. From https://stackoverflow.com/a/25138164:

the key (the unique name of the lock in the database)

Community
  • 1
  • 1