Skip to main content

Null Cache Driver

The Null Cache Driver disables caching. It is useful for testing, debugging, or when you want to turn off cache without changing code elsewhere.

When to Use

  • Testing environments
  • Debugging cache-related issues
  • Temporarily disabling cache

Best For

  • Ensuring your app works without cache
  • Measuring performance impact of caching

Limitations

  • No data is cached, ever
  • Not for production use

Configuration

src/config/cache.ts
import { cache, NullCacheDriver } from "@warlock.js/core";

cache.setConfigurations({
default: "null",
drivers: {
null: NullCacheDriver,
},
options: {
null: {
// No specific options needed
},
},
});

Options

The Null Cache Driver accepts any options but ignores them all. This makes it compatible with the cache interface while providing no functionality.

OptionTypeDefaultDescription
*anyundefinedAny options are accepted but ignored

Example Options (Ignored)

{
// All of these are ignored by the null driver
globalPrefix: "test",
ttl: 3600,
host: "localhost",
port: 6379,
customOption: "value",
}

Behavior

The Null Cache Driver implements all cache methods but provides no actual caching:

  • set(): Always returns the value (no storage)
  • get(): Always returns null (no data retrieved)
  • remove(): Always succeeds (no data to remove)
  • flush(): Always succeeds (no data to clear)
  • removeNamespace(): Always succeeds (no data to clear)

Example Usage

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

// These operations appear to work but don't actually cache anything
await cache.set("key", "value", 3600); // Returns "value"
const result = await cache.get("key"); // Returns null
await cache.remove("key"); // Succeeds
await cache.flush(); // Succeeds

Use Cases

Testing

// In your test configuration
const testConfig = {
default: "null",
drivers: { null: NullCacheDriver },
options: { null: {} }
};

Debugging

// Temporarily disable cache to isolate issues
const debugConfig = {
default: "null",
drivers: { null: NullCacheDriver },
options: { null: {} }
};

Performance Measurement

// Compare performance with and without cache
const withCache = await measurePerformance(() => {
return cache.get("expensive-operation");
});

// Switch to null driver
cache.use("null");

const withoutCache = await measurePerformance(() => {
return cache.get("expensive-operation");
});

Troubleshooting

  • Why is nothing cached? This is expected! Switch to another driver for real caching.
  • Performance is the same with/without cache? The null driver is working correctly - it's not caching anything.
  • Need to re-enable cache? Switch back to a real driver like memory or redis.