Skip to main content

Cache Configurations

Cache configurations define which drivers are available, the default driver, and options for each driver.

Environment Variable Parsing

The examples in this documentation use @mongez/dotenv for environment variable parsing, but this is just an example. You can use any environment variable parser you prefer, such as:

  • dotenv (most popular)
  • @mongez/dotenv
  • process.env (Node.js built-in)
  • envalid (with validation)
  • Or any other package of your choice

The cache package is framework-agnostic and has no dependency on any specific environment variable parser.

Configuration Structure

A cache configuration object has three main properties:

  • drivers - Object mapping driver names to driver classes
  • default - The name of the default driver to use
  • options - Object mapping driver names to their configuration options

Basic Example

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

const cacheConfigurations: CacheConfigurations = {
drivers: {
redis: RedisCacheDriver,
},
default: env("CACHE_DRIVER", "redis"),
options: {
redis: {
host: env("REDIS_HOST"),
port: env("REDIS_PORT"),
password: env("REDIS_PASSWORD"),
},
},
};

export default cacheConfigurations;

After defining your configuration, you'll need to set it and initialize the cache manager. See Cache Manager for details on using setCacheConfigurations() and init().

Multiple Drivers

You can define multiple drivers and switch between them at runtime:

src/config/cache.ts
import { join } from "path";
import {
FileCacheDriver,
RedisCacheDriver,
MemoryCacheDriver,
LRUMemoryCacheDriver,
MemoryExtendedCacheDriver,
CACHE_FOR
} from "@warlock.js/cache";

const cacheConfigurations = {
drivers: {
redis: RedisCacheDriver,
file: FileCacheDriver,
memory: MemoryCacheDriver,
lru: LRUMemoryCacheDriver,
memoryExtended: MemoryExtendedCacheDriver,
},
default: "redis",
options: {
redis: {
host: "localhost",
globalPrefix: "myapp",
ttl: CACHE_FOR.ONE_HOUR,
},
file: {
directory: join(process.cwd(), "storage", "cache"),
globalPrefix: "myapp",
},
memory: {
ttl: CACHE_FOR.ONE_HOUR,
globalPrefix: "myapp",
},
lru: {
capacity: 1000,
globalPrefix: "myapp",
},
memoryExtended: {
ttl: CACHE_FOR.ONE_HOUR,
globalPrefix: "myapp",
},
},
};

export default cacheConfigurations;

Once configured, you can switch between drivers at runtime using the cache manager. See Switching Drivers for details.

Custom Drivers

Include your custom drivers in the configuration:

src/config/cache.ts
import { CacheConfigurations } from "@warlock.js/cache";
import { MyCustomCacheDriver } from "./my-custom-driver";

const cacheConfigurations: CacheConfigurations<"custom"> = {
drivers: {
custom: MyCustomCacheDriver,
},
default: "custom",
options: {
custom: {
globalPrefix: "myapp",
ttl: 3600,
// ... other custom options
},
},
};

export default cacheConfigurations;
TypeScript Type Safety

When using custom drivers, you must specify the driver name(s) as a generic parameter to CacheConfigurations. See Make Your Own Cache Driver for more details.

Learn how to create custom drivers.

Environment-Based Configuration

Conditionally configure drivers based on your environment:

src/config/cache.ts
import { env } from "@mongez/dotenv";
import { join } from "path";
import {
CacheConfigurations,
RedisCacheDriver,
FileCacheDriver,
MemoryCacheDriver,
CACHE_FOR
} from "@warlock.js/cache";

const isProduction = process.env.NODE_ENV === "production";

const cacheConfigurations: CacheConfigurations = {
drivers: {
redis: RedisCacheDriver,
file: FileCacheDriver,
memory: MemoryCacheDriver,
},
default: isProduction ? "redis" : "memory",
options: {
redis: {
host: env("REDIS_HOST", "localhost"),
port: env("REDIS_PORT", 6379),
password: env("REDIS_PASSWORD"),
globalPrefix: env("APP_NAME", "warlock"),
ttl: env("CACHE_TTL", CACHE_FOR.ONE_HOUR),
},
file: {
directory: env("CACHE_DIRECTORY") || join(process.cwd(), "storage", "cache"),
globalPrefix: env("APP_NAME", "warlock"),
},
memory: {
globalPrefix: env("APP_NAME", "warlock"),
ttl: env("CACHE_TTL", CACHE_FOR.ONE_HOUR),
},
},
};

export default cacheConfigurations;

Driver Options

Each driver has its own configuration options. See the individual driver documentation for detailed options:

Troubleshooting

  • Driver not found? Make sure it's listed in the drivers object with the correct name.
  • Wrong driver used? Check the default property matches a driver name in drivers.
  • Options not applied? Ensure the key in options matches the driver name in drivers.