MemcachedLock

Distributed lock that is implemented based on Memcached’s caching service.

Extends: Lock

Implements: IConfigurable, IReferenceable, IOpenable

Description

The MemcachedLock class allows you to create a lock that is implemented based on the Memcached’s caching service.

Important points

  • The current implementation does not support authentication.

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
  • options:
    • max_size: maximum number of values stored in this cache (default: 1000)
    • max_key_size: maximum key length (default: 250)
    • max_expiration: maximum expiration duration in milliseconds (default: 2592000)
    • max_value: maximum value length (default: 1048576)
    • pool_size: pool size (default: 5)
    • reconnect: reconnection timeout in milliseconds (default: 10 sec)
    • retries: number of retries (default: 3)
    • timeout: default caching timeout in milliseconds (default: 1 minute)
    • failures: number of failures before stop retrying (default: 5)
    • retry: retry timeout in milliseconds (default: 30 sec)
    • idle: idle timeout before disconnect in milliseconds (default: 5 sec)

References

  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve connection

Instance methods

close

Closes a component and frees used resources.

public close(context: IContext): Promise<void>

  • context: IContext - (optional) a context to trace execution through a 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(context: IContext): Promise<void>

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

releaseLock

Releases a prevously acquired lock by its key.

public releaseLock(context: IContext, key: string): Promise<void>

  • 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.

public setReferences(references: IReferences): void

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

tryAcquireLock

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

public tryAcquireLock(context: IContext, key: string, ttl: number): Promise<boolean>

  • context: IContext - (optional) a context to trace execution through a 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 successfull and false otherwise.

Examples

let lock = new MemcachedLock();
lock.configure(ConfigParams.fromTuples(
  "host", "localhost",
  "port", 11211
));
await lock.open("123");
await lock.acquire("123", "key1");
try {
  // Processing...
} finally {
  await lock.releaseLock("123", "key1");
}