Home  >  Article  >  Backend Development  >  How to handle product clicks

How to handle product clicks

WBOY
WBOYOriginal
2016-12-01 00:25:521371browse

No matter how many times the same user clicks on a certain product, it will only be counted as 1, and the number of clicks on the product will be counted in real time.
How to achieve the optimal solution in this situation
What I am thinking of now is just using redis’s setdata Type, use redis to save it every time you click, because set cannot be repeated, so directly SCARD, the total will be calculated, and then persisted to the database after a period of time. But if the amount of data is large in the future , it will also take up a lot of memory. Does anyone have any other solutions?
Just wait and see!!!

Reply content:

No matter how many times the same user clicks on a certain product, it will only be counted as 1, and the number of clicks on the product will be counted in real time.
How to achieve the optimal solution in this situation
What I am thinking of now is just using redis’s setdata Type, use redis to save it every time you click, because set cannot be repeated, so directly SCARD, the total will be calculated, and then persisted to the database after a period of time. But if the amount of data is large in the future , it will also take up a lot of memory. Does anyone have any other solutions?
Just wait and see!!!

If you don’t need to be very precise and don’t need to know which users have browsed the products, you can consider HyperLogLog

pfadd shop:{$good_id} user:{$user_id}

In addition, the phpredis extension must be updated to 2.2.7 before it can be used

<code class="php">$redis->pfadd("key", array(
                        $element1, 
                        $element2,
                        ...,
                        $elementN)
                        );</code>

Related reference materials: Redis cardinality statistics - HyperLogLog’s small memory and great use

How big can it be? Isn’t there an in and out? Isn’t it really possible to add a machine?

There are incomings and outgoings. When you go out, you can swipe the data into the database and save the numbers. At the same time, you can save the click relationship of the user's products and check it the next time you go out.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn