Google Antigravity Directory

The #1 directory for Google Antigravity prompts, rules, workflows & MCP servers. Optimized for Gemini 3 agentic development.

Resources

PromptsMCP ServersAntigravity RulesGEMINI.md GuideBest Practices

Company

Submit PromptAntigravityAI.directory

Popular Prompts

Next.js 14 App RouterReact TypeScriptTypeScript AdvancedFastAPI GuideDocker Best Practices

Legal

Privacy PolicyTerms of ServiceContact Us
Featured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver ToolsFeatured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver Tools

© 2026 Antigravity AI Directory. All rights reserved.

The #1 directory for Google Antigravity IDE

This website is not affiliated with, endorsed by, or associated with Google LLC. "Google" and "Gemini" are trademarks of Google LLC.

Antigravity AI Directory
PromptsMCPBest PracticesUse CasesLearn
Home
Prompts
Upstash Redis Caching Strategies

Upstash Redis Caching Strategies

Implement high-performance caching with Upstash Redis in Google Antigravity applications including cache invalidation and session management.

rediscachingperformancesessionupstash
by antigravity-team
⭐0Stars
.antigravity
# Upstash Redis Caching Strategies

Implement high-performance caching patterns with Upstash Redis in your Google Antigravity applications. This guide covers caching strategies, session management, cache invalidation, and real-world patterns.

## Redis Client Setup

Configure a type-safe Redis client with connection pooling:

```typescript
// lib/redis.ts
import { Redis } from "@upstash/redis";
import { Ratelimit } from "@upstash/ratelimit";

// Singleton Redis client
let redis: Redis | null = null;

export function getRedis(): Redis {
  if (!redis) {
    redis = new Redis({
      url: process.env.UPSTASH_REDIS_REST_URL!,
      token: process.env.UPSTASH_REDIS_REST_TOKEN!,
    });
  }
  return redis;
}

// Cache wrapper with automatic serialization
export class CacheClient {
  private redis: Redis;
  private defaultTTL: number;

  constructor(ttlSeconds = 3600) {
    this.redis = getRedis();
    this.defaultTTL = ttlSeconds;
  }

  async get<T>(key: string): Promise<T | null> {
    const data = await this.redis.get<T>(key);
    return data;
  }

  async set<T>(key: string, value: T, ttl?: number): Promise<void> {
    await this.redis.set(key, value, {
      ex: ttl || this.defaultTTL,
    });
  }

  async delete(key: string): Promise<void> {
    await this.redis.del(key);
  }

  async deletePattern(pattern: string): Promise<void> {
    const keys = await this.redis.keys(pattern);
    if (keys.length > 0) {
      await this.redis.del(...keys);
    }
  }

  async getOrSet<T>(
    key: string,
    fetcher: () => Promise<T>,
    ttl?: number
  ): Promise<T> {
    const cached = await this.get<T>(key);
    if (cached !== null) {
      return cached;
    }

    const fresh = await fetcher();
    await this.set(key, fresh, ttl);
    return fresh;
  }
}

export const cache = new CacheClient();
```

## Cache-Aside Pattern

Implement the cache-aside pattern for database queries:

```typescript
// lib/cached-queries.ts
import { cache } from "./redis";
import { db } from "./db";

export interface CacheOptions {
  ttl?: number;
  tags?: string[];
}

export async function getCachedPrompts(
  category?: string,
  options: CacheOptions = {}
) {
  const cacheKey = category 
    ? `prompts:category:${category}` 
    : "prompts:all";

  return cache.getOrSet(
    cacheKey,
    async () => {
      const prompts = await db
        .selectFrom("prompts")
        .selectAll()
        .where("is_approved", "=", true)
        .$if(!!category, (qb) =>
          qb.where("tags", "@>", [category!])
        )
        .orderBy("created_at", "desc")
        .execute();

      return prompts;
    },
    options.ttl || 300 // 5 minutes default
  );
}

export async function getCachedPromptById(id: string) {
  const cacheKey = `prompt:${id}`;

  return cache.getOrSet(
    cacheKey,
    async () => {
      const prompt = await db
        .selectFrom("prompts")
        .selectAll()
        .where("id", "=", id)
        .executeTakeFirst();

      return prompt || null;
    },
    600 // 10 minutes for individual prompts
  );
}
```

## Cache Invalidation

Implement smart cache invalidation strategies:

```typescript
// lib/cache-invalidation.ts
import { cache } from "./redis";

export class CacheInvalidator {
  // Invalidate specific prompt cache
  async invalidatePrompt(promptId: string): Promise<void> {
    await Promise.all([
      cache.delete(`prompt:${promptId}`),
      cache.deletePattern("prompts:*"), // Clear all list caches
    ]);
  }

  // Invalidate by category
  async invalidateCategory(category: string): Promise<void> {
    await cache.deletePattern(`prompts:category:${category}*`);
  }

  // Invalidate user-specific caches
  async invalidateUserCache(userId: string): Promise<void> {
    await cache.deletePattern(`user:${userId}:*`);
  }

  // Tag-based invalidation
  async invalidateByTags(tags: string[]): Promise<void> {
    const invalidations = tags.map((tag) =>
      cache.deletePattern(`*:tag:${tag}:*`)
    );
    await Promise.all(invalidations);
  }
}

export const invalidator = new CacheInvalidator();
```

## Session Management

Implement Redis-based session management:

```typescript
// lib/session.ts
import { getRedis } from "./redis";
import { nanoid } from "nanoid";

interface Session {
  userId: string;
  email: string;
  createdAt: number;
  expiresAt: number;
  metadata?: Record<string, unknown>;
}

export class SessionManager {
  private redis = getRedis();
  private readonly SESSION_PREFIX = "session:";
  private readonly SESSION_TTL = 7 * 24 * 60 * 60; // 7 days

  async createSession(userId: string, email: string): Promise<string> {
    const sessionId = nanoid(32);
    const session: Session = {
      userId,
      email,
      createdAt: Date.now(),
      expiresAt: Date.now() + this.SESSION_TTL * 1000,
    };

    await this.redis.set(
      `${this.SESSION_PREFIX}${sessionId}`,
      session,
      { ex: this.SESSION_TTL }
    );

    // Store user's active sessions for management
    await this.redis.sadd(`user:${userId}:sessions`, sessionId);

    return sessionId;
  }

  async getSession(sessionId: string): Promise<Session | null> {
    return this.redis.get<Session>(`${this.SESSION_PREFIX}${sessionId}`);
  }

  async destroySession(sessionId: string): Promise<void> {
    const session = await this.getSession(sessionId);
    if (session) {
      await this.redis.srem(`user:${session.userId}:sessions`, sessionId);
    }
    await this.redis.del(`${this.SESSION_PREFIX}${sessionId}`);
  }

  async destroyAllUserSessions(userId: string): Promise<void> {
    const sessionIds = await this.redis.smembers(`user:${userId}:sessions`);
    if (sessionIds.length > 0) {
      const keys = sessionIds.map((id) => `${this.SESSION_PREFIX}${id}`);
      await this.redis.del(...keys);
    }
    await this.redis.del(`user:${userId}:sessions`);
  }
}

export const sessionManager = new SessionManager();
```

## Best Practices

1. **TTL Strategy**: Set appropriate TTLs based on data volatility
2. **Key Namespacing**: Use consistent key prefixes for organization
3. **Serialization**: Use JSON for complex objects, avoid storing large blobs
4. **Connection Pooling**: Reuse connections across requests
5. **Error Handling**: Implement fallbacks when cache is unavailable
6. **Monitoring**: Track cache hit rates and memory usage

When to Use This Prompt

This redis prompt is ideal for developers working on:

  • redis applications requiring modern best practices and optimal performance
  • Projects that need production-ready redis code with proper error handling
  • Teams looking to standardize their redis development workflow
  • Developers wanting to learn industry-standard redis patterns and techniques

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.

How to Use

  1. Copy the prompt - Click the copy button above to copy the entire prompt to your clipboard
  2. Paste into your AI assistant - Use with Claude, ChatGPT, Cursor, or any AI coding tool
  3. Customize as needed - Adjust the prompt based on your specific requirements
  4. Review the output - Always review generated code for security and correctness
💡 Pro Tip: For best results, provide context about your project structure and any specific constraints or preferences you have.

Best Practices

  • ✓ Always review generated code for security vulnerabilities before deploying
  • ✓ Test the redis code in a development environment first
  • ✓ Customize the prompt output to match your project's coding standards
  • ✓ Keep your AI assistant's context window in mind for complex requirements
  • ✓ Version control your prompts alongside your code for reproducibility

Frequently Asked Questions

Can I use this redis prompt commercially?

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.

Which AI assistants work best with this prompt?

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.

How do I customize this prompt for my specific needs?

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.

Related Prompts

💬 Comments

Loading comments...