gpt4 book ai didi

database-design - 使用 Redis + Node.js 的数据库架构

转载 作者:IT王子 更新时间:2023-10-29 05:59:25 24 4
gpt4 key购买 nike

在回答其他一些 SO 问题之后,我正在开发一个网站监控应用程序作为一个宠物项目,目的是学习更多关于 Node.js + Redis 的知识。

我的计划是让用户添加 url 并将它们添加到 Redis SET。每分钟,我都会获得 SET 结果,执行 HTTP Get 请求并打印响应。

这似乎工作正常,但是,我有几个问题:

  1. 鉴于 Redis SET 不允许重复键(这将使我免于对同一 URL 发出请求),当用户从他的帐户中删除该 URL 但另一个用户具有相同的 URL 时,我该如何控制? 我能否在 URL key 中包含一个 INCR 值,以便我知道有多少用户在他们的帐户中有该 URL?

  2. 考虑到我每分钟都会发出一个 HTTP 请求,并且我想使用 Redis 来保存结果(响应时间、上行/下行等),将所有这些数据保存在 Redis 中的最佳方式是什么(每分钟对每个网址的请求的结果)?我应该将每个响应保存在唯一的 Redis key 中吗?

  3. 为了将结果实时展示给用户,查询结果并实时解析的最佳方式是什么?

感谢您的帮助。

最佳答案

我认为你应该开始在 redis-cli 中编写原型(prototype)。我还想指出这篇来自 Simon Willison explaining redis 的非常好的文章.

Given that Redis SET does not allow repeated keys (which will save me from doing a request to the same URL), how do I control when a user removes the URL from his account but another user has the same URL? Can I have an INCR value in the URL key so I know how many users have the URL in their account?

我会使用 SADD + INCR为了那个原因。

SADD urls http://www.google.com
INCR http://www.google.com

要删除 http://www.google.com 我会简单地做:

DECR http://www.google.com
#Only if DECR http://www.google.com => 0, then you should remove from SET
SREM urls http://www.google.com

Given that I do an HTTP request every minute and I want to use Redis to save the results (response time, up/down, etc), what's the best way to save all that data in Redis (results from the requests to each url every minute)?

我会为每个 URL 使用一个唯一的键,并使用 MSET 将数据作为 json(JSON.stringify(obj)) 写回 Redis .

MSET data:http://www.google.com "{json for google}" data:http://www.yahoo.com "{json for yahoo}"

In order to show results to the user in real-time, what's the best way to query the results and parse it in real-time?

我会通过 MGET 得到结果并解析 json(obj = JSON.parse(json-string)).

关于database-design - 使用 Redis + Node.js 的数据库架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4746324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com