Cache Management
StoneJS provides a powerful caching system to dramatically improve performance. Cache entire components, database queries, API responses, or computed values with simple, intuitive APIs.
Cache Statistics
| Store Type: | disk |
| Cache Size: | 0 items |
| Default TTL: | 7200 seconds |
| Page Renders: | 1 |
Component Caching with cacheSelf()
Cache entire component output with a single call - perfect for expensive page generation, dhandlers, and high-traffic pages.
<%%init> section - that's it!
Quick Example
Interactive Demo
Visit /demo/cache-self-demo.html to see cacheSelf() in action! That page caches itself for 1 minute - refresh it multiple times to see the timestamp stay frozen, then wait a minute and watch it update.
Try the live demo:
- Open the cacheSelf() demo page
- Note the "Page Generated At" timestamp
- Refresh multiple times - timestamp doesn't change (cached!)
- Wait 1 minute and refresh - timestamp updates (cache expired)
More Examples
Default Caching (URI-based)
Custom Key for User-Specific Caching
Dhandler with Per-URL Caching
API Reference
$cache.cacheSelf(options)
| Option | Type | Default | Description |
|---|---|---|---|
key |
string | $req.path |
Custom cache key (optional) |
expiresIn |
string|number | 3600 | TTL in human format ('3 hours') or seconds |
busyLock |
string|number | '30 sec' | Busy lock timeout to prevent cache stampede |
Time Format Examples
| Format | Seconds |
|---|---|
| '30 sec' | 30 |
| '1 minute' | 60 |
| '5 minutes' | 300 |
| '1 hour' | 3600 |
| '3 hours' | 10800 |
| '1 day' | 86400 |
| '7 days' | 604800 |
| '1 week' | 604800 |
Data Cache API Reference
| Method | Description |
|---|---|
await $cache.set(key, value, ttl) |
Store a value in cache with TTL (in seconds or human format) |
await $cache.get(key) |
Retrieve a value from cache (returns null if not found or expired) |
await $cache.forget(key) |
Delete a specific cache key (alias: delete()) |
await $cache.remember(key, ttl, fn) |
Get from cache or execute callback and cache result (recommended pattern) |
await $cache.flush() |
Clear all cached data |
$cache.getStats() |
Get cache statistics (size, store type, default TTL) |
Partial Caching (Data & Fragments)
For fine-grained control, cache specific data or HTML fragments within your components using the $cache API.
Cache Remember Pattern (Recommended)
Live Demo - Cached Timestamp (30 second TTL):
2025-12-26T10:47:48.278Z
Reload the page multiple times - this timestamp won't change for 30 seconds!
The remember pattern automatically handles cache miss and sets cache. The callback only runs if cache is empty:
Interactive Cache Operations
1. Set Cache Value
2. Get Cache Value
3. Delete Cache Value
Basic Cache Operations
Set a value
Get a value
Delete a value
Clear all cache
Handle cache miss manually
Fragment Caching
Cache expensive HTML generation for better performance:
Common Caching Patterns
1. Database Query Results
Cache database query results to reduce database load:
2. API Responses
Cache external API responses to improve response time and reduce API calls:
3. Computed Values
Cache expensive computations:
4. User-Specific Cache
Use dynamic cache keys for user-specific data:
Advanced Topics
Cache Invalidation
When data changes, invalidate the cache:
Cache Stampede Prevention
Both cacheSelf() and remember() automatically prevent cache stampedes. When cache expires under high load, only one request regenerates the data while others wait:
Cache Configuration
Configure cache behavior in your application:
Performance Benefits
Typical Performance Improvements:
- Component Caching: 100-1000x faster for cached pages
- Database Queries: 10-100x faster
- API Calls: 100-1000x faster
- Complex Computations: 50-500x faster
- Server Impact: Reduced load and database connections
Best Practices
- Use cacheSelf() for expensive pages - Especially dhandlers and database-heavy pages
- Use remember() for data - Cache queries, API calls, and computations
- Choose appropriate TTLs - Balance freshness vs. performance
- Don't cache rapidly changing data - Or use very short TTLs
- Use descriptive cache keys - Makes debugging easier
- Invalidate on updates - Delete cache when underlying data changes
- Monitor cache hit rates - Track effectiveness in production
- Consider user-specific keys - Use custom keys for personalized content
Clear All Cache
This will clear all cached data from the cache store, including both component and data caches.