Medusa/developer documentation/Cache

Cache

In-memory cache stores for images, renders, messages, and any module data that is expensive to fetch.

medusa.cache holds named in-memory stores. Each store keeps entries for a fixed lifetime, evicts the least recently used entry once it is full, and sweeps expired entries every minute. Nothing is written to disk, and every store starts empty after a restart.

The cache is never a stand-in for the database. Settings, audit records, and module models always read and write the database directly, and Medusa stops if the database cannot be reached. Only cache data that can be fetched or rebuilt again.

Stores

modules/example/src/handlers/profiles.js
async profile(userId) {
    return this.medusa.cache.store("example.profiles", { ttl: 300000, max: 1000 })
        .remember(userId, () => this.fetch(userId));
}

store(name, options) returns the same store on every call, so options only apply the first time. Prefix store names with the module id to keep them apart.

MethodSignatureDescription
get(key)(key: any) => anyReturns the value, or undefined when missing or expired.
set(key, value, ttl?)(key: any, value: any, ttl?: number) => anyStores the value and returns it. ttl overrides the store lifetime for this entry.
has(key)(key: any) => booleanChecks for a live entry.
remember(key, load, ttl?)(key: any, load: () => Promise<any>, ttl?: number) => Promise<any>Returns the cached value, or runs load and caches its result. Concurrent calls for the same key share one load. null and undefined results are not cached.
delete(key)(key: any) => booleanRemoves one entry.
clear()() => voidRemoves every entry.
stats()() => objectname, size, max, ttl, hits, and misses.

Built-in Stores

StoreLifetimeLimitHolds
canvas.images30 minutes300Images loaded by medusa.canvas.image(url), keyed by URL.
canvas.renders5 minutes100PNG buffers from medusa.canvas.render, keyed by a hash of the layout, data, and scale.

Messages

Discord messages are cached by discord.js on each channel (channel.messages.cache). Medusa limits every channel to the 200 most recent messages and sweeps messages older than 30 minutes every 5 minutes. channel.messages.fetch(id) reads that cache before calling Discord, so repeated lookups of the same panel or review message stay local.