Redis — RedisManager

Manages an ioredis connection to Redis. Automatically connected when 'redis' is included in the databases array during PlatformClient.init().


Import

import { RedisManager } from '@mpratyush54/sdk-node';

// Access via client:
// client.db.redis

Constructor

const redis = new RedisManager(config?: RedisConfig);

RedisConfig

Field Environment variable Default
host PLATFORM_REDIS_HOST localhost
port PLATFORM_REDIS_PORT 6379
password PLATFORM_REDIS_PASSWORD (none)
username PLATFORM_REDIS_USERNAME (none)
keyPrefix PLATFORM_REDIS_KEY_PREFIX (none)
sentinels PLATFORM_REDIS_SENTINELS (host:port,host:port) (none)
masterName PLATFORM_REDIS_SENTINEL_MASTER mymaster
sentinelUsername PLATFORM_REDIS_SENTINEL_USERNAME (none)
sentinelPassword PLATFORM_REDIS_SENTINEL_PASSWORD (none)

Sentinel (ha profile)

When sentinels is set (an array of { host, port } or a host:port,... string), the manager asks Sentinel for the current master instead of using host/port. On Cairn's ha profile /api/sdk/db-credentials returns these fields, so PlatformClient.init() uses Sentinel automatically. After a failover ioredis reconnects to the new master by itself, and a command rejected by a demoted master (READONLY) is retried on the new one; no restart is needed. redis.mode is 'sentinel' or 'standalone'.


Methods

connect()

await redis.connect(): Promise<void>

Creates an ioredis client with lazyConnect: true and calls connect(). On failure, schedules automatic reconnection with exponential backoff (up to 5 retries, max 30 s delay).

get(key)

await redis.get(key: string): Promise<string | null>

Returns the string value for key, or null if the key does not exist.

set(key, value, ttl?)

await redis.set(key: string, value: string, ttl?: number): Promise<void>

Sets key to value. If ttl is provided, uses SETEX with the TTL in seconds; otherwise uses plain SET.

del(key)

await redis.del(key: string): Promise<void>

Deletes key.

disconnect()

await redis.disconnect(): Promise<void>

Closes the Redis connection via client.quit().


Properties

Property Type Description
redis.isConnected boolean Whether the client is connected
redis.health { connected, mode } Health-check summary
redis.mode 'sentinel' | 'standalone' How the master is found

Auto-connect via PlatformClient

When databases: ['redis'] is passed to init(), credentials are fetched automatically from the Cairn API (GET /api/sdk/db-credentials), and the manager is available at client.db.redis immediately after init() resolves.


Full Example

import { PlatformClient } from '@mpratyush54/sdk-node';

const client = new PlatformClient();

async function main() {
  await client.init({
    projectName: 'my-service',
    platformUrl: 'https://platform.example.com',
    databases: ['redis'],
  });

  const redis = client.db.redis!;

  // Set with 1-hour TTL
  await redis.set('session:abc123', JSON.stringify({ userId: 42 }), 3600);

  // Get
  const raw = await redis.get('session:abc123');
  const session = raw ? JSON.parse(raw) : null;
  console.log('Session:', session);

  // Delete
  await redis.del('session:abc123');

  await client.shutdown();
}

main();

Error Handling

  • Connection failure is non-blocking — isConnected remains false.
  • Exponential backoff reconnection is attempted automatically (5 retries max).
  • get() returns null if not connected (instead of throwing).
  • set() and del() silently no-op if not connected.