简体   繁体   English

如何处理一些事件发生多少次的计数?

[英]How to handle count of how many times some events occurs?

Try to get the best practice of handling count of how many times books being "i like"d. 尝试获得处理“我喜欢”的书数的最佳实践。 Let's say right now, I have many series of books and each series can have many books. 现在说,我有很多书,每个系列可以有很多书。 Now, when people click "i like" to one book, I want to add 1 to the book and also add 1 to the series so that later I can render these numbers, correspondingly. 现在,当人们在一本书上单击“我喜欢”时,我想在书上加1,然后在系列中加1,以便以后我可以相应地渲染这些数字。 Now, I am struggle on two ways. 现在,我在两种方法上进行挣扎。

1) when one book is clicked, I will add 1 on the "liked" column in the book table and at the same time, add 1 on the "liked" column in the series table. 1)当单击一本书时,我将在书籍表的“喜欢”列中添加1,同时在系列表的“喜欢”列中添加1。

2) when one book is clicked, I only add 1 on the "liked" column in the book table. 2)单击一本书时,我只在书表的“喜欢”列上添加1。 When I try to do a render of the number for the series, I do a SUM on the "liked" column of the books that belong to the series. 当我尝试对系列编号进行渲染时,我对属于该系列的书籍的“喜欢”列进行了SUM运算。

However, both of have pro and cons. 但是,两者都有优点和缺点。 The 1) way, I can simply fetch the "liked" column of the series table when I try to show how many people liked the whole series. 1)方法,当我尝试显示有多少人喜欢整个系列时,我可以简单地获取系列表的“喜欢”列。 It will be efficient than the 2) way cause there is no aggregation needed, especially when many people try to render the series page. 这将比2)方式高效,因为不需要聚合,尤其是当许多人尝试呈现系列页面时。 However, this will take more effort when we click "i like" button. 但是,当我们单击“我喜欢”按钮时,这将需要更多的精力。 Cause even when people click on different books. 即使当人们单击不同的书时,也会引起该问题。 As long as these books belong to the same series, it will need to update the number of the series. 只要这些书属于同一系列,就需要更新该系列的编号。 And it will be concurrent process. 而且它将是并发过程。 On the contrary, if I don't do update on the series table when people click "i like" for the book. 相反,如果人们单击“我喜欢”这本书时,我不对系列表进行更新。 It will be more efficient at that level, but will waste a lot of effort to do the redundant aggregation calculation when many people try to load the same series page, where the "liked" number for the series is shown. 在那个级别上它会更高效,但是当许多人尝试加载同一系列页面时,将花费大量的精力进行冗余聚合计算,其中显示了该系列的“喜欢”数字。

Any other ideas? 还有其他想法吗? If no, what is a better solution? 如果没有,有什么更好的解决方案? 1) or 2)? 1)或2)? Thanks in advance. 提前致谢。

Use HttpSession 使用HttpSession

HttpSession session = request.getSession(false);
if (session == null) {
    // store value in session
}
else {
    // read value from session
}

Another option is to use Redux for larger Single Page Applications but only if makes sense. 另一个选择是将Redux用于较大的“单页应用程序”,但前提是有意义。

I prefer the second approach. 我更喜欢第二种方法。 The logic there makes more sense. 那里的逻辑更有意义。 Instead of just adding 1 to the series each time the book is clicked, you can just sum up the total number of likes on the series. 不仅可以在每次单击该书时在系列中加1,还可以对系列中的喜欢总数进行汇总。

将数据存储在数据库中,否则在服务器重新启动时会丢失数据。

Using a RDBMS, you should focus on data integrity . 使用RDBMS时,您应该专注于数据完整性 The first approach can introduce an update anomaly, if not done in a single transaction. 如果没有在单个事务中完成,第一种方法可能会引入更新异常。 Anyway, you would have high concurrency on the series counter. 无论如何,您在系列计数器上的并发性很高。 So I would prefer the second approach. 所以我更喜欢第二种方法。

Start with a denormalization only if your solution lacks of performance. 仅当解决方案缺乏性能时,才从非规范化开始。

I recommend using a database. 我建议使用数据库。 You can have a books table and a likes table. 您可以有一个books表和一个likes表。 When someone likes a book, you take the id of the book they liked and add it to the likes table. 当某人喜欢一本书时,您可以获取他们喜欢的书的ID,并将其添加到Likes表中。

This way you can also store the name of the person who liked it, and stop them from liking more than twice, and you can also give them the option to remove their like. 这样,您还可以存储喜欢的人的名字,并阻止他们喜欢两次以上,并且还可以给他们选择删除喜欢的人的选项。 For example: 例如:

Books Table: 图书表:

id, bookname, author, (and any other information) ID,书名,作者(以及其他任何信息)

Likes Table: 点赞表:

id, username, book_id, date ID,用户名,book_id,日期

Then when counting how many likes there are for a single book, you can query the database and count the number of rows for where the book_id = ? 然后,当计算一本书的喜欢数量时,您可以查询数据库并计算book_id =?的行数。

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

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