Introduction
High-performance caching and Pub/Sub messaging with Redis. Store provides a unified interface for key-value caching, atomic operations, and event-driven communication.
Overview
The Store adapter provides a powerful, type-safe interface for caching and Pub/Sub messaging in Igniter.js applications. Built on Redis, it enables high-performance data storage, retrieval, and real-time event broadcasting across your application. Whether you need to cache expensive database queries, manage user sessions, or build real-time features, the Store adapter gives you the tools to optimize performance and enable event-driven architectures.
The Store adapter abstracts away the complexity of Redis operations, providing a clean, intuitive API that handles serialization, connection management, and error handling automatically. This makes it easy to add caching and real-time features to your application without worrying about the underlying implementation details.
The Store adapter enables both caching capabilities and real-time features via Pub/Sub messaging. Adding a store adapter also automatically enables the .realtime service for building real-time applications.
Key Features
- Key-Value Caching: Store and retrieve data with automatic JSON serialization
- TTL Support: Set expiration times on cached values to keep data fresh
- Pub/Sub Messaging: Publish and subscribe to channels for event-driven communication
- Atomic Operations: Increment counters and manage key expiration atomically
- Type Safety: Full TypeScript inference from store operations to cached values
- Adapter Architecture: Works with any Redis-compatible backend via the adapter pattern
Architecture
The Store system follows Igniter.js's adapter pattern, providing a unified interface (IgniterStoreAdapter) that can be implemented by different storage backends. The Redis adapter (@igniter-js/adapter-redis) is the production-ready implementation, but you can create custom adapters for in-memory storage, Memcached, or any other storage backend that fits your needs.
This architecture provides flexibility—you can swap storage backends without changing your application code. The adapter handles all the low-level details like serialization, connection management, and error handling, so you can focus on building features rather than managing infrastructure.
graph LR
A[Your Application] --> B[IgniterStoreAdapter Interface]
B --> C[Redis Adapter]
C --> D[Redis Server]
B -.-> E[Other Adapters]
E --> F[In-Memory<br/>Memcached<br/>etc.]The adapter handles:
- Serialization: Automatic JSON stringify/parse for complex objects
- Client Management: Separate clients for commands and subscriptions (Redis requirement)
- Error Handling: Graceful fallbacks and error recovery
Quick Start
Get started with the Store adapter in minutes. This quick start guide shows you how to install dependencies, create the adapter, register it with Igniter, and start using it in your actions. Follow these steps to add high-performance caching and Pub/Sub messaging to your application.
Quick Start Guide
Install Dependencies
Install the Redis adapter and its peer dependency:
npm install @igniter-js/adapter-redis ioredispnpm add @igniter-js/adapter-redis ioredisyarn add @igniter-js/adapter-redis ioredisbun add @igniter-js/adapter-redis ioredisCreate the Adapter
Create a Redis client and wrap it with the Store adapter:
// src/services/store.ts
import { createRedisStoreAdapter } from '@igniter-js/adapter-redis';
import { Redis } from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);
export const store = createRedisStoreAdapter(redis);Register with Igniter
Register the store adapter in your main Igniter configuration:
// src/igniter.ts
import { Igniter } from '@igniter-js/core';
import { createIgniterAppContext } from '@/igniter.context';
import { store } from './services/store';
export const igniter = Igniter
.context(createIgniterAppContext())
.store(store)
.create();Use in Your Actions
Use igniter.store to access the store service in your handlers:
export const usersController = igniter.controller({
name: 'Users',
description: 'Manage user accounts and data',
path: '/users',
actions: {
list: igniter.query({
name: 'List Users',
description: 'Retrieve a list of all users with caching',
path: '/',
handler: async ({ context, response }) => {
// Check cache first
const cached = await igniter.store.get<User[]>('users:list');
if (cached) {
return response.success({ users: cached, source: 'cache' });
}
// Fetch from database
const users = await context.db.users.findMany();
// Cache for 5 minutes
await igniter.store.set('users:list', users, { ttl: 300 });
return response.success({ users, source: 'database' });
},
}),
},
});Core Concepts
The Store adapter provides three main capabilities that work together to enable high-performance applications. Understanding these concepts helps you use the Store effectively in your application, whether you're caching data, building real-time features, or managing distributed state.
Use Cases
The Store adapter enables many powerful use cases in modern applications. Here are some common patterns that demonstrate how you can use caching, Pub/Sub messaging, and atomic operations to build high-performance, scalable applications.