Cache Configuration

Use the cache block on new MonSQLize() to configure database read caching.

import MonSQLize from 'monsqlize';

const msq = new MonSQLize({
  type: 'mongodb',
  databaseName: 'shop',
  config: { uri: 'mongodb://localhost:27017/shop' },
  cache: {
    maxEntries: 100_000,
    enableStats: true,
    autoInvalidate: false
  }
});

Options

OptionTypeDefaultDescription
cache.maxEntriesnumber100000Maximum number of local memory cache entries.
cache.maxMemorynumber0Maximum memory budget in bytes. 0 means unlimited.
cache.defaultTtlnumberimplementation defaultDefault TTL used by direct cache writes. Query caching still uses the per-query cache TTL.
cache.enableStatsbooleantrueTrack hits, misses, sets, deletes, and evictions.
cache.enabledbooleantrueDisable the cache subsystem when set to false.
cache.autoInvalidatebooleanfalseBroadly invalidate collection read caches after successful monSQLize writes.
cache.distributedobjectdisabledRedis Pub/Sub invalidation for multi-instance deployments.

Query TTL

Read caching is opt-in per query:

const products = await msq.collection('products').find(
  { category: 'phone' },
  { cache: 30_000, limit: 20 }
);

cache on a read option is a TTL in milliseconds. Omitting it means the query is not cached.

Write Invalidation Default

Writes do not invalidate read caches by default. Enable broad invalidation globally only when that tradeoff is acceptable:

const msq = new MonSQLize({
  type: 'mongodb',
  databaseName: 'shop',
  config: { uri },
  cache: {
    autoInvalidate: true
  }
});

For precise write paths, prefer per-write cache.invalidate. See Cache Invalidation.