Advanced

AutoCache

CanxJS includes an intelligent "AutoCache" system that learns from traffic patterns and automatically caches slow, frequent GET requests.

How it Works

The PatternAnalyzer monitors your incoming requests. If a specific GET route is accessed frequently (high hits) and has a high average response time (>100ms), AutoCache marks it as "cacheable" and begins serving it from memory.

Configuration

src/app.ts
1import { createApp, autoCacheMiddleware } from "canxjs";
2
3const app = createApp();
4
5// Enable AutoCache
6// It automatically detects frequent, slow GET requests and caches them.
7app.use(autoCacheMiddleware({
8 enabled: true,
9 defaultTtl: 300, // 5 minutes
10 exclude: ['/api/auth/*', '/api/admin/*']
11}));

Manual Management

Terminal
# Clear all caches
bun run canx cache:clear
# Optimize (includes cache clear)
bun run canx optimize

Next Steps

Ensure your app is secure.