Disclosure of Invention
In view of this, embodiments of the present invention provide a method and an apparatus for merging network requests, and an electronic device, which can improve a network request speed and improve user experience.
In a first aspect, an embodiment of the present invention provides a method for merging network requests, where the method is applied to a terminal device, and the method includes: judging whether a batch processing mode is started or not; if the batch processing mode is started, the network request is put into a first queue; judging whether preset batch processing conditions are met or not; and if the preset batch processing conditions are met, merging the network requests in the first queue.
According to a specific implementation manner of the embodiment of the present invention, the determining whether the preset batch processing condition is satisfied includes: judging whether the number of the network requests put into the first queue reaches a first threshold value or whether the duration of starting the batch processing mode reaches a second threshold value; and if the number of the network requests put into the first queue reaches a first threshold value or the starting time of the batch processing mode reaches a second threshold value, determining that the preset batch processing condition is met.
According to a specific implementation manner of the embodiment of the present invention, before determining whether the batch processing mode is started, the method further includes: judging whether the number of the current network requests exceeds a third threshold value; and if the current network request quantity exceeds a third threshold value, judging whether a batch processing mode is started or not.
According to a specific implementation manner of the embodiment of the present invention, the method further includes: if the batch processing mode is not started, the network request is put into a second queue; judging whether an idle sending channel exists or not; and if the idle sending channel exists, merging the network requests in the second queue.
According to a specific implementation manner of the embodiment of the present invention, the determining whether the batch processing mode is started includes: judging whether the network request carries batch processing mark information or not; and if the network request carries batch processing mark information, determining that a batch processing mode is started for the network request.
According to a specific implementation manner of the embodiment of the present invention, before determining whether the batch processing mode is started, the method further includes: batch flag information is injected for a predetermined network request.
In a second aspect, an embodiment of the present invention provides a device for merging network requests, where the device is applied to a terminal device, and the device includes: the batch processing judging module is used for judging whether a batch processing mode is started or not; the first operation module is used for placing the network request into a first queue if the batch processing mode is started; the first condition judgment module is used for judging whether a preset batch processing condition is met or not; and the first merging module is used for merging the network requests in the first queue if the preset batch processing condition is met.
According to a specific implementation manner of the embodiment of the present invention, the first condition determining module is configured to: judging whether the number of the network requests put into the first queue reaches a first threshold value or whether the duration of starting the batch processing mode reaches a second threshold value; and if the number of the network requests put into the first queue reaches a first threshold value or the starting time of the batch processing mode reaches a second threshold value, determining that the preset batch processing condition is met.
According to a specific implementation manner of the embodiment of the present invention, the apparatus further includes: a third threshold judgment module, configured to judge whether the number of current network requests exceeds a third threshold; and the batch processing judging module is used for judging whether a batch processing mode is started or not if the number of the current network requests exceeds a third threshold value.
According to a specific implementation manner of the embodiment of the present invention, the apparatus further includes: the second operation module is used for placing the network request into a second queue if the batch processing mode is not started; the idle channel judging module is used for judging whether an idle sending channel exists or not; and the second merging module is used for merging the network requests in the second queue if an idle sending channel exists.
According to a specific implementation manner of the embodiment of the present invention, the determining whether the batch processing mode is started by the batch processing determining module includes: judging whether the network request carries batch processing mark information or not; and if the network request carries batch processing mark information, determining that a batch processing mode is started for the network request.
According to a specific implementation manner of the embodiment of the present invention, the merging processing apparatus includes an injection module, and the injection module is configured to inject batch processing flag information into a predetermined network request.
In a third aspect, an embodiment of the present invention provides an electronic device, where the electronic device includes: the device comprises a shell, a processor, a memory, a circuit board and a power circuit, wherein the circuit board is arranged in a space enclosed by the shell, and the processor and the memory are arranged on the circuit board; a power supply circuit for supplying power to each circuit or device of the electronic apparatus; the memory is used for storing executable program codes; the processor executes the program corresponding to the executable program code by reading the executable program code stored in the memory, and is used for executing the method of any one of the foregoing implementation modes.
In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium storing one or more programs, which are executable by one or more processors to implement the method according to any one of the embodiments of the present invention.
The embodiment of the invention provides a method, a device and electronic equipment for merging network requests, which are applied to terminal equipment and used for judging whether a batch processing mode is started or not; if the batch processing mode is started, the network request is put into a first queue; judging whether preset batch processing conditions are met or not; if the preset batching conditions are met, merging the network requests in the first queue, and directly sending the merged network requests to a server without queuing for sending a channel; the network request speed can be improved, and the user experience is improved.
Detailed Description
Embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
It should be understood that the described embodiments are only some embodiments of the invention, and not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In a first aspect, the present embodiment provides a method for merging network requests, so as to improve a network request speed and improve user experience.
Fig. 1 is a flowchart illustrating a method for merging network requests according to a first embodiment of the present invention, and as shown in fig. 1, the present embodiment is applied to a terminal device. The method of the embodiment comprises the following steps:
step 101, judging whether a batch processing mode is started or not;
in this embodiment, most of the existing applications need to send more than 30 network requests to the server through a Web Service (the Web Service is also called XML Web Service, which is a lightweight independent communication technology capable of receiving requests transmitted from other systems on the Internet or Intranet), and the Web Service technology can enable different applications running on different machines to exchange data or integrate with each other without additional and special third-party software or hardware. The existing method for requesting the network service has the problems of low loading speed and poor user experience when an application program loads a page due to the limited number of request channels. The invention can carry out batch processing on the network requests sent by the application program, namely, a batch processing mode is started on the network requests, the batch processing in the invention means that the network requests are accumulated to a certain number and are submitted to the server once again, the interaction times with the server are reduced, and the network request speed is greatly improved. Alternatively, the batch mode may utilize a scheme of a transaction mechanism (beginBatch/commit Batch) to merge network requests in the middle of beginBatch to commit Batch. A transaction refers to a group of operations logically, and each unit forming the group of operations is not successful or not successful; the network request is processed in a mode of combining batch processing and a transaction mechanism, and the speed of the network request is further improved.
As an optional implementation manner of the embodiment of the present invention, before determining whether the batch processing mode is turned on, the method further includes: batch flag information is injected for a predetermined network request.
In this embodiment, according to the service requirements of each module in the application program, for example: the browser comprises modules such as latest movies, mini games and novel major and the like, and batch processing mark information can be injected into the mini game modules in advance for the mini game modules with more network requests, so that when the small game modules are loaded by the browser, the network requests sent by the mini game modules are subjected to batch processing and then sent to the server, a request channel is not occupied, and therefore flow is saved and network request response time is reduced; for the small and large modules with small quantity of network requests, batch processing identification information is not injected, and the network requests are directly queued for sending. The scheme provided by the embodiment can be flexibly set according to the service requirement, so that one part of network requests are sent to the server after being subjected to batch processing, and the other part of network requests are sent to the server through the request channel, and the resources of the client can be fully utilized.
Or dividing network requests with similar or related functions into the same module, and injecting batch processing mark information into the network requests so as to synchronize the time of the application program responding to the network requests of the module, so that the display interface is neat and ordered, and the user experience is improved.
As an optional implementation manner of the embodiment of the present invention, the step 101 includes: judging whether the network request carries batch processing mark information or not; and if the network request carries batch processing mark information, determining that a batch processing mode is started for the network request.
In the embodiment, batch processing mark information is injected into the network requests in advance, so that which network requests need to be subjected to batch processing and which network requests do not need to be subjected to batch processing can be conveniently and quickly distinguished; compared with the scheme of randomly starting the batch processing mode, the method has higher pertinence, ensures that the network request processing mode is more reasonable and efficient, and is beneficial to reasonably utilizing the resources of the client.
In this embodiment, the batch mode may be started by calling a beginBatch function, and if the batch mode is started, step 102 is executed.
Step 102, if the batch processing mode is started, putting the network request into a first queue;
in this embodiment, if it is determined in step 101 that the batch processing mode is started, the network request after the batch processing mode is started is placed in the first queue. And if the batch processing adopts a transaction mechanism, the first queue is a transaction queue.
Step 103, judging whether preset batch processing conditions are met;
in this embodiment, the network requests are batched, and when the network requests in the first queue meet a preset batching condition, the current network requests in the first queue are merged.
As shown in fig. 2, as an optional implementation manner of the embodiment of the present invention, the determining whether the preset batch processing condition is met includes:
step 1031, judging whether the number of the network requests put into the first queue reaches a first threshold value or judging whether the duration of starting the batch processing mode reaches a second threshold value;
step 1032, if the number of the network requests put into the first queue reaches a first threshold, or the duration of the batch processing mode starting reaches a second threshold, it is determined that the preset batch processing condition is met.
In this embodiment, whether the network requests placed in the first queue satisfy the preset batching condition or not can be judged by presetting the number of the network requests or presetting the time length from the earliest network request entering the first queue to the current time, or judging whether the network requests placed in the first queue satisfy the preset batching condition or not by combining the two network requests; the number of the merged network requests can be reasonably set by presetting the first threshold, and the time for waiting for the network requests in each batch can be reasonably set by presetting the second threshold; therefore, the network request speed can be effectively improved. The preset batch processing conditions are not limited in this embodiment, and can be set correspondingly according to actual needs.
For example: the application program A sends 30 network requests through the Web service, and under the condition that the batch processing mode is started, the preset batch processing conditions are set as follows: the number of the current network requests in the first queue is 8 or the time length from the first network request entering the current transaction queue to the current time is greater than B. If the number of the current network requests in the first queue reaches 8, or the time length from the current time to the first network request entering the current transaction queue is greater than B, the preset batch processing condition is met, and step 104 is executed. Therefore, 8 network requests are combined into a group and then sent to the server, the collision rate of the network requests is improved, the network requests only need to be sent to the server for 4 times, the response speed of the network requests is high, and the user experience is improved.
And step 104, if the preset batch processing conditions are met, merging the network requests in the first queue.
In this embodiment, if the network requests in the first queue meet the preset batch processing condition, merging the network requests in the first queue; according to the embodiment, a plurality of network requests can be merged and submitted to the server once again, and an idle sending channel does not need to be waited, so that the number of interaction times with the server is reduced, and the network request speed can be improved. In addition, the network requests in the designated batches are merged together by using a transaction mechanism, so that the collision rate of the merging of the network requests is improved, the efficiency is higher than that of random merging, and the designated task data can be returned at one time.
Optionally, the method of this embodiment further includes, after step 104:
and 105, submitting a batch of combined network requests to a server.
In this embodiment, the merged network request may be submitted by using an executeBatch function, so that the server returns the request data once, the number of times of interaction between the network request and the server is reduced, and the network request speed can be increased.
As another alternative implementation of the embodiment of the present invention, as shown in fig. 3, the method includes:
step 201, judging whether a batch processing mode is started;
in this embodiment, the method and the implementation principle for determining whether to start the batch processing mode are similar to those described in step 101, and are not described herein again.
Step 202, if the batch processing mode is not started, the network request is put into a second queue;
in this embodiment, the second queue is a request management queue, and if the batch processing mode is not started, the network request is placed in the request management queue for queuing.
Step 203, judging whether an idle sending channel exists;
and step 204, if an idle sending channel exists, merging the network requests in the second queue.
In this embodiment, when an idle sending channel exists, merging the network requests in the second queue, and sending the merged network requests to the server through the idle sending channel; for example: the number of the sending channels is 4, the first 4 network requests sent by the application program respectively occupy one sending channel, after one network request returns data, the sending channel occupied by the network request is idle, at this time, the network requests in the current second queue are merged and sent to the server through the idle sending channel, and so on, until all the network requests sent by the application program are sent to the server. Compared with the method for sending the network requests one by one through the sending channels, the method for merging the network requests in the embodiment reduces the number of times of interaction with the server and improves the speed of the network requests.
As shown in fig. 4, as a further alternative implementation of the embodiment of the present invention, the method includes:
step 301, judging whether the number of the current network requests exceeds a third threshold value;
in this embodiment, the third threshold may be set in advance, for example, the third threshold is set to 6, and if the number of the current network requests is less than 6, the number of the current network requests is small; and if the number of the current network requests is more than 6, the number of the current network requests is more. Judging whether the number of the current network requests exceeds a third threshold value, and if the number of the current network requests exceeds the third threshold value, executing the step 302; if the number of the current network requests does not exceed the third threshold, the network requests can be directly put into the request management queue and sent to the server through the sending channel, and at this time, because the number of the network requests is small, the waiting time of the network requests in the request management queue is short, the network requests do not need to be executed with combination processing, and the running resources of the terminal equipment can be effectively saved.
Step 302, if the number of the current network requests exceeds a third threshold, judging whether a batch processing mode is started;
in this embodiment, if the number of the current network requests exceeds the third threshold, the number of the current network requests is large, and the network request speed can be increased by adopting a merging processing mode for the current network requests. At this time, whether a batch processing mode is started is judged, if the batch processing mode is not started, the current network request is put into a request management queue, and the network request merging method shown in fig. 3 is executed; if the batch mode is enabled, step 303 is performed.
Step 303, if the batch processing mode is started, putting the network request into a first queue;
in this embodiment, if the batch processing mode is started, the method and the implementation principle for placing the network request into the first queue are similar to those described in step 102, and are not described herein again.
Step 304, judging whether preset batch processing conditions are met;
in this embodiment, the method and the implementation principle for determining whether the preset batch processing condition is satisfied are similar to those described in step 103, and are not described herein again.
And 305, if the preset batch processing condition is met, merging the network requests in the first queue.
In this embodiment, if the preset batch processing condition is satisfied, the method and the implementation principle for merging the network requests in the first queue are similar to those described in step 103, and are not described herein again.
As an optional implementation manner of the embodiment of the present invention, the determining, by the batch processing determining module, whether the batch processing mode is started includes: judging whether the network request carries batch processing mark information or not; and if the network request carries batch processing mark information, determining that a batch processing mode is started for the network request.
In this embodiment, according to the service requirements of each module in the application program, for example: the browser comprises modules such as latest movies, mini games and novel major and the like, and batch processing mark information can be injected into the mini game modules in advance for the mini game modules with more network requests, so that when the small game modules are loaded by the browser, the network requests sent by the mini game modules are subjected to batch processing and then sent to the server, a request channel is not occupied, and therefore flow is saved and network request response time is reduced; for the small and large modules with small quantity of network requests, batch processing identification information is not injected, and the network requests are directly queued for sending. The scheme provided by the embodiment can be flexibly set according to the service requirement, so that one part of network requests are sent to the server after being subjected to batch processing, and the other part of network requests are sent to the server through the request channel, and the resources of the client can be fully utilized.
Or dividing network requests with similar or related functions into the same module, and injecting batch processing mark information into the network requests so as to synchronize the time of the application program responding to the network requests of the module, so that the display interface is neat and ordered, and the user experience is improved.
As an optional implementation manner of the embodiment of the present invention, the merging processing apparatus includes an injection module, and the injection module is configured to inject batch processing flag information into a predetermined network request.
In the embodiment, batch processing mark information is injected into the network requests in advance, so that which network requests need to be subjected to batch processing and which network requests do not need to be subjected to batch processing can be conveniently and quickly distinguished; compared with the scheme of randomly starting the batch processing mode, the method has higher pertinence, ensures that the network request processing mode is more reasonable and efficient, and is beneficial to reasonably utilizing the resources of the client.
In a second aspect, the present embodiment provides a merging processing device for network requests, so as to improve the network request speed and improve the user experience.
Fig. 5 is a schematic structural diagram of a merging processing device for network requests according to a second embodiment of the present invention, and as shown in fig. 5, the present embodiment is applied to a terminal device. The device of the embodiment comprises: a batch processing judging module 41, configured to judge whether a batch processing mode is turned on; a first operation module 42, configured to place the network request into a first queue if the batch processing mode is started; a first condition judgment module 43, configured to judge whether a preset batch processing condition is met; the first merging module 44 is configured to merge the network requests in the first queue if a preset batching condition is met.
The apparatus of this embodiment may be used to implement the technical solution of the method embodiment shown in fig. 1, and the implementation principle and the technical effect are similar, which are not described herein again.
As shown in fig. 6, as an optional implementation manner of the embodiment of the present invention, the first condition determining module 53 is specifically configured to: judging whether the number of the network requests put into the first queue reaches a first threshold value or whether the duration of starting the batch processing mode reaches a second threshold value; and if the number of the network requests put into the first queue reaches a first threshold value or the starting time of the batch processing mode reaches a second threshold value, determining that the preset batch processing condition is met.
The apparatus of this embodiment may be used to implement the technical solutions of the method embodiments shown in fig. 1 or fig. 2, and the implementation principles and technical effects are similar, which are not described herein again.
As shown in fig. 7, as another alternative implementation of the embodiment of the present invention, the apparatus further includes: a second operation module 65, configured to, if the batch processing mode is not started, place the network request into a second queue; an idle channel determining module 66, configured to determine whether an idle sending channel exists; and a second merging module 67, configured to merge the network requests in the second queue if there is an idle sending channel.
The apparatus of this embodiment may be used to implement the technical solutions of the method embodiments shown in fig. 1 or fig. 3, and the implementation principles and technical effects are similar, which are not described herein again.
As shown in fig. 8, as a further alternative implementation of the embodiment of the present invention, the apparatus further includes: a third threshold determining module 71, configured to determine whether the number of current network requests exceeds a third threshold; the batch processing determining module 72 is configured to determine whether a batch processing mode is started if the number of the current network requests exceeds a third threshold.
The apparatus of this embodiment may be used to implement the technical solutions of the method embodiments shown in fig. 1 or fig. 4, and the implementation principles and technical effects are similar, which are not described herein again.
In a third aspect, fig. 8 is a schematic structural diagram of an embodiment of an electronic device of the present invention, which may implement the flows shown in fig. 1 to 4 of the present invention, and as shown in fig. 8, the electronic device may include: the device comprises a shell 71, a processor 72, a memory 73, a circuit board 74 and a power circuit 75, wherein the circuit board 74 is arranged inside a space enclosed by the shell 71, and the processor 72 and the memory 73 are arranged on the circuit board 74; a power supply circuit 75 for supplying power to each circuit or device of the electronic apparatus; the memory 73 is used to store executable program code; the processor 72 executes a program corresponding to the executable program code by reading the executable program code stored in the memory 73, for performing any of the embodiments described above.
For the specific execution process of the above steps by the processor 72 and the steps further executed by the processor 72 by running the executable program code, reference may be made to the description of the embodiment shown in fig. 1 to 4 of the present invention, which is not described herein again.
The electronic device exists in a variety of forms, including but not limited to:
(1) a mobile communication device: such devices are characterized by mobile communications capabilities and are primarily targeted at providing voice, data communications. Such terminals include: smart phones (e.g., iphones), multimedia phones, functional phones, and low-end phones, among others.
(2) Ultra mobile personal computer device: the equipment belongs to the category of personal computers, has calculation and processing functions and generally has the characteristic of mobile internet access. Such terminals include: PDA, MID, and UMPC devices, etc., such as ipads.
(3) A portable entertainment device: such devices can display and play multimedia content. This type of device comprises: audio, video players (e.g., ipods), handheld game consoles, electronic books, and smart toys and portable car navigation devices.
(4) A server: the device for providing the computing service comprises a processor, a hard disk, a memory, a system bus and the like, and the server is similar to a general computer architecture, but has higher requirements on processing capacity, stability, reliability, safety, expandability, manageability and the like because of the need of providing high-reliability service.
(5) And other electronic equipment with data interaction function.
In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium storing one or more programs, which are executable by one or more processors to implement the method according to any one of the embodiments of the present invention.
It should be noted that, in this document, relational terms such as first and second, and the like are used only for description
One entity or operation is distinguished from another entity or operation by no means required or implied
There may be any such actual relationship or order between the entities or operations. Also, the terms "include", "bag" and the like
The terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. The term "comprising", without further limitation, means that the element so defined is not excluded from the group consisting of additional identical elements in the process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments.
In particular, as for the apparatus embodiment, since it is substantially similar to the method embodiment, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
For convenience of description, the above devices are described separately in terms of functional division into various units/modules. Of course, the functionality of the units/modules may be implemented in one or more software and/or hardware implementations of the invention.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by a computer program, which can be stored in a computer-readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. The storage medium may be a magnetic disk, an optical disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), or the like.
The above description is only for the specific embodiment of the present invention, but the scope of the present invention is not limited thereto, and any changes or substitutions that can be easily conceived by those skilled in the art within the technical scope of the present invention are included in the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.