Advanced Caching
Overview
Basic Cache Configuration
Simple Caching Setup
import { IHttpClient, CacheInterceptor } from '@aurelia/fetch-client';
import { DI, resolve } from '@aurelia/kernel';
export class CachedApiService {
private http = resolve(IHttpClient);
constructor() {
// Create cache interceptor with basic configuration
const cacheInterceptor = DI.getGlobalContainer().invoke(CacheInterceptor, [{
cacheTime: 300_000, // Cache valid for 5 minutes
staleTime: 60_000, // Data becomes stale after 1 minute
}]);
this.http.configure(config => config.withInterceptor(cacheInterceptor));
}
async getUser(id: string) {
// This request will be automatically cached
const response = await this.http.get(`/api/users/${id}`);
return response.json();
}
}Cache Configuration Options
Understanding Cache Timing
Cache Service API
Accessing the Cache Service
Cache Service Methods
set() and get()
setItem() and getItem()
delete() and clear()
Cache Events System
Available Cache Events
Subscribing to Cache Events
Practical Event Monitoring Examples
Cache Performance Monitoring
Cache Debugging Dashboard
Background Refresh
Basic Background Refresh
Conditional Background Refresh
Storage Backends
Memory Storage (Default)
LocalStorage Backend
SessionStorage Backend
IndexedDB Backend
Choosing the Right Storage Backend
Use Case
Recommended Backend
Reason
Custom Storage Implementation
Complete Caching Example
Best Practices
1. Choose Appropriate Cache Times
2. Monitor Cache Performance
3. Use Background Refresh Strategically
4. Handle Cache Invalidation
Summary
Last updated
Was this helpful?