Skip to main content

Memory Cache Driver

The memory cache driver stores data in memory, it will be persisted until the application is restarted.

Driver name

The default name for the driver is memory.

Options

The memory cache driver has the following options:

  • globalPrefix: A prefix that will be added to all keys. This is useful when you want to use the same cache driver for multiple applications, it could be a string or a callback that returns a string, if not provided then there will no be prefix for the keys.
  • ttl: Time to live in seconds, the default value is Infinity.

Usage

To use the memory cache driver, you need to define it in the drivers list in the cache configurations file:

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

const cacheConfigurations: CacheConfigurations = {
drivers: {
memory: MemoryCacheDriver,
},
default: env("CACHE_DRIVER", "memory"),
options: {
memory: {
globalPrefix: "online-store",
ttl: 60 * 60 * 24, // 24 hours
},
},
};

export default cacheConfigurations;

This will allow the cache manager to pick it if there is no CACHE_DRIVER environment variable defined.

We set the global prefix to online-store, this will be added to all keys. This is useful when you want to use the same cache driver for multiple applications, it could be a string or a callback that returns a string, if not provided then there will no be prefix for the keys.

All cache keys will remain for 24 hours, after that they will be removed.

note

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