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.
public
close(correlationId: string): Promise<void>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
configure
Configures a component by passing its configuration parameters.
public
configure(config: ConfigParams): void
- config: ConfigParams - configuration parameters to be set.
isOpen
Checks if the component is open.
public
isOpen(): boolean
- returns: boolean - true if the component is open and false otherwise.
open
Opens the component.
public
open(correlationId: string): Promise<void>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
releaseLock
Releases a prevously acquired lock by its key.
public
releaseLock(correlationId: string, key: string): Promise<void>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
- key: string - unique lock key to release.
setReferences
Sets references to dependent components.
public
setReferences(references: IReferences): void
- 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.
public
tryAcquireLock(correlationId: string, key: string, ttl: number): Promise<boolean>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
- key: string - unique lock key to acquire.
- ttl: number - lock timeout (time to live) in milliseconds.
- returns: Promise<boolean> - true if lock was successfully acquired and false otherwise.
Examples
let lock = new RedisRedis();
lock.configure(ConfigParams.fromTuples(
"host", "localhost",
"port", 6379
));
lock.open("123");
await lock.acquire("123", "key1");
try {
// Processing...
} finally {
await lock.releaseLock("123", "key1");
}