lock<T> static method

Future<T?> lock<T>(
  1. String key,
  2. FutureOr<T> body(), {
  3. Duration ttl = const Duration(seconds: 30),
  4. Duration? wait,
})

Runs body while holding the lock named key, and releases it afterwards whether body returns or throws.

Returns what body returns, or null when another holder has the lock. With wait, a caller that finds the lock held keeps trying for that long before giving up with null.

ttl bounds how long a holder that died can wedge the lock; a body that runs longer than it can find a second holder beside it. Across processes the lock is only as atomic as the store: Redis and Memcached take it with a compare-and-set, memory and a database serve one process.

Implementation

static Future<T?> lock<T>(
  String key,
  FutureOr<T> Function() body, {
  Duration ttl = const Duration(seconds: 30),
  Duration? wait,
}) => const DVCache()._lock<T>(key, body, ttl: ttl, wait: wait);