Skip to content

TryLock

Attempts to acquire a lock for a shared key without blocking.

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

Parameters

string key
    The shared lock key.

Returns

The token value as a string if the lock is acquired; otherwise, null.

Remarks

This method immediately checks whether the shared key is locked and generates a token if the shared key is not locked. It does not perform any wait operation for availability. If you want to wait for a short period, use the $Cache.Lock method instead.

Example

1
2
3
4
5
6
7
var tokenValue = $Cache.TryLock('mylock');
if (tokenValue != null) {
  // Locked code block
  $Cache.ReleaseLock('mylock',tokenValue1);
} else {
  // Already locked
}

See Also