Use Cases
User Sessions
Section titled “User Sessions”Store session data with automatic expiration:
await kv.put('myapp', `session:${sessionId}`, { userId: '123', loginTime: Date.now()});Feature Flags
Section titled “Feature Flags”Toggle features dynamically:
const { value } = await kv.get('myapp', 'config:features');if (value.newFeature) { // Show new feature}Cache Layer
Section titled “Cache Layer”Cache API responses:
const cacheKey = `cache:api:${endpoint}`;let data = await kv.get('myapp', cacheKey).catch(() => null);
if (!data) { data = await fetchFromAPI(endpoint); await kv.put('myapp', cacheKey, data);}User Preferences
Section titled “User Preferences”Store user settings:
await kv.put('myapp', `user:${userId}:settings`, { theme: 'dark', notifications: true});Real-time Counters
Section titled “Real-time Counters”Track analytics:
const date = new Date().toISOString().split('T')[0];const key = `stats:daily:${date}`;const { value } = await kv.get('myapp', key);await kv.put('myapp', key, { count: (value?.count || 0) + 1 });