Implement high-performance caching with Redis in Google Antigravity including session storage and rate limiting
# Redis Caching for Google Antigravity
Redis provides fast in-memory data storage. This guide establishes patterns for using Redis with Google Antigravity projects.
## Redis Client Setup
```typescript
import { Redis } from "@upstash/redis";
export const redis = new Redis({
url: process.env.UPSTASH_REDIS_URL!,
token: process.env.UPSTASH_REDIS_TOKEN!,
});
export async function cacheGet<T>(key: string): Promise<T | null> {
return redis.get(key);
}
export async function cacheSet<T>(key: string, value: T, ttl?: number): Promise<void> {
if (ttl) await redis.set(key, value, { ex: ttl });
else await redis.set(key, value);
}
```
## Caching API Responses
```typescript
export async function getProductsWithCache(categoryId: string) {
const cacheKey = `products:${categoryId}`;
const cached = await redis.get<Product[]>(cacheKey);
if (cached) return cached;
const products = await db.product.findMany({ where: { categoryId } });
await redis.set(cacheKey, products, { ex: 3600 });
return products;
}
```
## Rate Limiting
```typescript
import { Ratelimit } from "@upstash/ratelimit";
const ratelimit = new Ratelimit({
redis,
limiter: Ratelimit.slidingWindow(10, "10 s"),
});
export async function checkRateLimit(identifier: string) {
const { success, remaining } = await ratelimit.limit(identifier);
return { allowed: success, remaining };
}
```
## Best Practices
1. **TTL strategy**: Set appropriate expiration
2. **Key naming**: Use consistent conventions
3. **Cache invalidation**: Invalidate on data changes
4. **Connection pooling**: Reuse connections
5. **Error handling**: Handle failures gracefullyThis Redis prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their redis implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Redis projects, consider mentioning your framework version, coding style, and any specific libraries you're using.