upstash-redis
它能做什么
让应用能够使用 Upstash Redis 快速保存和读取数据,即使在不支持直接连接的环境中也可以。它就像一个用于存放重要信息的高速“保险箱”。
适用场景
- ✓当你需要存放临时信息(缓存)时。
- ✓用于管理网站上的用户会话。
- ✓用于统计事件次数,比如访问量或点击量。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/upstash-redis/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“保存用户名为“Maria”、ID 为“user:123”的数据,并让它在 1 小时后过期。”
每周 5 个新技能,1 分钟读完。
工作原理
应用通过互联网请求把数据发送到 Upstash Redis。Upstash Redis 会存储这些数据,并在应用请求时将其返回。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Upstash Redis
Overview
@upstash/redis is a Redis client that talks to an Upstash Redis database over
HTTPS instead of a TCP connection. Because every command is a stateless HTTP
request, it works in environments where a pooled TCP client is awkward or
impossible: Vercel and Netlify functions, Cloudflare Workers, Deno, Bun, and
Next.js middleware. The client serializes and deserializes JavaScript values
automatically, so numbers and objects round-trip without manual JSON.parse.
When to Use This Skill
- Use when the user needs a cache, session store, counter, leaderboard, or simple queue from a serverless or edge function.
- Use when the user mentions Upstash Redis,
UPSTASH_REDIS_REST_URL, or@upstash/redis. - Use when migrating an
ioredisornode-rediscall site to a runtime that cannot hold a persistent TCP socket. - Do not use for a self-hosted or non-Upstash Redis server; the client only
speaks the Upstash REST protocol. Use
ioredisornode-redisthere. - Do not use for Redis administration or CLI work; see
redis-cli.
How It Works
Step 1: Configure credentials
Create a database in the Upstash console and copy the REST URL and token into environment variables. Never hardcode them.
UPSTASH_REDIS_REST_URL=https://<your-db>.upstash.io
UPSTASH_REDIS_REST_TOKEN=<your-rest-token>
Step 2: Create one client per module
import { Redis } from "@upstash/redis";
export const redis = Redis.fromEnv();
On Cloudflare Workers, import from @upstash/redis/cloudflare and pass the
worker env object: Redis.fromEnv(env).
Step 3: Call Redis commands as methods
Command names are lowercase methods (get, set, hset, zadd, incr).
Values are auto-serialized; pass and receive native JavaScript types.
Examples
Example 1: Cache-aside with a TTL
import { Redis } from "@upstash/redis";
const redis = Redis.fromEnv();
export async function getUser(id: string) {
const cached = await redis.get<{ id: string; name: string }>(`user:${id}`);
if (cached) return cached;
const user = await db.users.findById(id);
await redis.set(`user:${id}`, user, { ex: 3600 }); // expires in 1 hour
return user;
}
export async function invalidateUser(id: string) {
await redis.del(`user:${id}`);
}
Example 2: Batch commands in a pipeline
const pipeline = redis.pipeline();
pipeline.hset("user:1", { name: "Alice", plan: "pro" });
pipeline.incr("signups:total");
pipeline.zadd("leaderboard", { score: 120, member: "user:1" });
const [hsetResult, signups, zaddResult] = await pipeline.exec();
pipeline() batches independent commands into one round trip. It is not
atomic; use redis.multi() for MULTI/EXEC or a reviewed server-side Lua script
when commands must run as one unit.
Best Practices
- ✅ Read credentials with
Redis.fromEnv()or from a secrets manager. - ✅ Set a TTL (
{ ex: seconds }) on cache entries so stale data expires. - ✅ Namespace keys (
user:123,session:abc) to keep the keyspace readable. - ✅ Use
pipeline()ormget/msetinstead of many sequential awaits. - ❌ Don't
JSON.stringifyvalues beforeset; the client already does it. - ❌ Don't call
keys("*")in request handlers; usescanfor large keyspaces. - ❌ Don't store secrets or PII in Redis without a retention plan and TTL.
Limitations
- Requires an Upstash Redis database; it cannot connect to other Redis servers.
- Each command is an HTTP request, so latency is higher than a warm TCP connection; batch with pipelines where it matters.
- Transactions (
multi()) do not roll back on runtime errors, andWATCHis not available over REST; use a Lua script for atomic check-and-set. - Pub/Sub subscribe and blocking commands (
BLPOP,XREAD BLOCK) are not supported over the REST client. - This skill covers the TypeScript client only. Python, Go, and other SDKs differ in method names.
- This skill does not replace environment-specific validation, testing, or expert review.
Security & Safety Notes
- The REST token grants full read/write access to the database. Keep it in server-side environment variables; never ship it to a browser bundle.
- Use a read-only token from the console for read-only workloads.
- Commands such as
flushdbanddelare destructive; confirm with the user before running them against a production database.
Common Pitfalls
…
常见问题
为什么要用 Upstash Redis,而不用普通数据库?
它经过优化,速度非常快,并且很适合现代的编程环境(无服务器/边缘计算),这些环境通常不擅长处理长时间保持的连接。
什么是“键”(keys)和“值”(values)?
你可以把“键”想象成文件名,把“值”想象成文件的内容。你通过键来找到并读取对应的值。
我可以存储任何类型的信息吗?
可以,你可以存储文本、数字,甚至是对象等复杂的数据结构。系统会自动完成转换。
详情
作者的原始描述
Use the @upstash/redis HTTP client for caching, sessions, counters, and Redis data structures from serverless and edge runtimes without connection pooling.
热度
- GitHub 星标
- 48k
- 排名
- #6072
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。