跳转到内容
搜索文档

Upstash

最后更新 查看 MarkdownAgent 设置

Upstash 是一款具有 Redis* 和 Kafka API 的无服务器数据库。Upstash 还提供 QStash,一款专为无服务器设计的任务队列/调度器。

Upstash for Redis

要设置 Upstash 集成:

  1. 你需要有一个现有的 Upstash 数据库可供连接。创建 Upstash 数据库从现有数据库加载数据到 Upstash

  2. 向 Upstash 数据库插入一些数据。你可以通过两种方式向 Upstash 数据库添加数据:

    • 直接从 Upstash 控制台使用 CLI。
    • 或者,在本地安装 redis-cli 并运行以下命令。
    set GB "Ey up?"
    OK
    set US "Yo, what's up?"
    OK
    set NL "Hoi, hoe gaat het?"
    OK
  3. 在 Worker 中配置 Upstash Redis 凭据:

    你需要将 Upstash Redis 数据库 URL 和令牌作为 secret 添加到 Worker。从 Upstash 控制台 的数据库详情中获取这些值,然后使用 Wrangler 添加为 secret:

    # Add the Upstash Redis URL as a secret
    npx wrangler secret put UPSTASH_REDIS_REST_URL
    # When prompted, paste your Upstash Redis REST URL
    
    # Add the Upstash Redis token as a secret
    npx wrangler secret put UPSTASH_REDIS_REST_TOKEN
    # When prompted, paste your Upstash Redis REST token
  4. 在 Worker 中安装 @upstash/redis,这是一个用于连接数据库并开始操作数据的 HTTP 客户端:

    npm i @upstash/redis
  5. 以下示例展示如何在 Worker 中查询 Upstash 数据库。连接 Upstash 所需的凭据已作为 secret 添加到 Worker。

    import { Redis } from "@upstash/redis/cloudflare";
    
    export default {
    	async fetch(request, env) {
    		const redis = Redis.fromEnv(env);
    
    		const country = request.headers.get("cf-ipcountry");
    		if (country) {
    			const greeting = await redis.get(country);
    			if (greeting) {
    				return new Response(greeting);
    			}
    		}
    
    		return new Response("Hello What's up!");
    	},
    };

要了解更多关于 Upstash 的信息,请参阅 Upstash 文档

Upstash QStash

要设置 Upstash QStash 集成:

  1. 配置你要向其发送消息的公开可用 HTTP 端点

  2. 在 Worker 中配置 Upstash QStash 凭据:

    你需要将 Upstash QStash 令牌作为 secret 添加到 Worker。从 Upstash 控制台 的 QStash 设置中获取令牌,然后使用 Wrangler 添加为 secret:

    # Add the QStash token as a secret
    npx wrangler secret put QSTASH_TOKEN
    # When prompted, paste your QStash token
  3. 在 Worker 中安装 @upstash/qstash,这是一个用于连接 QStash 端点的 HTTP 客户端:

    npm i @upstash/qstash
  4. 请参阅 Upstash 文档:如何在 Cloudflare Worker 中接收来自 QStash 的 webhook

* Redis 是 Redis Ltd. 的商标。其中所有权利均归 Redis Ltd. 所有。Upstash 的任何使用仅出于引用目的,并不表示 Redis 与 Upstash 之间存在任何赞助、认可或关联。

这篇文档对您有帮助吗?