You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Redis-based distributed locking for coordinating access to shared resources across multiple processes or services. Built on top of SE.Redis's LockTake/LockRelease/LockExtend primitives.
API Reference
Low-Level API
Method
Description
LockTakeAsync(key, value, expiry)
Acquire a lock with a specific holder value
LockReleaseAsync(key, value)
Release a lock (only if held by the specified value)
LockExtendAsync(key, value, expiry)
Extend the lock TTL (only if held by the specified value)
LockQueryAsync(key)
Query the current lock holder value (null if not locked)
Acquire a lock with retry logic, returns IRedisLock
IRedisLock properties and methods:
Member
Description
Key
The lock key
Value
The unique holder identifier (auto-generated GUID)
IsAcquired
Whether the lock was successfully acquired
ExtendAsync(expiry)
Extend the lock TTL
DisposeAsync()
Release the lock automatically
Usage
Using the IAsyncDisposable wrapper (recommended)
// Acquire a lock with automatic release on disposeawaitusingvarlockObj=awaitredis.LockAcquireAsync("resource:order:123",expiry:TimeSpan.FromSeconds(30),maxRetries:5,retryDelay:TimeSpan.FromMilliseconds(200));if(lockObjisnull){// Could not acquire the lock after all retriesthrownewInvalidOperationException("Could not acquire lock");}// Critical section — lock is heldawaitProcessOrder(123);// Lock is automatically released when `lockObj` is disposed
Extending a lock
awaitusingvarlockObj=awaitredis.LockAcquireAsync("long-task",TimeSpan.FromSeconds(10));if(lockObjis not null){// ... doing work ...// Need more time? Extend the lockvarextended=awaitlockObj.ExtendAsync(TimeSpan.FromSeconds(30));if(!extended){// Lock was lost (expired before we could extend)return;}// ... continue work ...}