简体   繁体   English

基于MongoDb ObjectId的基于Redis光标的分页

[英]Redis cursor-based pagination based on MongoDb ObjectId

I don't have experience with Redis so far, but I'm exploring possibilities to use MongoDB as database and Redis as cache. 到目前为止,我还没有使用Redis的经验,但是我正在探索将MongoDB用作数据库而将Redis用作缓存的可能性。

The question I'm dealing with is whether Redis is capable of handling MongoDb ObjectId's in the scope of cursor-based pagination as described, for example here: https://developer.twitter.com/en/docs/tweets/timelines/guides/working-with-timelines.html . 我要处理的问题是,Redis是否能够在如上所述的基于光标的分页范围内处理MongoDb ObjectId,例如: https : //developer.twitter.com/en/docs/tweets/timelines/guides /working-with-timelines.html
In this example we have a maxId that serves as the maximum id that was fetched from the previous request, and will be used as lower bound for fetching the next page. 在此示例中,我们有一个maxId,它用作从上一个请求中获取的最大id,并将用作获取下一页的下限。

In MongoDb I've explored that it is not a problem to user greater than / smaller than operators on ObjectId's, but I don't know if I will be capable to handle this in Redis, as ObjectId's will most probably be stored as a string value. 在MongoDb中,我探索了大于/小于ObjectId上的运算符的用户这不是问题,但是我不知道我是否能够在Redis中处理此问题,因为ObjectId很有可能将存储为字符串值。

This question is important for me as it will help me to decide whether to use MongoDb ObjectId's, or to use auto-increments as PK id. 这个问题对我来说很重要,因为它将帮助我决定是使用MongoDb ObjectId还是使用自动增量作为PK ID。 I would prefer to use ObjectId's though. 我宁愿使用ObjectId。

Note: I'm writing my backend with Java, so fancy npm modules are not what I'm looking for. 注意:我正在用Java编写后端,所以花哨的npm模块不是我想要的。

The solution I came up with: 我想出的解决方案:

  • Use timestamp as cursor 使用时间戳记作为光标
  • Store timestamp as score in Redis. 将时间戳记存储为Redis中的分数。 Even when there are theoretically duplicate scores possible in this case, the chance that this will cause conflict in terms of pagination is negligeable for my application. 即使在这种情况下理论上可能存在重复的分数,但对于我的应用程序来说,这可能导致分页冲突的可能性可以忽略不计。
    For example: I have a duplicate score at the 10th result. 例如:我在第10个成绩上有重复的分数。 The next request will include the current timestamp in its result, which means that both 10th and 11th result would be returned on the next request. 下一个请求将在其结果中包括当前时间戳,这意味着在下一个请求中将同时返回第10和第11个结果。
  • In case Redis returns results, ok 如果Redis返回结果,可以
  • In case Redis does not return results, the timestamp cursor can be used to query ObjectId's in MongoDb as well. 如果Redis不返回结果,则时间戳游标也可以用于查询MongoDb中的ObjectId。 Even though ObjectId doesn't support milliseconds, this is not a real problem. 即使ObjectId不支持毫秒,这也不是真正的问题。 Find all objectId's <= cursor timestamp with limit / offset should work fine. 查找具有限制/偏移量的所有objectId的<=光标时间戳应该可以正常工作。 The cursor timestamp in the search needs , so millisecond variations won't cause trouble. 搜索中的光标时间戳需要,因此毫秒级的变化不会造成麻烦。
  • In case Redis only returns a partial result, MongoDb can be queried based on the ObjectId of the last available post that was found in Redis. 如果Redis仅返回部分结果,则可以根据Redis中找到的最后一个可用帖子的ObjectId来查询MongoDb。

This solution isn't ideal since the client will need to perform extra checks on the last processed vs the newly received results to avoid duplicate rendering, but this isn't a real problem as this is not an open api and only used internally. 该解决方案并不理想,因为客户端将需要对上次处理的结果与新收到的结果进行额外的检查,以避免重复渲染,但这并不是一个真正的问题,因为这不是开放的api,仅在内部使用。 After looking for quite some time, there doesn't appear to be an one-fit-all solution to this kind of problem. 在寻找了相当长的时间之后,似乎没有一种万能的解决方案来解决这种问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cursor 中基于 MongoDB 的分页,对 2 个属性进行排序 - Cursor based pagination in MongoDB with sorting on 2 properties 使用 ObjectId 对 mongoDB 中的嵌套字段进行光标分页 - Cursor pagination for nested fields in mongoDB using ObjectId MongoDB基于ObjectID(_id)更新多个文档 - MongoDB Update Multiple Documents based on ObjectID (_id) MongoDB:如何基于ObjectID更新n条记录 - MongoDB: how to update n records based on ObjectID 当_id被覆盖时,如何使用官方mongodb客户端在node.js中为mongodb实现基于光标的分页? - How to implement Cursor based pagination for mongodb in node.js using official mongodb client when _id is overrided? 基于ObjectId参考条件的MongoDB查询 - MongoDB Query Based on ObjectId Reference Condition How to implement twitter and facebook api like cursor based pagination in mongodb in nodejs using official mongodb client? - How to implement twitter and facebook api like cursor based pagination in mongodb in nodejs using official mongodb client? MongoDB 没有跳过和 ObjectId 的分页 - MongoDB pagination without skip and ObjectId mongoDB:如何基于ObjectId查询,将数据分片到多个通道? - mongoDB: how to query based on ObjectId, to shard data into multiple channels? MongoDB - 基于非唯一字段的分页 - MongoDB - Pagination based on non-unique fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM