Agent skill

redis-patterns

Upstash Redis patterns for caching and rate limiting.

Stars 232
Forks 15

Install this agent skill to your Project

npx add-skill https://github.com/aiskillstore/marketplace/tree/main/skills/barnhardt-enterprises-inc/redis-patterns

SKILL.md

Upstash Redis Patterns

Setup

typescript
// lib/redis.ts
import { Redis } from '@upstash/redis';

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

Basic Caching

typescript
// Cache with TTL
async function getCachedUser(id: string): Promise<User | null> {
  const cacheKey = `user:${id}`;

  // Try cache first
  const cached = await redis.get<User>(cacheKey);
  if (cached) return cached;

  // Fetch from DB
  const user = await db.query.users.findFirst({
    where: eq(users.id, id),
  });

  if (user) {
    // Cache for 5 minutes
    await redis.setex(cacheKey, 300, user);
  }

  return user;
}

Cache Invalidation

typescript
// Invalidate on update
async function updateUser(id: string, data: UpdateUserInput): Promise<User> {
  const user = await db.update(users)
    .set(data)
    .where(eq(users.id, id))
    .returning();

  // Invalidate cache
  await redis.del(`user:${id}`);

  // Also invalidate list caches
  await redis.del('users:list');

  return user[0];
}

Rate Limiting

typescript
import { Ratelimit } from '@upstash/ratelimit';

const ratelimit = new Ratelimit({
  redis,
  limiter: Ratelimit.slidingWindow(10, '10 s'), // 10 requests per 10 seconds
  analytics: true,
});

// In API route or middleware
export async function POST(request: Request) {
  const ip = request.headers.get('x-forwarded-for') ?? 'anonymous';
  const { success, limit, reset, remaining } = await ratelimit.limit(ip);

  if (!success) {
    return new Response('Too Many Requests', {
      status: 429,
      headers: {
        'X-RateLimit-Limit': limit.toString(),
        'X-RateLimit-Remaining': remaining.toString(),
        'X-RateLimit-Reset': reset.toString(),
      },
    });
  }

  // Process request...
}

Session Storage

typescript
interface Session {
  userId: string;
  expiresAt: number;
}

async function createSession(userId: string): Promise<string> {
  const sessionId = crypto.randomUUID();
  const session: Session = {
    userId,
    expiresAt: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days
  };

  await redis.setex(`session:${sessionId}`, 7 * 24 * 60 * 60, session);
  return sessionId;
}

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

async function deleteSession(sessionId: string): Promise<void> {
  await redis.del(`session:${sessionId}`);
}

Pub/Sub for Real-time

typescript
// Publisher
async function publishEvent(channel: string, data: unknown): Promise<void> {
  await redis.publish(channel, JSON.stringify(data));
}

// Usage
await publishEvent('user:updates', { userId: '123', action: 'updated' });

Leaderboard

typescript
// Add score
await redis.zadd('leaderboard', { score: 100, member: 'user:123' });

// Get top 10
const topUsers = await redis.zrevrange('leaderboard', 0, 9, { withScores: true });

// Get user rank
const rank = await redis.zrevrank('leaderboard', 'user:123');

Cache Patterns

typescript
// Cache-aside pattern
async function getData<T>(
  key: string,
  fetcher: () => Promise<T>,
  ttl: number = 300
): Promise<T> {
  const cached = await redis.get<T>(key);
  if (cached) return cached;

  const data = await fetcher();
  await redis.setex(key, ttl, data);
  return data;
}

// Usage
const user = await getData(
  `user:${id}`,
  () => db.query.users.findFirst({ where: eq(users.id, id) }),
  300
);

Expand your agent's capabilities with these related and highly-rated skills.

aiskillstore/marketplace

perigon-backend

Perigon ASP.NET Core + EF Core + Aspire conventions

232 15
Explore
aiskillstore/marketplace

perigon-agent

Pointers for Copilot/agents to apply Perigon conventions

232 15
Explore
aiskillstore/marketplace

perigon-angular

Angular 21+ standalone/Material/signal conventions for Perigon WebApp

232 15
Explore
aiskillstore/marketplace

fastapi-mastery

Comprehensive FastAPI development skill covering REST API creation, routing, request/response handling, validation, authentication, database integration, middleware, and deployment. Use when working with FastAPI projects, building APIs, implementing CRUD operations, setting up authentication/authorization, integrating databases (SQL/NoSQL), adding middleware, handling WebSockets, or deploying FastAPI applications. Triggered by requests involving .py files with FastAPI code, API endpoint creation, Pydantic models, or FastAPI-specific features.

232 15
Explore
aiskillstore/marketplace

context7-efficient

Token-efficient library documentation fetcher using Context7 MCP with 86.8% token savings through intelligent shell pipeline filtering. Fetches code examples, API references, and best practices for JavaScript, Python, Go, Rust, and other libraries. Use when users ask about library documentation, need code examples, want API usage patterns, are learning a new framework, need syntax reference, or troubleshooting with library-specific information. Triggers include questions like "Show me React hooks", "How do I use Prisma", "What's the Next.js routing syntax", or any request for library/framework documentation.

232 15
Explore
aiskillstore/marketplace

browser-use

Browser automation using Playwright MCP. Navigate websites, fill forms, click elements, take screenshots, and extract data. Use when tasks require web browsing, form submission, web scraping, UI testing, or any browser interaction.

232 15
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results