redis achieve thumbs up function

    Under the company's Employee columns, articles have to deal with thumbs up. Search the Internet case, a common practice there are two, one is directly connected to mysql database for storage. A little

The disadvantage is that when popular articles frequently thumbs up, pressure to access the database becomes larger. Another feature is the use of service points to throw Like Redis (or memcache), and then brush back off mysql like.

  

Written directly to Mysql

Written directly Mysql is the simplest approach.

You can do three tables,

  • comment_info

    The main contents of the recording paper, mainly like_count, hate_count, score three fields are the main fields that we in this function.

  • comment_like

    Record the number of times the article is awesome, there are many people like this kind of data can be found directly from the table;

  • user_like_comment

    Like user records which had the article, when you open the list of articles to display There are no flattery of data in there;

Shortcoming

  • Database read and write pressure

    Popular articles point there will be many users praise, even a short period of time a lot of thumbs up, operate directly from the database in the long run is not a good thing for

redis storage batch is then brushed back to the database

redis main feature is the fast, after all, the main thing data in memory;

The main reason why I chose another instead of memcache redis redis that supports more data types, such as hash, set, zset and so on.

Specific will use these types below.

advantage

  • High performance

  • Relieve pressure reading and writing database

    In fact, I write more is to alleviate the pressure, as the pressure reading, can be resolved from the cache to make even hot data by adding redis through the main mysql,

    Write the pressure is really crippled for the previous program.

Shortcoming

  • The development of complex

    The ratio of direct write mysql program is much more complicated, you need to consider a lot of places;

  • We can not guarantee data security

    redis hang time will lose data, while data synchronization redis is not timely, it may be eliminated when redis memory replacement;

    But for us it thumbs up, little one o'clock a little lost data problem;

In fact, the second point above disadvantages can be avoided, which involves redis some design patterns, do not understand it does not matter, I try to write in detail, later I will show how to solve this shortcoming.

Before designing functional knowledge to prepare

  1. redis data type to use (in particular the type of instructions, see the bottom of the link, detailed description):

  • zset   of this type is mainly used for ordering increase and decrease or digital, and is used here as like hate digital recording, and the recording of heat.
  • set   this is an unordered set, primarily used to record today or need updating, will be today thumbs up (including points hate) over the article id recorded, or have time at night to facilitate this part of the data update.
  • hash   that is a hash, mainly used to store and index data. Here are used to record which users point what article for next judge (I've seen some online introduction to using set records that can be, but I think this is more space-saving, and easy to manage, then there is hash of fast speed).
  • list   This is the queue chiefs, our data can not be  safe  back mysql depends on it.

  2. About how to judge the heat:

  We all know that the higher the number of points like this, the higher the heat of the article article available, then how to judge it? Chan is not the number of points recorded directly on the line, but how to do Latest articles? For example, an article published a year ago, to get 50 praise, an article latest article for 49 praise, but in accordance with the above mentioned article a year ago than the latest high heat also, which is unreasonable, articles It is timeliness, who want to see the hottest new.

  so! We want another way to handle the timeliness, the vast majority of languages have  timestamps  generated methods, the time stamp with the new time, the larger the number, the time stamp initialization directly assign score to the article, this latest articles phase than the previous article will be the front. Then the impact point on the score of praise, we assume that one day get 20 praise regarded heat of the day, 60 * 60 * 24 = 86,400 seconds day and then get a praise is to get 86400/20 = 4320 points. Specific numbers to see their business needs may be, my son just an example of it. Of course, the point will hate minus the corresponding number.

Following the code re-write in detail.

 

Guess you like

Origin www.cnblogs.com/kisshappyboy/p/11693687.html
Recommended