Optimize client calls to server interfaces to reduce request data capacity

jacklove
Release: 2023-03-31 06:28:01
Original
1408 people have browsed it

Due to project needs, it is necessary to record the user's reservation time in the next three days, and each reservation period is 1 hour.

For example:00:00:00~00:59:59or01:00:00~01:59:59etc. is a reservation period

The front-end code is as follows:

nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">   提交预约日期     

请选择预约时间:

Copy after login

2015-05-28:

00:0001:0002:0003:0004:0005:0006:0007:0008:0009:0010:0011:00

12:0013:0014:0015:0016:0017:0018:0019:0020:0021:0022:0023:00

2015-05-29:

00:0001:0002:0003:0004:0005:0006:0007:0008:0009:0010:0011:00

12:0013:0014:0015:0016:0017:0018:0019:0020:0021:0022:0023:00

2015-05-30:

00:0001:0002:0003:0004:0005:0006:0007:0008:0009:0010:0011:00

12:0013:0014:0015:0016:0017:0018:0019:0020:0021:0022:0023:00

The back-end code is as follows:

true));?>
Copy after login


For example, to submit a user's 3 Day reservation data, reservations for all time periods (select all)
The request data received by the backend is as follows:

["2015-05-28 00:00:00","2015-05-28 01:00:00","2015-05-28 02:00:00","2015-05-28 03:00:00","2015-05-28 04:00:00","2015-05-28 05:00:00","2015-05-28 06:00:00","2015-05-28 07:00:00","2015-05-28 08:00:00","2015-05-28 09:00:00","2015-05-28 10:00:00","2015-05-28 11:00:00","2015-05-28 12:00:00","2015-05-28 13:00:00","2015-05-28 14:00:00","2015-05-28 15:00:00","2015-05-28 16:00:00","2015-05-28 17:00:00","2015-05-28 18:00:00","2015-05-28 19:00:00","2015-05-28 20:00:00","2015-05-28 21:00:00","2015-05-28 22:00:00","2015-05-28 23:00:00","2015-05-29 00:00:00","2015-05-29 01:00:00","2015-05-29 02:00:00","2015-05-29 03:00:00","2015-05-29 04:00:00","2015-05-29 05:00:00","2015-05-29 06:00:00","2015-05-29 07:00:00","2015-05-29 08:00:00","2015-05-29 09:00:00","2015-05-29 10:00:00","2015-05-29 11:00:00","2015-05-29 12:00:00","2015-05-29 13:00:00","2015-05-29 14:00:00","2015-05-29 15:00:00","2015-05-29 16:00:00","2015-05-29 17:00:00","2015-05-29 18:00:00","2015-05-29 19:00:00","2015-05-29 20:00:00","2015-05-29 21:00:00","2015-05-29 22:00:00","2015-05-29 23:00:00","2015-05-30 00:00:00","2015-05-30 01:00:00","2015-05-30 02:00:00","2015-05-30 03:00:00","2015-05-30 04:00:00","2015-05-30 05:00:00","2015-05-30 06:00:00","2015-05-30 07:00:00","2015-05-30 08:00:00","2015-05-30 09:00:00","2015-05-30 10:00:00","2015-05-30 11:00:00","2015-05-30 12:00:00","2015-05-30 13:00:00","2015-05-30 14:00:00","2015-05-30 15:00:00","2015-05-30 16:00:00","2015-05-30 17:00:00","2015-05-30 18:00:00","2015-05-30 19:00:00","2015-05-30 20:00:00","2015-05-30 21:00:00","2015-05-30 22:00:00","2015-05-30 23:00:00"]
Copy after login

Optimize client calls to server interfaces to reduce request data capacity

Request data content- length:2879

Using this method to request a larger data capacity will affect the response time.

Improvement method 1: Convert the DateTime format time to a timestamp and then submit

The front-end code is modified as follows:Modify the javascript fsubmit method

Copy after login

The request data received by the backend is as follows:

["1432742400","1432746000","1432749600","1432753200","1432756800","1432760400","1432764000","1432767600","1432771200","1432774800","1432778400","1432782000","1432785600","1432789200","1432792800","1432796400","1432800000","1432803600","1432807200","1432810800","1432814400","1432818000","1432821600","1432825200","1432828800","1432832400","1432836000","1432839600","1432843200","1432846800","1432850400","1432854000","1432857600","1432861200","1432864800","1432868400","1432872000","1432879200","1432882800","1432886400","1432890000","1432893600","1432897200","1432900800","1432904400","1432908000","1432911600","1432915200","1432918800","1432922400","1432926000","1432929600","1432933200","1432936800","1432940400","1432944000","1432947600","1432951200","1432954800","1432958400","1432962000","1432965600","1432969200","1432972800","1432976400","1432980000","1432983600","1432987200","1432990800","1432994400","1432998000"]
Copy after login

Optimize client calls to server interfaces to reduce request data capacity

##Request data content-length:

1916

After receiving it, the backend can convert the timestamp into datetime format for use

Improvement method 2: Use a 32-bit integer variable to convert each hour As one of the 32-bit integers, the corresponding relationship between the

period and the 32-bit integer is


starting from right to left: 00:00, 01:00, 02:00 ,03:00 and so on, if there is a selected time period, it will be 1, otherwise it will be 0

For example: you need to make a reservation for 10:00, 12:00, 14:00 on 2016-05-28 ,16:00,18:00

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 忽略 忽略 忽略 忽略 忽略 忽略 忽略 忽略 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
Copy after login

is expressed in 32-bit binary as

1010101010000000000, that is, decimal349184 = 2 to the 10th power 2 to the 12th power 2 14 times 2 to the 16th power 2 to the 18th power
The front-end code is modified as follows:Modify the javascript fsubmit method

Copy after login

The request received by the backend The data is as follows:

["2015-05-28 16777215","2015-05-29 16777215","2015-05-30 16777215"]
Copy after login

Optimize client calls to server interfaces to reduce request data capacity

Request data content-length:

107

Use this Method, no matter how many reservation time periods are selected in a day, there will only be one record, which greatly reduces the size of submitted data.
Backend method of converting decimal to time:1. Convert decimal to binary and reverse
2. Loop through each bit and record the value of 1
3. Save the record

The code is as follows:

Copy after login

Output:

Array( [0] => 2015-05-28 08:00:00 [1] => 2015-05-28 10:00:00 [2] => 2015-05-28 12:00:00 [3] => 2015-05-28 14:00:00 [4] => 2015-05-28 16:00:00 [5] => 2015-05-28 18:00:00 [6] => 2015-05-28 20:00:00)
Copy after login

Summary:This article provides a method that can convert large amounts of data into small-capacity data transmission, but it requires adding more operations. Therefore, in actual development, it is necessary to judge whether the algorithm of exchanging time for space or space for time is suitable based on actual needs.

This article explains how to optimize the client to call the server interface to reduce the request data capacity. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

mysql order by rand() efficiency optimization method

php Get the start date and end date All dates between

php method to get a random combination from the specified number

The above is the detailed content of Optimize client calls to server interfaces to reduce request data capacity. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!