Skip to content

Lock

Attempts to acquire a lock for the specified key, blocking until the lock is acquired.

1
$Cache.Lock(key: string):string

Attempts to acquire a lock for the specified key, blocking for a specified duration until the lock is acquired or the timeout expires.

1
$Cache.Lock(key: string, blockingTimeout: string):string

Parameters

string key
    The lock key, which is a unique identifier for shared access. Required.

string blockingTimeout
    The duration to wait for the lock in "hh:mm:ss" format. The default value is 60 seconds.

Returns

The acquired lock token if successful; otherwise, null. The acquired lock token if successful; otherwise, null.

Remarks

This method automatically enters a wait state until the shared key is free or a timeout is reached. If you do not want to wait, use the $Cache.TryLock method instead.
Acquired locks must always be released using the $Cache.ReleaseLock method with the generated token value. Otherwise, the lock is held for an extended period until it expires (default: 5 minutes).

Trying to acquire a lock, waiting if necessary, and releasing it after the operation is completed:

1
2
3
4
5
6
7
var tokenValue = $Cache.Lock('mylock');
try {
  // Locked code block
}
finally{
  $Cache.ReleaseLock('mylock',tokenValue);
}

This method automatically enters a wait state until the shared key is free or the timeout is reached. If you do not want to wait, use the $Cache.TryLock method instead.
Acquired locks must always be released using the $Cache.ReleaseLock method with the generated token value. Otherwise, the lock is held for an extended period until it expires (default: 5 minutes).

Waiting for 30 seconds if the lock is already held:

1
2
3
4
5
6
7
8
// Wait for 30 seconds if already locked
var tokenValue = $Cache.Lock('mylock', '00:00:30');
try {
  // Locked code block
}
finally {
  $Cache.ReleaseLock('mylock',tokenValue);
}

See Also