名称: redis 描述: Redis缓存模式、发布/订阅、会话管理、速率限制和数据结构。 允许工具: 读取、写入、编辑、Bash、Glob、Grep
Redis 技能
提供Redis缓存和数据管理的专家级协助。
能力
- 实施缓存策略
- 构建发布/订阅系统
- 管理会话
- 实施速率限制
- 使用Redis数据结构
缓存模式
async function getCachedUser(id: string) {
const cached = await redis.get(`user:${id}`);
if (cached) return JSON.parse(cached);
const user = await db.user.findUnique({ where: { id } });
await redis.setex(`user:${id}`, 3600, JSON.stringify(user));
return user;
}
速率限制
async function rateLimit(ip: string, limit = 100, window = 60) {
const key = `ratelimit:${ip}`;
const count = await redis.incr(key);
if (count === 1) await redis.expire(key, window);
return count <= limit;
}
目标流程
- 缓存实施
- 实时功能
- 会话管理