> ## Documentation Index
> Fetch the complete documentation index at: https://alphaconsultings.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Runtime Cache

> Cache behavior at the service boundary, environment selection, and read-through invalidation patterns.

# Runtime Cache

Keep cache logic in services, not controllers.

## Runtime setup

```ts theme={null}
import { CacheManager, setupCache } from "@alpha.consultings/eloquent-orm.js";

setupCache();
```

## Environment behavior

```env theme={null}
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

```ts theme={null}
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

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

## CLI helpers

```bash theme={null}
eloquent cache:stats
eloquent cache:clear
```

## Related pages

* [Runtime CRUD](./crud)
* [Services](../getting-started/services)
* [CLI Commands](../cli/commands)
