简体   繁体   中英

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. 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.

2) when one book is clicked, I only add 1 on the "liked" column in the book table. 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.

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. It will be efficient than the 2) way cause there is no aggregation needed, especially when many people try to render the series page. 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)? Thanks in advance.

Use 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.

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.

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

Using a RDBMS, you should focus on data integrity . 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. When someone likes a book, you take the id of the book they liked and add it to the likes table.

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)

Likes Table:

id, username, book_id, date

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 = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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