Skip to main content

Cache Configurations

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

Basic Usage

Create src/config/cache.ts:

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"),
port: env("REDIS_PORT"),
password: env("REDIS_PASSWORD"),
},
},
};

export default cacheConfigurations;

Advanced: Multiple Drivers

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

src/config/cache.ts
import { 
FileCacheDriver,
RedisCacheDriver,
MemoryCacheDriver,
DatabaseCacheDriver
} from "@warlock.js/core";
import { CacheModel } from "./models/CacheModel";

const cacheConfigurations = {
drivers: {
redis: RedisCacheDriver,
file: FileCacheDriver,
memory: MemoryCacheDriver,
database: DatabaseCacheDriver,
},
default: "redis",
options: {
redis: {
host: "localhost",
globalPrefix: "myapp",
ttl: 3600,
},
file: {
directory: "./tmp/cache",
globalPrefix: "myapp",
},
memory: {
ttl: 3600,
globalPrefix: "myapp",
},
database: {
model: CacheModel,
globalPrefix: "myapp",
ttl: 3600,
},
},
};

Using a Custom Driver

You can add your own driver:

import { MyCustomCacheDriver } from "./my-custom-driver";

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

Environment-Based Configuration

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

const isProduction = environment() === "production";
const isDevelopment = environment() === "development";

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", 3600),
},
file: {
directory: env("CACHE_DIRECTORY", "./storage/cache"),
globalPrefix: env("APP_NAME", "warlock"),
},
memory: {
globalPrefix: env("APP_NAME", "warlock"),
ttl: env("CACHE_TTL", 3600),
},
},
};

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 drivers.
  • Wrong driver used? Check the default property and your environment variables.
  • Options not applied? Ensure the options object matches your driver names.
  • Database driver error? Make sure the model is properly configured and the table exists.

See Cache Manager for runtime switching and more advanced usage.