Like as a high frequency operation, if every operation read and write database will increase the pressure of the database, so the cache + scheduled task to achieve. The “like” data is cached in Redis for half an hour, and the scheduled task is executed every 5 minutes for persistent storage. The cache time and task execution time can be determined according to the project situation.

advantages

1. Reduce the impact on the database. 2

disadvantages

1. If the task hangs, the like data will be lost. 2

Sequence diagram

Database design

create table user_like(
id bigint(20) unsigned not null auto_increment comment 'id',
user_id bigint(20) not null default 0 comment 'user id',
liked_id varchar(21) not null default ' ' comment 'Liked ID',
liked_status int(11) not null default 0 comment 'Liked status, 0 unliked, 1 liked',
liked_type int(11) not null default 0 comment 'Type of likes',
liked_time timestamp not null default '0000-00-00 00:00:00. 000000' comment 'Like time',
 is_delete tinyint not null default '0' comment 'Logical deletion or not',
 create_time timestamp not null default CURRENT_TIMESTAMP comment 'Creation time',
 update_time timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment 'Update Time',
 primary key (id),
 unique uniq_user_id_liked_id_type(user_id,liked_id,liked_type),
 key idx_liked_id (liked_id),
 key idx_create_time (create_time),
 key idx_update_time (update_time)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='User likes table';

create table user_like_stat(
id bigint(20) unsigned not null auto_increment comment 'id',
liked_id varchar(21) not null default ' ' comment 'Liked ID',
liked_count int(11) not null default 0 comment 'Total likes',
 is_delete tinyint not null default '0' comment 'Logical deletion or not',
 create_time timestamp not null default CURRENT_TIMESTAMP comment 'Creation time',
 update_time timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment 'Update Time',
 primary key (id),
 unique uniq_info_num(liked_id),
 key idx_create_time (create_time),
 key idx_update_time (update_time)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='Likes TAB';
Copy the code

Implementation steps

1. Design the cache data format

The whole like module mainly uses caching to complete, so to choose the appropriate data structure, I choose the hash data structure to implement, because it can add, obtain, remove a single key value pair, and can obtain all key value pairs. The main cache of two kinds of data, one is the user’s “like” status, one is the “like” id number. Both types of data are stored with two keys, each containing multiple key-value pairs. The key-value pair format is as follows:

State of the user’s thumb up key – value — — — — — – > {” id: thumb up: user id “:” thumb up state: : thumb up time: : “thumb up type}

Key-value ——>{” liked ID “:” Liked number “}

2. Split large keys

In the case of a large amount of “like” data, the above design will cause a large value stored by a single key. Since Redis is run in a single thread, if the value of a single operation is large, it will affect the response time of the whole Redis. Therefore, we will split the above two keys. Fixed the number of keys, each time the access is first locally calculated which key is dropped, this operation is similar to redis partitioning, sharding. It helps to reduce the pressure of a single operation by splitting the pressure across multiple keys.

// Split newHashKey =hashKey +"_"+ (userId% 5); hset (newHashKey, field, value) ; Hget (newHashKey, field) // newHashKey =hashKey +"_"+ Math.abs((hash*(liked id)) % 5); hset (newHashKey, field, value) ; hget(newHashKey, field)Copy the code

3. Code implementation

The following values are a snippet of code to provide an idea.

1. Enumeration of thumbs-up status

Juejin. Cn/post / 684490…

4. Improve the point

The current read is using a key, next can be optimized to make the key read and write separate. Use different keys for writes and reads to reduce the waste of resources. Otherwise, data that has been persisted and has not been cached will be queried each time the scheduled task is run.

The above is a realization of the idea of praise, we have any better method or improvement point, welcome to put forward.