Skip to content

Lock

Tries to acquire a lock for the enclosure key until the lock is retrieved.

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

Tries to acquire a lock for shared key, until lock has acquired or timeout.

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

Parameters

string key
    Lock key. Key is a unique identifier for shared access. Required.

string blockingTimeout
    Duration of waiting timeout in hh:mm:ss format. Default value is 60 second.

Returns

Acquired lock token if successful. Otherwise returns null. Acquired lock token if successful. Otherwise returns null.

Remarks

This method automatically enters to wait state for shared key is set to free until timeout is reached. If you do not want to wait please use the $Cache.TryLock method instead.
Acquired lock always must be released with $Cache.ReleaseLock method with generated token value. Otherwise lock is kept for long period until it's expire. (Default 5 min)

Try to wait for a lock for a shared key and release it after 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 to wait state for shared key is set to free until timeout is reached. If you do not want to wait please use the $Cache.TryLock method instead.
Acquired lock always must be released with $Cache.ReleaseLock method with generated token value. Otherwise lock is kept for long period until it's expire. (Default 5 min)

Example

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