Method and terminal for requesting data over long connections

By creating multiple cache objects in the cache proxy layer and performing hash mapping, the problem of data access disorder in long-connection access is solved, and data access efficiency is improved and machine resource requirements are reduced while locking is implemented.

CN115543193BActive Publication Date: 2026-03-17FUJIAN TIANQUAN EDUCATION TECH LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-21
Publication Date
2026-03-17

AI Technical Summary

Technical Problem

Existing technologies are prone to errors when accessing long-connection data in high-concurrency scenarios. In order to ensure data consistency, locking is required, which leads to performance loss and requires additional machine resources.

Method used

Multiple cache objects are created in the cache proxy layer, and a mapping relationship is established between long connections and cache objects. Data requests and locking are performed in the cache objects corresponding to long connections to avoid locking the entire cache proxy layer. The cache objects are determined by hash calculation.

Benefits of technology

This achieves fast data access while locking, reducing the demand on machine resources and improving data access efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115543193B_ABST
    Figure CN115543193B_ABST
Patent Text Reader

Abstract

The application discloses a long connection request data method and a terminal. A first number of first cache objects are created in a cache proxy layer, and a mapping relationship between a long connection and the first cache objects is established when the long connection is created. A request for locally cached data and locking are performed in the first cache object corresponding to the long connection. Therefore, the locally cached data is operated through the cache proxy layer, and the cache proxy layer is subdivided into a plurality of cache objects. When the locally cached data is subjected to locking processing, the entire cache proxy layer does not need to be locked, but the cache objects therein are locked, which does not affect other cache objects, so that resource locking is realized while ensuring fast data access. Further, since the cache objects in the cache proxy layer are locked, a plurality of instances do not need to be deployed, thereby reducing additional machine resources.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data memory technology, and in particular to a method and terminal for requesting data via a long connection. Background Technology

[0002] In today's booming mobile internet era, most mobile apps offer push notification functionality, such as trending news recommendations on news apps, chat message notifications in instant messaging tools, e-commerce product promotions, and notifications and approval processes in enterprise applications. Push notifications play a crucial role in improving product activity, increasing the usage rate of functional modules, enhancing user stickiness, and improving user retention.

[0003] Many push notification applications now employ a multi-cluster deployment of an access service. Each time, the client establishes a persistent connection with one of the access applications within this cluster. Once established, the server and client maintain this persistent connection, and all message sending and receiving is conducted through this connection. There are numerous such persistent connections; typically, each access application maintains tens of thousands of such connections.

[0004] In current access applications, information about these long-lived connections is stored in local memory, typically using the client's unique identifier as the key and other relevant information as a value object. Because this object is shared, multiple connections attempting to access it can lead to data corruption. Currently, to ensure rapid connection establishment for push notifications, systems generally do not lock this object to improve performance. However, in high-concurrency scenarios, data corruption may still occur. For more demanding systems, locking is implemented, which limits performance. Therefore, multiple application instances need to be deployed on the server to distribute this performance loss, requiring additional machine resources and increasing costs. Summary of the Invention

[0005] The technical problem to be solved by the present invention is to provide a method and terminal for requesting data through a long connection, which can lock resources while ensuring fast data access and reducing additional machine resources.

[0006] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:

[0007] A method for requesting data via a long-lived connection includes the following steps:

[0008] Create the first number of first cache objects in the cache proxy layer;

[0009] When creating a persistent connection, a mapping relationship is established between the persistent connection and the first cache object;

[0010] In the first cache object corresponding to the long connection, request and lock the local cache data.

[0011] To solve the above-mentioned technical problems, another technical solution adopted by the present invention is as follows:

[0012] A terminal for requesting data via a long-connection includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it performs the following steps:

[0013] Create the first number of first cache objects in the cache proxy layer;

[0014] When creating a persistent connection, a mapping relationship is established between the persistent connection and the first cache object;

[0015] In the first cache object corresponding to the long connection, request and lock the local cache data.

[0016] The beneficial effects of this invention are as follows: A first number of first cache objects are created in the cache proxy layer, and a mapping relationship is established between the long connection and the first cache objects when a long connection is created. Local cached data requests and locking are performed within the first cache objects corresponding to the long connection. Therefore, by operating on local cached data through the cache proxy layer, and by subdividing the cache proxy layer into multiple cache objects, locking local cached data does not require locking the entire cache proxy layer; instead, locking is applied to specific cache objects within it, without affecting other cache objects. This ensures fast data access while achieving resource locking. Furthermore, since locking is applied to the cache objects within the cache proxy layer, multiple instances do not need to be deployed, thereby reducing additional machine resources. Attached Figure Description

[0017] Figure 1 This is a flowchart illustrating a method for requesting data via a long connection according to an embodiment of the present invention;

[0018] Figure 2 This is a schematic diagram of a terminal requesting data via a long connection, according to an embodiment of the present invention.

[0019] Label Explanation:

[0020] 1. A terminal for requesting data via a long-lived connection; 2. A memory; 3. A processor. Detailed Implementation

[0021] To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.

[0022] Please refer to Figure 1This invention provides a method for requesting data via a long connection, comprising the following steps:

[0023] Create the first number of first cache objects in the cache proxy layer;

[0024] When creating a persistent connection, a mapping relationship is established between the persistent connection and the first cache object;

[0025] In the first cache object corresponding to the long connection, request and lock the local cache data.

[0026] As described above, the beneficial effects of this invention are as follows: a first number of first cache objects are created in the cache proxy layer, and a mapping relationship is established between the long connection and the first cache object when a long connection is created. Local cached data requests and locking are performed within the first cache object corresponding to the long connection. Therefore, by operating on local cached data through the cache proxy layer, and by subdividing the cache proxy layer into multiple cache objects, locking local cached data does not require locking the entire cache proxy layer; instead, locking is applied to specific cache objects within it, without affecting other cache objects. This ensures fast data access while achieving resource locking. Furthermore, since locking is applied to the cache objects within the cache proxy layer, multiple instances do not need to be deployed, thereby reducing additional machine resources.

[0027] Furthermore, requesting and locking local cached data in the first cache object corresponding to the long connection includes:

[0028] When the long connection request performs a data read operation, the local cached data is obtained directly through the first cache object;

[0029] When the long connection request performs write and read operations, if the first cache object is in an idle state, local cache data is written through the first cache object and the first cache object is locked. If the first cache object is not in an idle state, the long connection request is assigned to other cache objects for processing.

[0030] As described above, when writing local cached data through the first cache object, if the first cache object is in an idle state, it will be locked; otherwise, the long connection request will be distributed to other cache objects for processing, thereby improving data access efficiency.

[0031] Furthermore, establishing a mapping relationship between the long connection and the first cache object includes:

[0032] A unique identifier is set for the long connection, and a hash calculation is performed on the unique identifier to obtain the hash calculation result;

[0033] Based on the hash calculation result, the first cache object corresponding to the long connection is determined in the cache proxy layer, and a mapping relationship is established.

[0034] As described above, by determining the first cache object corresponding to the long connection through the hash calculation result of the unique identifier of the long connection, the one-to-one correspondence between the long connection and the first cache object can be established, which facilitates the subsequent processing of data requests for the long connection directly based on the first cache object.

[0035] Furthermore, it also includes:

[0036] Determine whether the number of long connections has reached the first preset number. If so, create a second number of second cache objects in the cache proxy layer and re-establish the mapping relationship between long connections and second cache objects.

[0037] Switch the long-lived connection request from the corresponding first cache object to the corresponding second cache object, and delete the first cache object;

[0038] The first quantity is less than the second quantity.

[0039] As described above, when the number of long connections reaches the first preset number, dynamically expanding the cache objects in the cache proxy layer can improve request efficiency.

[0040] Furthermore, it also includes:

[0041] Determine whether the number of long connections is less than the second preset number. If so, create a third number of third cache objects in the cache proxy layer and re-establish the mapping relationship between long connections and third cache objects.

[0042] Switch the long-lived connection request from the corresponding second cache object to the corresponding third cache object, and delete the second cache object;

[0043] The third quantity is less than the second quantity.

[0044] As can be seen from the above description, when the number of long connections is less than the second preset number, dynamically scaling down the cache objects in the cache proxy layer can reduce resource consumption.

[0045] Please refer to Figure 2 Another embodiment of the present invention provides a terminal for requesting data via a long connection, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it performs the following steps:

[0046] Create the first number of first cache objects in the cache proxy layer;

[0047] When creating a persistent connection, a mapping relationship is established between the persistent connection and the first cache object;

[0048] In the first cache object corresponding to the long connection, request and lock the local cache data.

[0049] As described above, a first number of cache objects are created in the cache proxy layer, and a mapping relationship is established between the long connection and the first cache object when a long connection is created. Local cache data requests and locking are then performed within the first cache object corresponding to the long connection. Therefore, by operating on local cache data through the cache proxy layer, and by subdividing the cache proxy layer into multiple cache objects, locking local cache data does not require locking the entire cache proxy layer; instead, locking is applied to specific cache objects within it, without affecting other cache objects. This ensures fast data access while achieving resource locking. Furthermore, since locking is applied to the cache objects within the cache proxy layer, multiple instances do not need to be deployed, thus reducing additional machine resources.

[0050] Furthermore, requesting and locking local cached data in the first cache object corresponding to the long connection includes:

[0051] When the long connection request performs a data read operation, the local cached data is obtained directly through the first cache object;

[0052] When the long connection request performs write and read operations, if the first cache object is in an idle state, local cache data is written through the first cache object and the first cache object is locked. If the first cache object is not in an idle state, the long connection request is assigned to other cache objects for processing.

[0053] As described above, when writing local cached data through the first cache object, if the first cache object is in an idle state, it will be locked; otherwise, the long connection request will be distributed to other cache objects for processing, thereby improving data access efficiency.

[0054] Furthermore, establishing a mapping relationship between the long connection and the first cache object includes:

[0055] A unique identifier is set for the long connection, and a hash calculation is performed on the unique identifier to obtain the hash calculation result;

[0056] Based on the hash calculation result, the first cache object corresponding to the long connection is determined in the cache proxy layer, and a mapping relationship is established.

[0057] As described above, by determining the first cache object corresponding to the long connection through the hash calculation result of the unique identifier of the long connection, the one-to-one correspondence between the long connection and the first cache object can be established, which facilitates the subsequent processing of data requests for the long connection directly based on the first cache object.

[0058] Furthermore, it also includes:

[0059] Determine whether the number of long connections has reached the first preset number. If so, create a second number of second cache objects in the cache proxy layer and re-establish the mapping relationship between long connections and second cache objects.

[0060] Switch the long-lived connection request from the corresponding first cache object to the corresponding second cache object, and delete the first cache object;

[0061] The first quantity is less than the second quantity.

[0062] As described above, when the number of long connections reaches the first preset number, dynamically expanding the cache objects in the cache proxy layer can improve request efficiency.

[0063] Furthermore, it also includes:

[0064] Determine whether the number of long connections is less than the second preset number. If so, create a third number of third cache objects in the cache proxy layer and re-establish the mapping relationship between long connections and third cache objects.

[0065] Switch the long-lived connection request from the corresponding second cache object to the corresponding third cache object, and delete the second cache object;

[0066] The third quantity is less than the second quantity.

[0067] As can be seen from the above description, when the number of long connections is less than the second preset number, dynamically scaling down the cache objects in the cache proxy layer can reduce resource consumption.

[0068] The method and terminal for requesting data via long-connection described above are applicable to reading, writing, and locking data via long-connection in local caching. They can achieve resource locking while ensuring fast data access and reducing additional machine resources. The following detailed implementation methods illustrate this:

[0069] Example 1

[0070] Please refer to Figure 1 A method for requesting data via a long-lived connection, comprising the following steps:

[0071] S1. Create the first number of first cache objects in the cache proxy layer.

[0072] In this embodiment, 100 first cache objects are created in the cache proxy layer.

[0073] S2. When creating a long connection, establish a mapping relationship between the long connection and the first cache object.

[0074] S21. Set a unique identifier for the long connection, perform a hash calculation on the unique identifier, and obtain the hash calculation result.

[0075] In this embodiment, when creating a connection and needing to retrieve local cache information, each connection has a channelId, which is passed to the cache proxy layer. The cache proxy layer performs a hash calculation based on the channelId to obtain the hash result.

[0076] S22. Based on the hash calculation result, determine the first cache object corresponding to the long connection in the cache proxy layer and establish a mapping relationship.

[0077] Specifically, by establishing a mapping relationship between the long connection and the first cache object based on the hash calculation result, the data requested by the long connection can be processed in the corresponding first cache object.

[0078] S3. Request and lock local cached data in the first cache object corresponding to the long connection.

[0079] S31. When the long connection request performs a data reading operation, the local cached data is directly obtained through the first cache object.

[0080] S32. When the long connection request performs write and read operations, if the first cache object is in an idle state, local cache data is written through the first cache object and the first cache object is locked. If the first cache object is not in an idle state, the long connection request is allocated to other cache objects for processing.

[0081] Specifically, during write operations, locking is required. If requests for different cached data arrive at this time, they are distributed to other smaller cache objects for processing according to hash rules. If requests for the same cached data arrive, they are queued and processed when the cache object becomes available. In this way, locking only applies to a small cache object in the scenario, rather than locking the entire cache proxy layer, thus improving access efficiency.

[0082] In summary, this embodiment uses long-lived connection hash calculation to make long-lived connections correspond to multiple cache objects in the cache proxy layer, thereby realizing sharding in the local cache through hash and locking the cache shards. Therefore, while realizing resource locking, it can ensure fast data access and reduce additional machine resources.

[0083] Example 2

[0084] Compared to Embodiment 1, this embodiment further defines the method for scaling up and down the cache object:

[0085] The expansion method is as follows: determine whether the number of long connections has reached the first preset number. If so, create a second number of second cache objects in the cache proxy layer and re-establish the mapping relationship between long connections and second cache objects.

[0086] Switch the long-lived connection request from the corresponding first cache object to the corresponding second cache object, and delete the first cache object;

[0087] The first quantity is less than the second quantity.

[0088] In this embodiment, a monitoring program is present. When connection creation is frequent or in a high-risk system scenario, it needs to automatically reduce the locking strength. Based on monitoring information, when the cache size reaches a preset threshold, an asynchronous thread is pre-triggered. In this embodiment, 200 new second cache objects are pre-created. Historical statistical data needs to be analyzed, and the program automatically calculates the number of objects requiring expansion, thereby achieving data expansion.

[0089] If a write operation is performed, a locked write operation is performed on 200 new second cache objects. These 200 second cache objects are pre-hashized; once the second cache object corresponding to the long-lived connection is found, it is locked, and the data is written. If a read operation is performed, the 200 second cache objects are pre-hashized; once the second cache object corresponding to the long-lived connection is found, the data is read. If no data is found, it is read according to the mapping relationship with the first cache object.

[0090] The cached data in the original 100 first cache objects is automatically migrated to the corresponding second cache objects according to the new number of hashes. After the update is completed, the proxy layer switches the requests to these 200 second cache objects, realizing a cache expansion and thus improving efficiency.

[0091] The scaling down method is as follows: determine whether the number of long connections is less than the second preset number; if so, create a third number of third cache objects in the cache proxy layer and re-establish the mapping relationship between long connections and third cache objects.

[0092] Switch the long-lived connection request from the corresponding second cache object to the corresponding third cache object, and delete the second cache object;

[0093] The third quantity is less than the second quantity.

[0094] In this embodiment, a monitoring program reduces the number of cached objects when connection creation decreases. Based on monitoring information, when the number of cached objects falls below a preset threshold, an asynchronous thread is pre-triggered. In this embodiment, 120 new third-level cached objects are pre-created. This pre-calculation requires analysis of historical statistical data, and the program automatically calculates the number of objects to be scaled down, thereby achieving data scaling down.

[0095] If a write operation is performed, a locked write operation is performed on 120 new third cache objects. The 120 second cache objects are pre-hashized to find the corresponding third cache object for the long-lived connection, which is then locked before the data is written. If a read operation is performed, the 120 third cache objects are pre-hashized to find the corresponding third cache object for the long-lived connection. If no data is found, it is read according to the mapping relationship with the second cache objects.

[0096] The cached data in the original 200 second cache objects is automatically migrated to the corresponding third cache objects according to the new number of hashes. After the update is completed, the proxy layer switches the requests to these 120 third cache objects, realizing a cache expansion and thus improving efficiency.

[0097] Example 3

[0098] Please refer to Figure 2 A terminal 1 for requesting data via a long connection includes a memory 2, a processor 3, and a computer program stored on the memory 2 and executable on the processor 3. When the processor 3 executes the computer program, it implements the various steps of a method for requesting data via a long connection according to Embodiment 1 or 2.

[0099] In summary, the present invention provides a method and terminal for requesting data via long-connection. A first number of first cache objects are created in a cache proxy layer, and a mapping relationship is established between the long connection and the first cache objects when the long connection is created. Local cached data is requested and locked within the first cache object corresponding to the long connection. Therefore, by operating on local cached data through a cache proxy layer, and by subdividing the cache proxy layer into multiple cache objects, locking local cached data does not require locking the entire cache proxy layer; instead, locking is applied to specific cache objects, without affecting other cache objects. This achieves local cache sharding through hashing and locking of cache shards, ensuring fast data access while implementing resource locking. Furthermore, since locking is applied to cache objects within the cache proxy layer, multiple instances do not need to be deployed, thus reducing additional machine resources.

[0100] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.

Claims

1. A method of requesting data for a long connection, characterized by, The method comprises the steps of: creating a first number of first cache objects in a cache proxy layer; establishing a mapping relationship between the long connection and the first cache object when the long connection is created; requesting and locking local cache data in the first cache object corresponding to the long connection: when the long connection requests a data reading operation, local cache data is directly obtained through the first cache object; when the long connection requests a write reading operation, if the first cache object is in an idle state, write local cache data through the first cache object and lock the first cache object, and if the first cache object is not in an idle state, distribute the long connection request to other cache objects for processing.

2. The method of claim 1, wherein, The mapping relationship between the long connection and the first cache object comprises: setting a unique identifier for the long connection, performing hash calculation on the unique identifier to obtain a hash calculation result; determining the first cache object corresponding to the long connection in the cache proxy layer according to the hash calculation result, and establishing a mapping relationship.

3. The method of claim 1, wherein, Further comprising: determining whether the number of long connections reaches a first preset number, if yes, creating a second number of second cache objects in the cache proxy layer, and re-establishing the mapping relationship between the long connection and the second cache object; switching the long connection request from the corresponding first cache object to the corresponding second cache object, and deleting the first cache object; The first number is less than the second number.

4. The method of claim 3, wherein, Further comprising: determining whether the number of long connections is less than a second preset number, if yes, creating a third number of third cache objects in the cache proxy layer, and re-establishing the mapping relationship between the long connection and the third cache object; switching the long connection request from the corresponding second cache object to the corresponding third cache object, and deleting the second cache object; The third number is less than the second number.

5. A terminal requesting data for a long connection, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the computer program to implement the following steps: creating a first number of first cache objects in a cache proxy layer; establishing a mapping relationship between the long connection and the first cache object when the long connection is created; requesting and locking local cache data in the first cache object corresponding to the long connection: when the long connection requests a data reading operation, local cache data is directly obtained through the first cache object; when the long connection requests a write reading operation, if the first cache object is in an idle state, write local cache data through the first cache object and lock the first cache object, and if the first cache object is not in an idle state, distribute the long connection request to other cache objects for processing.

6. The terminal of claim 5, wherein, The mapping relationship between the long connection and the first cache object comprises: setting a unique identifier for the long connection, performing hash calculation on the unique identifier to obtain a hash calculation result; determining the first cache object corresponding to the long connection in the cache proxy layer according to the hash calculation result, and establishing a mapping relationship.

7. The terminal of claim 5, wherein the long connection request data comprises a request for a connection to a network. Further comprising: determining whether the number of long connections reaches a first preset number, if yes, creating a second number of second cache objects in the cache proxy layer, and re-establishing a mapping relationship between the long connections and the second cache objects; switching the long connection request from the corresponding first cache object to the corresponding second cache object, and deleting the first cache object; the first number is less than the second number.

8. The long connection request data terminal of claim 7, wherein, Further comprising: determining whether the number of long connections is less than a second preset number, if yes, creating a third number of third cache objects in the cache proxy layer, and re-establishing a mapping relationship between the long connections and the third cache objects; switching the long connection request from the corresponding second cache object to the corresponding third cache object, and deleting the second cache object; the third number is less than the second number.

Citation Information

Patent Citations

  • Cache control method and device, terminal and storage medium

    CN109491928A

  • Method and system for dynamically adjusting number of threads based on distributed coordination system

    CN113391892A