name: upstash-redis description: Upstash Redis integration for serverless caching and data storage. Use when implementing Redis caching, rate limiting, or session storage with Upstash.
Upstash Redis Integration
Upstash provides serverless Redis.
Guidelines
- Use the
@upstash/redispackage to interact with Upstash Redis. - The integration uses the
KV_REST_API_URLandKV_REST_API_TOKENenvironment variables.
Environment Variables
KV_REST_API_URL- Upstash Redis REST API URLKV_REST_API_TOKEN- Upstash Redis REST API token
Example Usage
import { Redis } from '@upstash/redis'
const redis = new Redis({
url: process.env.KV_REST_API_URL,
token: process.env.KV_REST_API_TOKEN,
})
// Set a value
await redis.set('key', 'value')
// Get a value
const value = await redis.get('key')
// Set with expiration (seconds)
await redis.set('session', sessionData, { ex: 3600 })
// Increment
await redis.incr('counter')