Application caching for Google Antigravity IDE
# Caching Strategies Guide for Google Antigravity
Master caching in Google Antigravity IDE. This guide covers in-memory caching, CDN caching, and cache invalidation.
## In-Memory Caching
```typescript
import { LRUCache } from "lru-cache";
const cache = new LRUCache<string, unknown>({
max: 500,
ttl: 1000 * 60 * 5 // 5 minutes
});
export async function getCached<T>(
key: string,
fetcher: () => Promise<T>,
ttl?: number
): Promise<T> {
const cached = cache.get(key) as T | undefined;
if (cached !== undefined) return cached;
const data = await fetcher();
cache.set(key, data, { ttl });
return data;
}
// Usage
const user = await getCached(
`user:${userId}`,
() => db.user.findUnique({ where: { id: userId } }),
60000
);
```
## Next.js Caching
```typescript
// Fetch with caching
async function getPost(id: string) {
const response = await fetch(`https://api.example.com/posts/${id}`, {
next: {
revalidate: 3600, // Cache for 1 hour
tags: ["posts", `post-${id}`]
}
});
return response.json();
}
// Revalidation
import { revalidateTag, revalidatePath } from "next/cache";
async function updatePost(id: string, data: PostData) {
await db.post.update({ where: { id }, data });
revalidateTag(`post-${id}`);
revalidatePath(`/posts/${id}`);
}
```
## Response Headers
```typescript
import { NextResponse } from "next/server";
export async function GET() {
const data = await fetchData();
return NextResponse.json(data, {
headers: {
"Cache-Control": "public, s-maxage=60, stale-while-revalidate=300"
}
});
}
```
## Best Practices
1. **Cache at multiple levels** - Browser, CDN, server
2. **Use cache tags** - Granular invalidation
3. **Set appropriate TTLs** - Balance freshness and performance
Google Antigravity IDE provides caching scaffolding.This Caching 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 caching 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 Caching projects, consider mentioning your framework version, coding style, and any specific libraries you're using.