Skip to main content

Runtime Cache

Keep cache logic in services, not controllers.

Runtime setup

import { CacheManager, setupCache } from "@alpha.consultings/eloquent-orm.js";

setupCache();

Environment behavior

APP_ENV=production
MEMCACHED_HOST=127.0.0.1
MEMCACHED_PORT=11211
CACHE_DIR=.cache
Current runtime behavior:
  • APP_ENV=development: memory cache
  • APP_ENV=staging: file cache using CACHE_DIR
  • APP_ENV=production: Memcached, then file cache, then memory cache

Read-through pattern

async function activeUsers() {
  const key = "users:active:v1";
  const cached = await CacheManager.get(key);
  if (cached) return cached;

  const users = await User.where("is_active", true)
    .orderBy("created_at", "desc")
    .limit(20)
    .get();

  await CacheManager.set(key, users, 60);
  return users;
}

Write-side invalidation

async function createUser(data: Record<string, unknown>) {
  const created = await User.create(data);
  await CacheManager.delete("users:active:v1");
  return created;
}

CLI helpers

eloquent cache:stats
eloquent cache:clear