[data-reveal]{opacity:1!important;transform:none!important}
Redis

Interface Redis Documentation

Overview

Interface Redis provides direct access to a Redis client instance through a simple, promise-based API. It exposes a single GetClient() function that returns an ioredis client, giving you full access to all Redis commands including key-value operations, pub/sub messaging, transactions, and more.

Getting Started

Install the package in your project:

npm install @antelopejs/interface-redis

GetClient

The GetClient function retrieves the initialized Redis client instance. This is the main entry point for all Redis operations.

import { GetClient } from "@antelopejs/interface-redis";

async function example() {
  const client = await GetClient();
  // The client is a standard ioredis instance
}

The function returns a Promise<Redis> that resolves to the ioredis client once the connection is established.

Key-Value Operations

Redis excels at key-value storage. Use the client to store, retrieve, and manage data.

Store and retrieve values

import { GetClient } from "@antelopejs/interface-redis";

async function storeUserData(userId: string, data: object) {
  const client = await GetClient();

  // Store a JSON value
  await client.set(`user:${userId}`, JSON.stringify(data));

  // Retrieve it later
  const raw = await client.get(`user:${userId}`);
  if (raw) {
    const userData = JSON.parse(raw);
    console.log(userData);
  }
}

Use expiration for temporary data

import { GetClient } from "@antelopejs/interface-redis";

async function cacheResponse(key: string, data: string, ttlSeconds: number) {
  const client = await GetClient();

  // Store with an expiration time
  await client.set(key, data, "EX", ttlSeconds);
}

async function getCache(key: string): Promise<string | null> {
  const client = await GetClient();
  return client.get(key);
}

Work with hashes

import { GetClient } from "@antelopejs/interface-redis";

async function manageUserProfile(userId: string) {
  const client = await GetClient();

  // Store multiple fields in a hash
  await client.hset(`profile:${userId}`, {
    name: "Alice",
    email: "[email protected]",
    role: "admin",
  });

  // Retrieve a single field
  const name = await client.hget(`profile:${userId}`, "name");

  // Retrieve all fields
  const profile = await client.hgetall(`profile:${userId}`);
  console.log(profile); // { name: "Alice", email: "[email protected]", role: "admin" }
}

Pub/Sub Messaging

Redis pub/sub enables real-time messaging between different parts of your application. Use duplicate() to create a dedicated subscriber connection, since a subscribed client cannot issue other commands.

Subscribe to a channel

import { GetClient } from "@antelopejs/interface-redis";

async function setupNotifications() {
  const client = await GetClient();
  const subscriber = client.duplicate({ lazyConnect: true });
  await subscriber.connect();

  await subscriber.subscribe("notifications");

  subscriber.on("message", (channel, message) => {
    console.log(`[${channel}] ${message}`);
  });
}

Publish messages

import { GetClient } from "@antelopejs/interface-redis";

async function sendNotification(message: string) {
  const client = await GetClient();
  await client.publish("notifications", message);
}

Pattern-based subscriptions

import { GetClient } from "@antelopejs/interface-redis";

async function subscribeToUserEvents() {
  const client = await GetClient();
  const subscriber = client.duplicate({ lazyConnect: true });
  await subscriber.connect();

  // Subscribe to all channels matching the pattern
  await subscriber.psubscribe("user:*:events");

  subscriber.on("pmessage", (pattern, channel, message) => {
    console.log(`Pattern: ${pattern}, Channel: ${channel}, Message: ${message}`);
  });
}

Additional Operations

The ioredis client supports the full range of Redis commands. Here are a few common patterns.

Lists

import { GetClient } from "@antelopejs/interface-redis";

async function manageQueue() {
  const client = await GetClient();

  // Push items to a list
  await client.lpush("task-queue", "task-1", "task-2");

  // Pop an item from the list
  const task = await client.rpop("task-queue");
  console.log(task); // "task-1"
}

Sets

import { GetClient } from "@antelopejs/interface-redis";

async function trackUniqueVisitors(pageId: string, visitorId: string) {
  const client = await GetClient();

  await client.sadd(`visitors:${pageId}`, visitorId);

  const count = await client.scard(`visitors:${pageId}`);
  console.log(`Unique visitors: ${count}`);
}

Transactions

import { GetClient } from "@antelopejs/interface-redis";

async function transferCredits(fromUser: string, toUser: string, amount: number) {
  const client = await GetClient();

  // Execute multiple commands atomically
  const results = await client
    .multi()
    .decrby(`credits:${fromUser}`, amount)
    .incrby(`credits:${toUser}`, amount)
    .exec();

  console.log("Transfer complete:", results);
}

For the complete list of available commands, refer to the ioredis documentation and the official Redis command reference.