Skip to main content

Redis Cache Driver

Redis is a high-performance, in-memory data store. It is ideal for distributed, production-grade caching.

When to Use

  • Production environments
  • Multi-server or distributed systems
  • Need for persistence, high availability, or large cache sizes

Best For

  • Caching API/database responses
  • Session storage
  • Shared cache between multiple app instances

Limitations

  • Requires Redis server setup
  • Slightly more operational overhead than memory/file

Alternatives

Setup

By default Redis is shipped with Warlock so you don't need to install any dependencies.

Configurations

Go to the Cache Configurations file, and add the following:

src/config/cache.ts
import { env } from "@mongez/dotenv";
import { CacheConfigurations, RedisCacheDriver } from "@warlock.js/core";

const cacheConfigurations: CacheConfigurations = {
drivers: {
redis: RedisCacheDriver,
},
default: env("CACHE_DRIVER", "redis"),
options: {
redis: {
host: env("REDIS_HOST", "localhost"),
port: env("REDIS_PORT", 6379),
username: env("REDIS_USERNAME"),
password: env("REDIS_PASSWORD"),
globalPrefix: "prod-app",
ttl: 60 * 60 * 24, // 24 hours
},
},
};

export default cacheConfigurations;
tip

Make sure Redis is installed on your local machine and on the server otherwise it will throw an error.

Options

OptionTypeDefaultDescription
hoststring"localhost"Redis server host
portnumber6379Redis server port
usernamestringundefinedRedis username for authentication
passwordstringundefinedRedis password for authentication
urlstringundefinedRedis connection URL (overrides host/port)
globalPrefixstring | FunctionundefinedGlobal prefix for all cache keys
ttlnumberInfinityDefault TTL in seconds
clientOptionsobject{}Additional Redis client options

Connection Options

{
host: "localhost", // Redis host
port: 6379, // Redis port
username: "default", // Redis username
password: "your-password", // Redis password
url: "redis://user:pass@host:port", // Alternative: use URL
}

Global Prefix

{
globalPrefix: "myapp", // Static prefix
// OR
globalPrefix: () => `app-${Date.now()}`, // Dynamic prefix
}

TTL Configuration

{
ttl: 3600, // 1 hour default TTL
// OR
ttl: 60 * 60 * 24, // 24 hours
// OR
ttl: Infinity, // Never expire (default)
}

Advanced Client Options

{
clientOptions: {
retry_strategy: (options) => {
if (options.total_retry_time > 1000 * 60 * 60) {
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
return undefined;
}
return Math.min(options.attempt * 100, 3000);
},
max_attempts: 3,
connect_timeout: 10000,
}
}

Example Usage

Storing and Retrieving Data

import { cache } from "@warlock.js/core";

await cache.set("user:2", { name: "Bob" }, 3600);
const user = await cache.get("user:2");

Accessing the Redis Client

import { cache } from "@warlock.js/core";
const redisClient = cache.client;

Troubleshooting

  • Connection errors? Check your Redis server is running and credentials are correct.
  • Data not shared between servers? Make sure all instances use the same Redis server.
note

Please note that the Redis Cache Drier implements all methods in Cache Driver Interface so you can use it directly as a cache driver.