Skip to main content
Version: Next

Redis Plugin

The Redis plugin provides a cache provider for CommandKit that allows you to store cached data in Redis.

Installation

npm install @commandkit/redis

Usage

This plugin automatically registers the Redis cache provider with your CommandKit instance.

import { defineConfig } from 'commandkit';
import { redis } from '@commandkit/redis';

export default defineConfig({
plugins: [redis()],
});

With this plugin configured, your cache functions will automatically use Redis as the storage backend:

async function getCachedData() {
'use cache'; // This directive enables caching for the function

// Your data retrieval logic
const data = await getFromDatabase('something');

return data;
}

Manual Configuration

If you need more control over the Redis client configuration, you can set up the cache provider manually instead of using the plugin:

import { setCacheProvider } from '@commandkit/cache';
import { RedisCacheProvider } from '@commandkit/redis';
import { Redis } from 'ioredis';

// Configure the Redis client with custom options
const redis = new Redis({
host: 'your-redis-host',
port: 6379,
// Add other Redis options as needed
});

const redisProvider = new RedisCacheProvider(redis);

// Register the provider with CommandKit
setCacheProvider(redisProvider);

This approach gives you full control over the Redis client configuration while still integrating with CommandKit's caching system.