简体   繁体   English

使用Redis的新闻提要

[英]News feed using redis

I have the following table structure where I am storing activity of a user like 'product creation' 我具有以下表格结构,用于存储用户的活动(例如“产品创建”)

id user_id type_id type verb data created_at updated_at id user_id type_id类型动词数据created_at Updated_at

Whenever an event happens on a product like ( an order has come for the product), I need to update the updated time, so that the record appears in the first as the user will be seeing the 'sorted using updated_at' row data. 每当类似产品(产品的订单)上发生事件时,我都需要更新更新时间,以便该记录显示在第一位,因为用户将看到“使用updated_at排序”行数据。 The feed which I am referring here will be consumed by a single user and there is no concept of following here. 我在这里引用的提要将由一个用户使用,并且这里没有遵循的概念。 So whoever created a feed will be seeing his own feeds. 因此,创建提要的任何人都会看到自己的提要。 If there is an update (order placed by someone on that product) coming on the feed, it should go up. 如果Feed上有更新(某人在该产品上下的订单),它应该会上升。

So when an order is placed, I will update the updated time of the entry so that it appears in the first. 因此,在下订单时,我将更新条目的更新时间,使其出现在第一行中。

I am planning to use redis for the reads, but I am pretty confused on the update part. 我打算使用redis进行读取,但是在更新部分我很困惑。 How will I handle this case. 我将如何处理此案。

What I have tried ? 我尝试了什么?

Created the table structure as following 创建表结构如下

  1. id ID
  2. user_id - User who created the event user_id-创建事件的用户
  3. event - created_product 事件-created_product
  4. event_id - product_id of products table event_id-产品表的product_id
  5. data - json object of the product details data-产品详细信息的json对象
  6. created_at created_at
  7. updated_at 的updated_at

When an order is received 收到订单后

The updated_at timestamp is updated. updated_at时间戳已更新。 So that the record comes on the top of the user feed. 这样,记录就位于用户供稿的顶部。

But this doesn't seem to be a proper solution as frequent updates can come for a row which can lead to row locking and more waits. 但这似乎不是一个合适的解决方案,因为频繁更新可能会导致一行锁定,从而导致行锁定和更多等待。 How do I solve this? 我该如何解决?

Well, you could create sorted set in redis: 好吧,您可以在redis中创建排序集:

FEED:USER_ID containing values: FEED:USER_ID包含值:

PRODUCT_ID - last update time as score PRODUCT_ID-上次更新时间为得分

And then get products from database by ids which you got from redis. 然后通过从Redis获得的ID从数据库中获取产品。

But if your main concern is row locking, then maybe you should just queue updates? 但是,如果您主要关心的是行锁定,那么也许您应该只将更新排队? Create redis sorted set "PRODUCT_UPDATED" and add row whenever you are updating product: 创建redis排序集“ PRODUCT_UPDATED”,并在每次更新产品时添加行:

USER_ID#PRODUCT_ID - update time as score USER_ID#PRODUCT_ID-更新时间为得分

Then create some background CRON job which will update products found in that sorted set. 然后创建一些后台CRON作业,该作业将更新在该排序集中找到的产品。 You can do it every second, or every 30 seconds, you can throttle number of updates etc... 您可以每秒或每30秒执行一次,您可以限制更新次数等。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM