RedisLock

Distributed lock that is implemented based on the Redis in-memory database.

Extends: Lock

Implements: IReferenceable, IConfigurable, IOpenable

Description

The RedisLock class allows you to create a distributed lock that is implemented based on the Redis in-memory database.

Configuration parameters

  • connection(s):
    • discovery_key: (optional) key to retrieve the connection from IDiscovery
    • host: host name or IP address
    • port: port number
    • uri: resource URI or connection string with all parameters in it
  • credential(s):
    • store_key: key to retrieve parameters from credential store
    • username: username (currently is not used)
    • password: user’s password
  • options:
    • retry_timeout: timeout in milliseconds to retry lock acquisition. (Default: 100)
    • retries: number of retries (default: 3)

References

  • *:discovery:*:*:1.0 - (optional) IDiscovery services
  • *:credential-store:*:*:1.0 - (optional) ICredentialStore to resolve credentials

Instance methods

close

Closes the component and frees used resources.

@override

Future close(IContext? context)

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Configures a component by passing its configuration parameters.

@override

void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

isOpen

Checks if the component is open.

@override

bool isOpen()

  • returns: bool - true if the component is open and false otherwise.

open

Opens the component.

@override

Future open(IContext? context)

  • context: IContext - (optional) a context to trace execution through a call chain.

releaseLock

Releases a prevously acquired lock by its key.

@override

Future releaseLock(IContext? context, String key)

  • context: IContext - (optional) a context to trace execution through a call chain.
  • key: String - unique lock key to release.

setReferences

Sets references to dependent components.

@override

void setReferences(IReferences references)

  • references: IReferences - references to locate the component dependencies.

tryAcquireLock

Makes a single attempt to acquire a lock by its key. It returns immediately a positive or negative result.

@override

Future<bool> tryAcquireLock(IContext? context, String key, int ttl)

  • context: IContext - (optional) a context to trace execution through a call chain.
  • key: String - unique lock key to acquire.
  • ttl: int - lock timeout (time to live) in milliseconds.
  • returns: Future<bool> - true if lock was successfully acquired and false otherwise.

Examples

var lock = RedisLock();
lock.configure(ConfigParams.fromTuples([
  'host', 'localhost',
  'port', 6379
]));

await lock.open('123');
  ...
await lock.acquire('123', 'key1');

try {
    // Processing...
} finally {
   await lock.releaseLock('123', 'key1');
    // Continue...
}