redis-js by upstash/redis-js
npx skills add https://github.com/upstash/redis-js --skill redis-js本目录包含使用 @upstash/redis SDK 的全面指南。这些技能文件旨在帮助开发者和 AI 助手有效理解和使用该 SDK。
npm install @upstash/redis
import { Redis } from "@upstash/redis";
// 使用显式凭据初始化
const redis = new Redis({
url: "UPSTASH_REDIS_REST_URL",
token: "UPSTASH_REDIS_REST_TOKEN",
});
// 或从环境变量初始化
const redis = Redis.fromEnv();
在您的 .env 文件中设置:
UPSTASH_REDIS_REST_URL=https://your-redis.upstash.io
UPSTASH_REDIS_REST_TOKEN=your-token-here
包含自动序列化示例的 Redis 数据类型:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
复杂操作和优化:
常见用例和架构模式:
优化技术和最佳实践:
Redis 的全文搜索、过滤和聚合扩展:
从其他库迁移的指南:
// ❌ 错误 - 不要对 @upstash/redis 这样做
await redis.set("count", "42"); // 存储为字符串 "42"
const count = await redis.get("count");
const incremented = parseInt(count) + 1; // 需要手动解析
// ✅ 正确 - 让 SDK 处理
await redis.set("count", 42); // 存储为数字
const count = await redis.get("count");
const incremented = count + 1; // 直接使用
// ❌ 错误 - 对于 @upstash/redis 来说不必要
await redis.set("user", JSON.stringify({ name: "Alice" }));
const user = JSON.parse(await redis.get("user"));
// ✅ 正确 - 自动处理
await redis.set("user", { name: "Alice" });
const user = await redis.get("user");
// 字符串
await redis.set("key", "value");
await redis.get("key");
await redis.incr("counter");
await redis.decr("counter");
// 哈希
await redis.hset("user:1", { name: "Alice", age: 30 });
await redis.hget("user:1", "name");
await redis.hgetall("user:1");
// 列表
await redis.lpush("tasks", "task1", "task2");
await redis.rpush("tasks", "task3");
await redis.lrange("tasks", 0, -1);
// 集合
await redis.sadd("tags", "javascript", "redis");
await redis.smembers("tags");
// 有序集合
await redis.zadd("leaderboard", { score: 100, member: "player1" });
await redis.zrange("leaderboard", 0, -1);
// JSON
await redis.json.set("user:1", "$", { name: "Alice", address: { city: "NYC" } });
await redis.json.get("user:1");
// 过期
await redis.setex("session", 3600, { userId: "123" });
await redis.expire("key", 60);
await redis.ttl("key");
user:123、session:abc)有关特定主题的详细信息,请参阅 skills/ 目录中的各个技能文件。每个文件都包含其主题的全面示例、用例和最佳实践。
每周安装量
206
仓库
GitHub 星标数
931
首次出现
2026年1月23日
安全审计
安装于
opencode155
cursor153
codex153
gemini-cli148
github-copilot146
claude-code136
This directory contains comprehensive guides for using the @upstash/redis SDK. These skill files are designed to help developers and AI assistants understand and use the SDK effectively.
npm install @upstash/redis
import { Redis } from "@upstash/redis";
// Initialize with explicit credentials
const redis = new Redis({
url: "UPSTASH_REDIS_REST_URL",
token: "UPSTASH_REDIS_REST_TOKEN",
});
// Or initialize from environment variables
const redis = Redis.fromEnv();
Set these in your .env file:
UPSTASH_REDIS_REST_URL=https://your-redis.upstash.io
UPSTASH_REDIS_REST_TOKEN=your-token-here
Redis data types with auto-serialization examples:
Complex operations and optimizations:
Common use cases and architectural patterns:
Optimization techniques and best practices:
Full-text search, filtering, and aggregation extension for Redis:
Migration guides from other libraries:
// ❌ WRONG - Don't do this with @upstash/redis
await redis.set("count", "42"); // Stored as string "42"
const count = await redis.get("count");
const incremented = parseInt(count) + 1; // Manual parsing needed
// ✅ CORRECT - Let the SDK handle it
await redis.set("count", 42); // Stored as number
const count = await redis.get("count");
const incremented = count + 1; // Just use it
// ❌ WRONG - Unnecessary with @upstash/redis
await redis.set("user", JSON.stringify({ name: "Alice" }));
const user = JSON.parse(await redis.get("user"));
// ✅ CORRECT - Automatic handling
await redis.set("user", { name: "Alice" });
const user = await redis.get("user");
// Strings
await redis.set("key", "value");
await redis.get("key");
await redis.incr("counter");
await redis.decr("counter");
// Hashes
await redis.hset("user:1", { name: "Alice", age: 30 });
await redis.hget("user:1", "name");
await redis.hgetall("user:1");
// Lists
await redis.lpush("tasks", "task1", "task2");
await redis.rpush("tasks", "task3");
await redis.lrange("tasks", 0, -1);
// Sets
await redis.sadd("tags", "javascript", "redis");
await redis.smembers("tags");
// Sorted Sets
await redis.zadd("leaderboard", { score: 100, member: "player1" });
await redis.zrange("leaderboard", 0, -1);
// JSON
await redis.json.set("user:1", "$", { name: "Alice", address: { city: "NYC" } });
await redis.json.get("user:1");
// Expiration
await redis.setex("session", 3600, { userId: "123" });
await redis.expire("key", 60);
await redis.ttl("key");
user:123, session:abc)For detailed information on specific topics, refer to the individual skill files in the skills/ directory. Each file contains comprehensive examples, use cases, and best practices for its topic.
Weekly Installs
206
Repository
GitHub Stars
931
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode155
cursor153
codex153
gemini-cli148
github-copilot146
claude-code136
GSAP React 动画库使用指南:useGSAP Hook 与最佳实践
2,400 周安装