Cache
Aerostack Cache is backed by Cloudflare KV — a globally replicated key-value store with sub-millisecond read latency at the edge.
Quick start
import { sdk } from '@aerostack/sdk'
// Set a value with TTL
await sdk.cache.set('user:profile:123', userData, { ttl: 3600 }) // 1 hour
// Get a value
const cached = await sdk.cache.get('user:profile:123')
if (!cached) {
// Cache miss — fetch from DB
}
// Delete
await sdk.cache.delete('user:profile:123')Methods
| Method | Description |
|---|---|
sdk.cache.set(key, value, options?) | Store a value |
sdk.cache.get(key) | Retrieve a value (null if missing or expired) |
sdk.cache.delete(key) | Remove a key |
sdk.cache.has(key) | Check if a key exists |
Options
await sdk.cache.set(key, value, {
ttl: 300, // seconds until expiry (default: no expiry)
metadata: { // optional metadata stored alongside the value
userId: '123',
source: 'db'
}
})Cache-aside pattern
async function getUser(userId: string) {
const cacheKey = `user:${userId}`
// 1. Try cache
const cached = await sdk.cache.get(cacheKey)
if (cached) return cached
// 2. Fetch from DB
const user = await sdk.db.queryOne(
'SELECT * FROM users WHERE id = ?',
[userId]
)
// 3. Cache the result
if (user) {
await sdk.cache.set(cacheKey, user, { ttl: 300 })
}
return user
}Cache invalidation
// On profile update, invalidate the cached profile
async function updateUser(userId: string, updates: object) {
await sdk.db.query(
'UPDATE users SET name = ? WHERE id = ?',
[updates.name, userId]
)
// Invalidate cache
await sdk.cache.delete(`user:${userId}`)
}Cloudflare KV has eventual consistency — writes may take up to 60 seconds to propagate globally. For strong consistency requirements, use the database directly.