Skip to content

TryLock

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

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

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.

Examples

Skip work that is already running

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var token = $Cache.TryLock('DailySummary');

if (token != null) {
    try {
        // create the daily summary
    }
    finally {
        $Cache.ReleaseLock('DailySummary', token);
    }
}
else {
    console.log('The daily summary is already being created.');
}

See Also