Data Processing Method and Device in Cache
By assigning the same target cache identifier to the target thread and switching caches according to the system status and the relationship between threads, the problem of inconsistent reading of data when cache data is updated is solved, the accuracy of data acquisition and update is improved, and the normal operation of the business is ensured.
Patent Information
- Application Number
- CN202210725664.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-24
- Publication Date
- 2025-06-10
- Estimated Expiration
- 2042-06-24
AI Technical Summary
In the prior art, when the data in the cache is updated, it may cause the data values to be inconsistent when the thread reads the same data before and after the update, which reduces the accuracy of data acquisition and update and affects the normal implementation of the business.
By assigning the same target cache identifier to the target thread, according to the system state and the association relationship between threads, it is ensured that the data obtained by the corresponding threads of the same business are always consistent during an interaction process. The specific method includes obtaining data from the first cache in the first state, obtaining data from the second cache in the second state, and switching the cache state when the data is updated.
By ensuring the consistency of data obtained by threads, the inconsistent or inconsequential data reading caused by data updates are avoided, the accuracy of data acquisition and update is improved, and the normal implementation of business is ensured.
Smart Images

Figure CN115061816B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present application relate to the technical field of data processing, and in particular, to a method and apparatus for processing data in a cache. Background Art
[0002] With the development of computer technology, more and more technologies are applied in the financial field. The traditional financial industry is gradually transforming into financial technology (Fintech), and data processing technology is no exception. However, due to the security and real-time requirements of the financial industry, higher requirements are also put forward for data processing technology. In order to meet the needs of the growth of various financial businesses, the application of caches has become more and more common.
[0003] In the prior art, when implementing related services, data can be obtained from a cache through a thread, and then related services can be implemented based on the obtained data.
[0004] However, since the data in the cache may involve updates, and when updating the data, it also takes a certain amount of time to update the data one by one, it may cause the situation that the data values are inconsistent when the thread reads the same data before and after the update, or the corresponding relationship of related data is problematic, reducing the accuracy of data acquisition and update, and thus affecting the normal implementation of the service. Summary of the Invention
[0005] Embodiments of the present application provide a method and apparatus for processing data in a cache to improve the accuracy of data acquisition and update.
[0006] In a first aspect, an embodiment of the present application provides a method for processing data in a cache, including:
[0007] Obtaining a service processing request corresponding to a target service, and allocating at least one target thread to the service processing request corresponding to the target service;
[0008] Allocating the same target cache identifier to the at least one target thread according to the system state and the association relationship between the target threads, where the system state is a first state or a second state. When in the first state, data is obtained from a first cache, and when in the second state, data is obtained from a second cache, and different caches correspond to different cache identifiers. The first cache and the second cache are used to store the same data. The first state indicates that the update of the data in the first cache has been completed according to a data update request, but the update of the data in the second cache has not been completed. The second state indicates that the update of the data in the second cache has been completed according to the data update request;
[0009] Obtaining target data from the cache corresponding to the target cache identifier through the at least one target thread, and implementing the target service according to the target data.
[0010] Optionally, allocating the same target cache identifier for the at least one target thread according to the system state and the association relationship between threads includes:
[0011] Determine the system state and determine the target cache identifier according to the system state;
[0012] For each target thread, determine whether there is a cache identifier allocated by an associated thread for the target thread;
[0013] If the target thread does not have a cache identifier allocated by an associated thread, allocate the target cache identifier for the target thread.
[0014] Optionally, determining the target cache identifier according to the system state includes:
[0015] If the system state is the first state, determine that the target cache identifier is the cache identifier of the first cache corresponding to the first state;
[0016] If the system state is the second state, determine that the target cache identifier is the cache identifier of the second cache corresponding to the second state.
[0017] Optionally, it further includes:
[0018] If the target thread has a cache identifier allocated by an associated thread, determine the cache identifier allocated by the associated thread as the target cache identifier.
[0019] Optionally, the cache identifier is stored in the inheritable variable of the thread, and allocating at least one target thread for the service processing request corresponding to the target service includes:
[0020] Create a child thread of the associated thread to obtain at least one initial target thread;
[0021] Set the inheritable thread variable of the at least one initial target thread to the inheritable thread variable of the associated thread to obtain at least one target thread;
[0022] Allocate at least one target thread for the service processing request corresponding to the target service.
[0023] Optionally, the cache identifier is stored in the custom attribute information of the thread, and allocating at least one target thread for the service processing request corresponding to the target service includes:
[0024] Obtain at least one initial target thread from the thread pool;
[0025] Set the custom attribute information of the at least one initial target thread to the custom attribute information of the associated thread to obtain at least one target thread;
[0026] Allocate at least one target thread for the service processing request corresponding to the target service.
[0027] Optionally, it further includes:
[0028] Receive the data update request, where the data update request includes the to-be-updated data identifier and the corresponding to-be-updated data value;
[0029] Update the data value corresponding to the to-be-updated data identifier in the first cache corresponding to the first state to the to-be-updated data value according to the data update request, and set the system state to the first state;
[0030] Determine whether the thread allocated with the cache identifier corresponding to the second state has completed processing;
[0031] If the processing is completed, update the data value corresponding to the to-be-updated data identifier in the second cache corresponding to the second state to the to-be-updated data value;
[0032] Update the system state to the second state.
[0033] Optionally, it further includes:
[0034] If the processing is not completed, randomly wait for a target duration;
[0035] After waiting for the target duration, re-execute the steps of determining whether the thread allocated with the cache identifier corresponding to the second state has completed processing and subsequent steps.
[0036] Optionally, after obtaining the target data from the cache corresponding to the target cache identifier through the at least one target thread, it further includes:
[0037] Empty the target cache identifier of the at least one target thread.
[0038] In a second aspect, an embodiment of the present application provides a data processing device in a cache, including:
[0039] An acquisition module, configured to acquire a service processing request corresponding to a target service, and allocate at least one target thread for the service processing request corresponding to the target service;
[0040] A processing module, configured to allocate the same target cache identifier for the at least one target thread according to the system state and the association relationship between target threads, where the system state is a first state or a second state, data is obtained from a first cache in the first state, and data is obtained from a second cache in the second state, and different caches correspond to different cache identifiers, the first cache and the second cache are used to store the same data, the first state indicates that the data in the first cache has been updated according to a data update request, but the update of the data in the second cache has not been completed, and the second state indicates that the update of the data in the second cache has been completed according to the data update request;
[0041] The processing module is further configured to obtain target data from the cache corresponding to the target cache identifier through the at least one target thread, and implement the target service according to the target data.
[0042] An embodiment of the present application provides a method and device for processing data in a cache. After adopting the above solution, a service processing request corresponding to a target service can be obtained first, and at least one target thread can be allocated for the service processing request corresponding to the target service. Then, the same target cache identifier can be allocated for the at least one target thread corresponding to the same service according to the system state and the association relationship between target threads. Wherein, the system state can be a first state or a second state. Data is obtained from a first cache in the first state, and data is obtained from a second cache in the second state. Different caches correspond to different cache identifiers. The first cache and the second cache are used to store the same data. The first state indicates that the data in the first cache has been updated according to a data update request, but the update of the data in the second cache has not been completed. The second state indicates that the update of the data in the second cache has been completed according to the data update request. After allocating the same target cache identifier for the at least one target thread corresponding to the same service, the target service can be implemented by obtaining target data from the cache corresponding to the target cache identifier through the at least one target thread. By first allocating different caches for different system states, and the data in the corresponding cache in this system state is fixed, and then allocating the cache identifier of the same cache for the at least one target thread in an interaction process corresponding to a service processing request according to the system state and the association relationship between threads, it is ensured that the data obtained by the threads corresponding to the same service in an interaction process is always consistent, avoiding the situation that the data read by the threads is inconsistent or not corresponding due to data update, improving the accuracy of data acquisition and update, and thus ensuring the normal implementation of the service. Description of the Drawings
[0043] To more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the accompanying drawings required for the description of the embodiments or the prior art. Obviously, the accompanying drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can also be obtained based on these drawings.
[0044] Figure 1 Schematic diagram of the architecture of the application system for the data processing method in the cache provided by the embodiment of the present application;
[0045] Figure 2 Schematic diagram of the process of the data processing method in the cache provided by the embodiment of the present application;
[0046] Figure 3 Schematic diagram of the process of the data processing method in the cache provided by another embodiment of the present application;
[0047] Figure 4 Schematic diagram of the process of the cache switching process provided by the embodiment of the present application;
[0048] Figure 5 Schematic diagram of the structure of the data processing device in the cache provided by the embodiment of the present application;
[0049] Figure 6 Schematic diagram of the hardware structure of the electronic device provided by the embodiment of the present application. Detailed implementation manners
[0050] The following will clearly and completely describe the technical solutions in the embodiments of the present application in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, rather than all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present application.
[0051] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present application and the above accompanying drawings are used to distinguish similar objects, and do not necessarily need to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances, so that the embodiments of the present application described here can also include other sequence examples in addition to those illustrated or described. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units does not necessarily need to be limited to those clearly listed steps or units, but may include other steps or units that are not clearly listed or are inherent to these processes, methods, products or devices.
[0052] When implementing related services (such as applying for an account, transferring funds, purchasing related products), it may involve obtaining data from the cache through a thread and then implementing related services based on the obtained data. When the data in the cache needs to be updated, the data in the cache can be updated by reloading the data into the cache. However, since there is only one cache for storing data, even if the update logic runs very fast, the data is still updated one by one, which may cause the following problems:
[0053] First, the same thread reads the same data cache value before and after the update process and gets different results. For example, the data key1 needs to be updated from v1 to v2. The thread reads key1 and gets v1 before the cache is loaded, and reads key1 again and gets v2 after the cache is loaded. This situation can be called the non-repeatable readability of the parameter cache.
[0054] Second, when the same thread reads the associated data cache values before and after the update process, it may get incorrect results. For example, there are two associated data, the user name userName and the password password. Only when the two match can the data be considered correct. Now it is necessary to update userName=aaa, password=bbb to userName=kkk, password=vvv. Since the data in the cache is updated one by one, the userName can be updated from aaa to kkk first, and then the password can be updated from bbb to vvv. Then it is possible that a certain thread reads userName=kkk and password=bbb during the update of these two parameters, resulting in incorrect actual read data cache values. This situation can be called the inconsistent readability of the parameter cache.
[0055] The above two situations reduce the accuracy of data acquisition and update, and thus affect the normal implementation of the service.
[0056] In the prior art, there are cases where the aforementioned problem is solved by using two caches. The specific process is as follows: When the server starts, two caches are initialized, one of which is read-only and the other is writable. Before the interaction request starts, the client first requests the server to obtain a sessionId. The server allocates the sessionId and maintains the correspondence between the sessionId and the read-only cache. When the client obtains the parameter cache, it brings the sessionId. The server returns the value in the corresponding parameter cache according to the sessionId. Within one interaction, the sessionId is the same, so the read parameter cache block is also the same. Before the interaction request is completed, the client notifies the server to delete the correspondence between the sessionId and the read-only cache. The server periodically loads the latest parameters into the writable cache. After the writing is completed, the cache is switched. The original writable cache becomes the read-only cache, and the original read-only cache becomes the writable cache. For subsequent new interaction requests, the latest parameters in the switched read-only cache can be read.
[0057] However, whether the above solution can effectively achieve real-time consistency of parameters and repeatable read updates depends on whether the business system can accurately "apply to the server for a sessionId (used by the server to allocate the correct cache)" and "apply to the server to delete the sessionId (the server needs to determine that the cache is no longer in use by the client when updating the cache)", which has problems of high access cost and poor reliability. That is, when the user accesses the above solution, they need to apply for a sessionId at the entrance of each interaction request and delete the sessionId at the exit of the interaction request, even though this step is meaningless to the actual business processing logic. And since the application system usually has multiple interaction entrances, for example, a distributed system has multiple listening entrances, scheduled task execution entrances, etc., when using this solution, the user needs to analyze and find all possible interaction entrances in the system and add the logic of "applying to the server for a sessionId" at the entrance. In addition, because in an interaction process, there are not only the expected interaction result exits, such as interaction success, interaction failure, interaction timeout, etc.; there are also unpredictable abnormal end exits, such as code defect exceptions, network exceptions, machine failures, etc. For the expected exits, the user can manually call the operation to delete the sessionId, but for the unpredictable abnormal end exits, it is difficult for the user to ensure that the operation of deleting the sessionId is called in all cases. To ensure that the operation of deleting the sessionId at the interaction exit is not missed, the user needs a lot of analysis and verification. If the logic of "applying to the server for a sessionId" is omitted at some interaction entrances of the business system, then when the parameters are cached multiple times within the interaction, the aforementioned problems still exist.
[0058] Based on the above technical problems, in the present application, different caches are first allocated for different system states, and the data in the cache corresponding to the system state is fixed. Then, according to the system state and the association relationship between threads, the same cache identifier of the same cache is allocated to at least one target thread in an interaction corresponding to a service processing request, so that the data obtained by the threads corresponding to the same service in an interaction is always consistent, avoiding the situation that the data read by the threads is inconsistent or not corresponding due to data update, improving the accuracy of data acquisition and update, and thus ensuring the normal implementation of the service. Moreover, the present application allocates cache identifiers in terms of threads. A thread is the smallest execution unit of an application system, and each interaction (or service) is processed by multiple associated threads, and the application system can clearly know the start and end of the threads, simplifying the allocation process and maintenance difficulty of cache identifiers, and thus ensuring the normal implementation of the service.
[0059] Figure 1 The architecture diagram of the application system for the data processing method in the cache provided by the embodiment of the present application is as Figure 1 shown. In this application system, it may include a server, and caches corresponding to different system states are deployed in the server. For example, there may be a first cache corresponding to the system state of the first state, and a second cache corresponding to the system state of the second state. Among them, the first state is the system state when the data in the cache is updated, and the second state is the system state when the data in the cache is normally read.
[0060] After the server obtains a service processing request corresponding to a target service (wherein, a service processing request may correspond to an interaction), it can allocate at least one target thread for the service processing request corresponding to the target service, and then allocate the same target cache identifier to at least one target thread according to the system state and the association relationship between the target threads, and obtain target data from the cache corresponding to the target cache identifier through at least one target thread, and then the target service can be implemented according to the target data. Exemplarily, the target cache identifier may be the cache identifier of the second cache corresponding to the second state, and then the target data can be obtained from the second cache corresponding to the second state.
[0061] Optionally, the service processing request corresponding to the target service may be triggered regularly or in real time. Correspondingly, a service processing request can be triggered every preset duration according to a predefined timing task to implement the target service (for example, server inspection service, timing notification service, etc.). It can also trigger a service processing request in real time according to the user's touch operation, which will not be elaborated here.
[0062] The technical solution of the present application will be described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments.
[0063] Figure 2 It is a schematic flowchart of a data processing method in a cache provided by an embodiment of the present application. The method of this embodiment can be executed by a server. As Figure 2 shown, the method of this embodiment may include:
[0064] S201: Obtain a service processing request corresponding to a target service, and allocate at least one target thread to the service processing request corresponding to the target service.
[0065] In this embodiment, when implementing the target service, it can be achieved by triggering a service processing request. Optionally, the service processing request can be triggered regularly, or triggered in real time based on the user's touch operation. In addition, the service processing request corresponding to the target service can be directly obtained locally on the server, or the service processing request corresponding to the target service sent by the terminal device can be obtained, and no detailed limitation will be made here.
[0066] In addition, after obtaining the service processing request, at least one target thread can be allocated to the service processing request corresponding to the target service. Among them, the number of target threads can be one or more, and can be specifically allocated according to the actual number of threads required for the target task.
[0067] S202: Allocate the same target cache identifier to at least one target thread according to the system state and the association relationship between the target threads, where the system state is the first state or the second state. When in the first state, data is obtained from the first cache, and when in the second state, data is obtained from the second cache, and different caches correspond to different cache identifiers. The first cache and the second cache are used to store the same data. The first state indicates that the data in the first cache has been updated according to the data update request, but the data update in the second cache has not been completed. The second state indicates that the data in the second cache has been updated according to the data update request.
[0068] In this embodiment, to avoid inconsistent or mismatched data reading, the value of the same data for the same target task should be the same during one execution process (for example, during one execution process of the same target task, the port number is always 8888). Since during one execution process of the target task, the value of the same data in the cache corresponding to the same system state is the same, therefore, during one execution process of the target task, a cache identifier corresponding to the same system state can be assigned to at least one target thread, that is, the cache identifier corresponding to the same cache, and the same system state is the system state determined when the cache identifier is assigned to the first target thread. That is, the cache identifier corresponding to the first state or the cache identifier corresponding to the second state can be assigned to at least one target thread in one interaction process, so that at least one target thread assigned to the target task always obtains data from the same cache during one execution process of the target task, and the data in the cache is fixed in the corresponding current state. For example, in the first state, the data in the first cache corresponding to the first state is fixed, and in the second state, the data in the second cache corresponding to the second state is fixed, ensuring the consistency of data reading.
[0069] In addition, the system state can be the first state or the second state. Data can be obtained from the first cache in the first state and from the second cache in the second state, and different caches correspond to different cache identifiers. Among them, the first cache and the second cache are used to store the same data. The first state indicates that the data in the first cache has been updated according to the data update request, but the update of the data in the second cache has not been completed. The second state indicates that the data in the second cache has been updated according to the data update request. Optionally, when it is necessary to update the data in the cache, the data in the cache can be updated by receiving a data update request. When receiving the data update request, the system state is the second state, that is, at this time, the thread obtains data from the second cache corresponding to the second state. Therefore, the data in the first cache corresponding to the first state can be updated first. After the data in the first cache is updated, the system state can be updated to the first state. At this time, the thread needs to obtain data from the first cache corresponding to the first state. Therefore, the data in the second cache corresponding to the second state can be updated at this time, avoiding the situation of inconsistent data reading by the thread due to data update. In addition, when updating the data in the second cache corresponding to the second state, to ensure that the data obtained by the threads corresponding to the same interaction process from the cache is consistent, the data in the second cache corresponding to the second state can be updated after it is determined that all the threads reading the second cache have completed execution.
[0070] Optionally, the target thread(s) can be one or more. After receiving a service processing request, a target thread can be allocated for the service processing request corresponding to the target service first, and then a target cache identifier can be allocated for the target thread according to the system state. The target thread can obtain target data from the corresponding cache through the allocated target cache identifier. After the target thread finishes execution, it can also be determined whether other threads are still needed for the target service to continue implementing the target service. That is, it can be determined whether the target service has been completed. If it has been completed, the process can end; if not, a target thread can be continuously allocated, and the target cache identifier can be determined according to the association relationship between the newly allocated target thread and the previous target thread. The newly allocated target thread can obtain target data from the corresponding cache through the allocated target cache identifier until the target task is completed. Among them, the target cache identifiers of the newly allocated target thread and the previous target thread are the same, that is, the newly allocated target thread and the previous target thread both obtain data from the same cache, and the data in this cache remains unchanged under the current system state, ensuring the consistency of data reading.
[0071] Among them, the data update request can perform change operations, deletion operations, or addition operations on the data in the cache. And the data update request can be obtained before receiving the service processing request, can be obtained after receiving the service processing request, or can be obtained simultaneously with the service processing request. No detailed limitation is made here.
[0072] S203: Obtain target data from the cache corresponding to the target cache identifier through at least one target thread, and implement the target service according to the target data.
[0073] In this embodiment, the target thread(s) can be one or more. After allocating a target cache identifier for the target thread, the target service can be implemented according to the target thread to which the target cache identifier is allocated. Correspondingly, the target data can be obtained from the corresponding cache through the target thread to which the target cache identifier is allocated, and then the target service can be implemented according to the obtained target data. Exemplarily, the target service can be an account application service, a transfer service, a quota change service, etc. In addition, implementing the target service according to the obtained target data can be achieved in an existing manner, and no detailed discussion is made here.
[0074] In addition, it can also be ensured that when threads read data in the cache during the same interaction process, the data in the cache is always fixed and unchanged, that is, in the first state, the data in the first cache corresponding to the first state is always unchanged; in the second state, the data in the second cache corresponding to the second state is always unchanged, so as to avoid the situation that the read results of the same data from the cache are inconsistent before and after during an interaction process.
[0075] In addition, after allocating a target cache identifier for a target thread, the target threads with the same target cache identifier can be stored in the same queue, that is, the queue corresponding to the target cache identifier. Subsequently, when obtaining data through the target thread, the target thread can be directly obtained from the queue corresponding to the target cache identifier, and the cache can be directly determined according to the target cache identifier corresponding to the queue, without having to determine the cache for each target thread separately, thus improving the efficiency of data acquisition.
[0076] After adopting the above solution, the business processing request corresponding to the target business can be obtained first, and at least one target thread can be allocated for the business processing request corresponding to the target business. Then, the same target cache identifier can be allocated for at least one target thread corresponding to the same business according to the system state and the association relationship between the target threads. The system state can be the first state or the second state. Data is obtained from the first cache in the first state and from the second cache in the second state, and different caches correspond to different cache identifiers. The first cache and the second cache are used to store the same data. The first state indicates that the data in the first cache has been updated according to the data update request, but the update of the data in the second cache has not been completed. The second state indicates that the data in the second cache has been updated according to the data update request. After allocating the same target cache identifier for at least one target thread corresponding to the same business, the target business can be implemented by obtaining target data from the cache corresponding to the target cache identifier through at least one target thread. By first allocating different caches for different system states and the data in the corresponding cache in this system state is fixed, and then allocating the cache identifier of the same cache for at least one target thread in an interaction process corresponding to a business processing request according to the system state and the association relationship between the threads, the data obtained by the threads corresponding to the same business in an interaction process is always consistent, avoiding the situation of inconsistent or non-corresponding data read by the threads due to data update, improving the accuracy of data acquisition and update, and thus ensuring the normal implementation of the business.
[0077] Based on Figure 2 the method, some specific implementation schemes of the method are also provided in the embodiments of this specification, which will be described below.
[0078] In another embodiment, the allocating the same target cache identifier for the at least one target thread according to the system state and the association relationship between the target threads may specifically include:
[0079] Determine the system state, and determine the target cache identifier according to the system state.
[0080] For each target thread, determine whether there is a cache identifier allocated to an associated thread.
[0081] If the target thread does not have a cache identifier allocated by an associated thread, allocate the target cache identifier for the target thread.
[0082] In this embodiment, after allocating at least one target thread for the service processing request of the target service, the system state can be determined first, and then the target cache identifier to be allocated can be determined according to the determined system state. In addition, after determining the system state, for each target thread, it can be first determined whether the target thread has a cache identifier allocated by an associated thread. Generally, the first target thread allocated for the service processing request of the target service is created by the operating system and has no associated thread, so there is no cache identifier allocated by a related thread. Subsequently, if a target thread does not complete the target service, a target thread can be continuously created to implement the target service. The subsequent target threads that continue to implement the target service can be associated threads of the first target thread, and the target thread and the associated thread can be associated by allocating the cache identifier of the associated thread to the target thread. Among them, the target thread can include the associated thread, that is, the associated thread is the first target thread.
[0083] Further, the determining the target cache identifier according to the system state may specifically include:
[0084] If the system state is the first state, determine that the target cache identifier is the cache identifier of the first cache corresponding to the first state.
[0085] If the system state is the second state, determine that the target cache identifier is the cache identifier of the second cache corresponding to the second state.
[0086] In this embodiment, after determining the system state, the cache for the data to be obtained can be determined according to the determined system state. Correspondingly, there can be two caches. One is the second cache corresponding to the normal operation of the system, and the system state is the second state at this time. The other is the first cache corresponding to the system being updated, and the system state is the first state at this time. If it is determined that the system state is the first state, it indicates that data needs to be obtained from the first cache. Therefore, the target cache identifier can be determined as the cache identifier of the first cache. If it is determined that the system state is the second state, it indicates that data needs to be obtained from the second cache. Therefore, the target cache identifier can be determined as the cache identifier of the second cache.
[0087] In another embodiment, the method may further include:
[0088] If the target thread has a cache identifier allocated by an associated thread, determine the cache identifier allocated by the associated thread as the target cache identifier.
[0089] In this embodiment, if the target thread has a cache identifier allocated by an associated thread, it indicates that the target thread has an associated thread. Then, the cache identifier of the associated thread can be directly obtained, and the cache identifier allocated by the associated thread is determined as the target cache identifier. That is, both the target thread and its associated thread can obtain data from the cache corresponding to the same cache identifier, ensuring data consistency.
[0090] In addition, there are two possible relationships between the target thread and the associated thread. One is that the target thread is created by the associated thread, that is, the associated thread is the parent thread of the target thread, and the target thread is the child thread of the associated thread. The other is that the target thread is a thread allocated by the thread pool, and both the target thread and the associated thread belong to the same interaction process. That is, during the execution of the target service by the associated thread, a new target thread needs to be allocated by the thread pool to complete the entire target service.
[0091] Further, in one implementation, the cache identifier is stored in the inheritable variable of the thread. Allocating at least one target thread for the service processing request corresponding to the target service includes:
[0092] Creating a child thread of the associated thread to obtain at least one initial target thread.
[0093] Setting the inheritable thread variable of the at least one initial target thread to the inheritable thread variable of the associated thread to obtain at least one target thread.
[0094] Allocating at least one target thread for the service processing request corresponding to the target service.
[0095] Specifically, when creating the child thread of the associated thread, the inheritable thread variable of the child thread (i.e., the newly created target thread) can be directly assigned a value. Therefore, the child thread can also obtain the thread variable of the parent thread and keep the values of the inheritable variables of the parent and child threads consistent. Subsequently, if the target thread has a cache identifier allocated by the associated thread, the cache identifier allocated by the associated thread can be directly determined as the target cache identifier.
[0096] Exemplarily, if the target service is not processed within one thread, a new thread needs to be created to continue the processing. In this case, there will be a situation of creating a child thread or allocating a thread from a thread pool. If a child thread is created (i.e., there is an inheritance relationship between threads), when creating the child thread (i.e., the target thread), the parent thread (i.e., the associated thread) needs to first determine whether it has an inheritable thread variable InheritableThreadLocal. If it exists, when creating the child thread, the inheritable thread variable of the child thread is assigned, so that the child thread can also obtain the thread variable of the parent thread, thereby maintaining the consistency of the values of the parent and child threads. Correspondingly, the cache identifier can be placed in the inheritable thread variable of the parent thread, so that between threads with an inheritance relationship, this identifier can be passed continuously and ensure that the cache identifiers of the threads within the interaction are consistent. For example, assume that a child thread T1 is created in the associated thread T, then the cache identifier of the associated thread T will be passed to the child thread T1:
[0097] The inheritable thread variable InheritableThreadLocal of thread T = {readCacheFlag = A};
[0098] The inheritable thread variable InheritableThreadLocal of child thread T1 = {readCacheFlag = A}.
[0099] Furthermore, in another implementation, the cache identifier is stored in the custom attribute information of the thread. Allocating at least one target thread for the service processing request corresponding to the target service includes:
[0100] Obtaining at least one initial target thread from the thread pool.
[0101] Setting the custom attribute information of the at least one initial target thread to the custom attribute information of the associated thread to obtain at least one target thread.
[0102] Allocating at least one target thread for the service processing request corresponding to the target service.
[0103] Specifically, if the target thread is a thread allocated from a thread pool, since there is no inheritance relationship between the threads allocated from the thread pool, the inheritance relationship cannot be passed through the characteristic of the inheritable variable InheritableThreadLocal to ensure the consistency of the cache identifiers of the parent and child threads. To solve this problem, a new thread decoration class RunnableWrapper can be created to add a custom attribute information to the decorated thread object and save the cache identifier of the associated thread through this custom attribute information.
[0104] Exemplarily, since there is no inheritance relationship among the threads allocated by the thread pool, it is impossible to pass the inheritance relationship through the characteristics of the inheritable variable InheritableThreadLocal to ensure the consistency of the cache identifiers of the parent and child threads. To solve this problem, a new thread decoration class RunnableWrapper can be created to add a custom attribute information to the decorated thread object to save the cache identifier of the associated thread. When using the RunnableWrapper class, although the problem of passing values between threads without inheritance relationship in the thread pool can be solved, this forcibly requires the user to use the RunnableWrapper class when creating threads, which has a great invasiveness to the user. Therefore, the function of the thread pool can also be enhanced. The user still uses the native Runnable class when creating threads. When the thread is submitted to the thread pool, the Runnable class can be automatically converted into the RunnableWrapper class, achieving non-invasive to the user. For example, when a new thread Y needs to be created in thread T, the thread pool allocates thread Y_Wrapper for it:
[0105] The inheritable thread variable InheritableThreadLocal of thread T = {readCacheFlag = A};
[0106] The Y_Wrapper object {parentReadCacheFlag = A; runnable = thread Y}.
[0107] In summary, by allocating target cache identifiers to threads in different ways, automatic coloring of threads is achieved, and it is ensured that the target cache identifiers allocated to threads in the same interaction process are the same. Furthermore, the data obtained by threads in the same interaction process is the same, ensuring the consistency and accuracy of data reading.
[0108] In addition, after the at least one target thread obtains the target data from the cache corresponding to the cache identifier, the following is further included:
[0109] Clear the target cache identifier of the at least one target thread.
[0110] In summary, by clearing the target cache identifier of the target thread, the target thread can be directly recycled, increasing the recycling efficiency of the target thread, and thus saving the computing resources of the device.
[0111] Figure 3 The flow diagram of the data processing method in the cache provided by another embodiment of the present application is as Figure 3As shown, in this embodiment, a service processing request for a target service may be received first, and then a target thread may be created for the service processing request of the target service. After the creation is completed, the thread execution starts. Cache decision-making may be performed to determine a target cache identifier. If the target cache identifier is the cache identifier of the first cache corresponding to the first state, the inheritable variables of the target thread may be set according to the target cache identifier (the cache identifier of the first cache). Similarly, if the target cache identifier is the cache identifier of the second cache corresponding to the second state, the inheritable variables of the target thread may also be set according to the target cache identifier (the cache identifier of the second cache), and the target thread may be added to the corresponding cache reading queue. Then, the target service may be executed by the threads in the cache queue. The parameter identifier to be read and the target cache identifier may be determined through the service processing request, and then the target parameter corresponding to the parameter identifier may be obtained from the corresponding cache queue according to the target cache identifier. Then, it is determined whether other threads need to continue the execution. If so, a new target thread is allocated, and at the same time, the cache identifier of the associated thread is allocated to the new target thread, and then the new target thread is allocated to continue the foregoing process. At the same time, the previous target thread (i.e., the associated thread of the newly allocated target thread) is deleted from the corresponding cache queue, and the inheritable thread variables of this thread are cleared. If other threads do not need to continue the execution, the previous target thread (i.e., the associated thread of the newly allocated target thread) is also deleted from the corresponding cache queue, and the inheritable thread variables of this thread are cleared, thereby implementing the target service.
[0112] In this embodiment, when the operating system starts, the global cache block GlobalThreadFlagCache may be initialized first. At this time:
[0113] The system status machineStatus = NORMAL (normal).
[0114] The cache reading queue A cacheReadQueueA is empty.
[0115] The cache reading queue B cacheReadQueueB is empty.
[0116] Then, the parameters may be loaded into cache A and cache B. The user can customize and load the desired parameters into cache A and cache B. It may be assumed that a parameter key-value pair port = 8888 is loaded. At this time:
[0117] Cache A cacheA = {port = 8888}.
[0118] Cache B cacheB = {port = 8888}.
[0119] After the target service is started, the application system can receive a service processing request at this time. If it is the first target thread to start processing, the target thread is created by the application system and is assumed to be thread T at this time. In addition, if the target service is not processed completely within a target thread, a new thread needs to be created to continue the processing. In this case, there will be a situation where a child thread of the target thread is created or a thread is allocated from a thread pool.
[0120] Optionally, if a child thread is created, the parent thread needs to determine whether it has an inheritable thread variable InheritableThreadLocal. If it exists, when creating the child thread, the inheritable thread variable of the child thread is assigned, so that the child thread can also obtain the thread variable of the parent thread and keep the values of the parent and child threads consistent. Then, the cache identifier can be placed into the inheritable thread variable of the thread, so that this identifier can be passed between threads with an inheritance relationship and ensure that the cache identifiers of the threads within a transaction are consistent.
[0121] Assume that a child thread T1 is created in thread T, then the cache reading identifier of thread T will be passed to child thread T1:
[0122] The inheritable thread variable InheritableThreadLocal of thread T = {readCacheFlag = A}.
[0123] The inheritable thread variable InheritableThreadLocal of child thread T1 = {readCacheFlag = A}.
[0124] Optionally, in the case of thread allocation from a thread pool, since there is no inheritance relationship between the threads allocated by the thread pool, the inheritance relationship cannot be passed through the characteristics of the inheritable variable InheritableThreadLocal to ensure the consistency of the cache identifiers of the parent and child threads. To solve this problem, a new thread decoration class RunnableWrapper can be created to add an attribute to the decorated thread object to save the cache reading identifier of the associated thread. In addition, although using the RunnableWrapper class can solve the problem of passing values between threads without an inheritance relationship in the thread pool, this requires the user to use the RunnableWrapper class when creating a thread, which is highly invasive to the user. Therefore, the function of the thread pool can be enhanced. The user still uses the native Runnable class when creating a thread. When the thread is submitted to the thread pool, we automatically convert the Runnable class to the RunnableWrapper class to achieve non-invasiveness to the user. Exemplarily, assume that a new thread Y needs to be created in thread T, and the thread pool allocates thread Y_Wrapper for it:
[0125] The inheritable thread variable InheritableThreadLocal of thread T = {readCacheFlag = A}.
[0126] The Y_Wrapper object {parentReadCacheFlag = A; runnable = thread Y}.
[0127] Then it can enter the thread execution start process, which can be described in two cases: the created target thread and the allocated target thread.
[0128] Optionally, for the created target thread, automatic coloring and fading can be performed on the created thread. Exemplarily, the AOP (Aspect Oriented Programming) technology can be used. Before the target thread executes the target business, the processing logic for allocating the target cache identifier to the target thread can be automatically entered. After the execution of the target business, the processing logic for recycling the target thread can be automatically entered. The specific processing process can be: define a pointcut to enhance the functions of all classes that implement the java.lang.Runnable.run() method, that is, the process of executing the target business. Then the pointcut around method can be defined to perform enhancement processing before and after the execution of the target business. Then static compilation can be performed to automatically add the enhanced processing code to the execution code of all target threads without additional processing by the user.
[0129] When allocating the target thread, in the RunnableWrapper class, the process of the target thread executing the target business can be enhanced. Before the target thread executes the target business, the processing logic for allocating the target cache identifier to the target thread is executed first; after the execution of the target business, the recycling process of the target thread is executed. Since the target threads allocated in the thread pool are all of the RunnableWrapper class, these target threads will automatically execute the enhanced code.
[0130] In addition, when making a cache decision, the judgeUseWhichCache method of GlobalThreadFlagCache can be called. The default implementation of this method is: determine the cache that the current target thread should use according to the current system state. If the system state is NOMAL (i.e., the second state), the cache identifier of the cache corresponding to the normal state is read (exemplarily, it can be A), otherwise the cache identifier of the cache corresponding to the updated state is read (exemplarily, it can be B).
[0131] Exemplarily, the system state machineStatus = NORMAL.
[0132] The cache read flag readCacheFlag = A.
[0133] In addition, when setting the inheritable thread variable of the target thread, the markThread method of GlobalThreadFlagCache can be called. Specifically, it can first determine whether there is an associated thread. If there is, use the cache read flag of the associated thread to assign a value to its own inheritable variable. If there is no associated thread, use the cache read flag determined in the previous step to assign a value to its own inheritable thread variable.
[0134] Exemplarily, if the target thread T is the first to enter the processing process of the target service, so the target thread T has no associated thread, then use the target cache flag determined in the previous step to assign a value to its own inheritable thread variable. That is, the inheritable thread variable InheritableThreadLocal of the target thread T = {readCacheFlag = A}.
[0135] If it is a child thread T1 created in the target thread T, because the child thread T1 and the target thread T have an inheritance relationship, so the child thread T1 directly uses the target cache flag of the target thread T to assign a value to its own inheritable thread variable. That is, the inheritable thread variable InheritableThreadLocal of the child thread T1 = {readCacheFlag = A}.
[0136] If it is the thread Y_Wrapper allocated by the thread pool for the target thread T, the thread Y_Wrapper can determine whether it has an associated thread according to its own parentReadCacheFlag attribute, so it can use the cache flag of the associated thread to assign a value to the inheritable thread variable of the thread Y modified by it. That is, the Y_Wrapper object {parentReadCacheFlag = A; runnable = thread Y}, and the inheritable thread variable InheritableThreadLocal of the thread Y = {readCacheFlag = A}.
[0137] In addition, the target thread can also be added to the corresponding cache read queue. Actually, the markThread method of GlobalThreadFlagCache can be called. If the target cache flag of the target thread is A, the target thread can be put into the cache queue A, otherwise it is put into the cache queue B. When switching caches subsequently, we can know the situation of thread cache usage in the system by checking whether the target thread still exists in the cache queue, and then determine whether the cache can be switched. At this time:
[0138] The cache read queue A cacheReadQueueA = [thread ID of thread T].
[0139] The cache read queue B, cacheReadQueueB, is empty.
[0140] After the foregoing process, automatic coloring of the target thread is achieved, and then the process of executing the target service can be entered. If parameter caching needs to be read during this process, the target cache identifier of the current thread can be judged first. If the target cache identifier is A, the parameter is read from cache A; otherwise, it is read from cache B. Assume that thread T needs to read the parameter cache port at this time:
[0141] Because the InheritableThreadLocal of thread T = {readCacheFlag = A}, we read the value of port from cache A, and the result returns 8888.
[0142] Then it can be judged whether other target threads are still needed. If so, the inheritable thread variable of the target thread can be used to assign values to the new target thread according to the foregoing. Then the clearThreadFlag method of GlobalThreadFlagCache can be called to delete the target thread from the corresponding cache queue, indicating that the target thread no longer reads the corresponding cache. At this time:
[0143] The cache read queue A, cacheReadQueueA, is empty.
[0144] The cache read queue B, cacheReadQueueB, is empty.
[0145] In addition, the clearThreadFlag method of GlobalThreadFlagCache can also be called to set the inheritable thread variable of the target thread to null. Therefore, the created threads will be recycled by the system and will not be used again. However, the threads allocated by the thread pool will not be recycled and may be reused. To avoid parameter reading errors caused by the cache read identifier assigned last time still being used when the thread is reused again, it is necessary to set the inheritable thread variable of the thread to null in this step. At this time:
[0146] The inheritable thread variable InheritableThreadLocal of thread T is null.
[0147] The inheritable thread variable InheritableThreadLocal of thread T1 is null.
[0148] The inheritable thread variable InheritableThreadLocal of thread Y is null.
[0149] In addition, if the target service does not require other threads to continue execution, it indicates that the target service has been executed and the process can be ended.
[0150] In summary, this application does not intrude into the business logic, enabling the application to achieve consistent read and repeatable read of parameter configuration without the user's awareness. Moreover, the application can safely modify cache parameters during runtime, and ensures that when the program uses parameters within an interactive process, there will be no problems of inconsistent parameter readings twice and incorrect associated parameters, improving the accuracy of data reading.
[0151] In another embodiment, the method may further include:
[0152] Receiving the data update request, where the data update request includes the data identifier to be updated and the corresponding data value to be updated.
[0153] Updating the data value corresponding to the data identifier to be updated in the first cache corresponding to the first state to the data value to be updated according to the data update request, and setting the system state to the first state.
[0154] Determining whether the thread assigned with the cache identifier corresponding to the second state has completed processing.
[0155] If the processing has been completed, updating the data value corresponding to the data identifier to be updated in the second cache corresponding to the second state to the data value to be updated.
[0156] Updating the system state to the second state.
[0157] In this embodiment, the data in the cache may be involved in an update situation. When updating the data in the cache, in order to avoid the situation that the data read from the cache by the thread is inconsistent or not corresponding before and after in the same interaction process, when receiving the data update request, the data value corresponding to the data identifier to be updated in the first cache corresponding to the first state can be updated to the data value to be updated first. So far, the data update of the first cache corresponding to the first state has been achieved, and then the system state can be set to the first state to ensure that the data in the corresponding cache is fixed in the current state. Then, it is also necessary to update the data in the second cache corresponding to the second state. Since it is necessary to ensure that the data read by the thread from the cache is consistent in the same interaction process, therefore, it is necessary to wait until the thread reading the second cache has completed reading the data before updating the data in the second cache corresponding to the second state. Specifically, it can be determined first whether the thread assigned with the cache identifier corresponding to the second state has completed processing. If the processing has been completed, the data value corresponding to the data identifier to be updated in the second cache corresponding to the second state can be updated to the data value to be updated, and the system state can be updated to the second state.
[0158] In addition, the method may further include:
[0159] If the processing is not completed, randomly wait for a target duration.
[0160] After waiting for the target duration, re-execute the steps of determining whether the thread assigned the cache identifier corresponding to the second state has completed processing and subsequent steps.
[0161] Specifically, only when all threads in the application system are reading the first cache can the next process of switching caches be carried out. Since after the system state is in the first state, new target threads will all read the first cache corresponding to the first state, it is possible to only determine whether the queue corresponding to the second cache is empty to know whether all threads are reading the first cache. If the thread assigned the cache identifier for reading the second cache corresponding to the second state has not completed processing, it is possible to randomly wait for the target duration and then re-judge after the target duration. Among them, the target duration can be any value from 0 to 60 seconds.
[0162] In summary, by waiting for the randomly set target duration, the success rate of cache switching can be improved, thereby ensuring the consistency of data reading.
[0163] Figure 4 For the flowchart of the cache switching process provided by the embodiments of the present application, as Figure 4 shown, in this embodiment, the data in the first cache corresponding to the first state can be updated first. After the data in the first cache is updated, the system state can be set to the first state, and then randomly wait for the target duration, and then determine whether the second cache corresponding to the second state can be updated. If it can, the second cache corresponding to the second state can be directly updated. Otherwise, re-wait for the randomly generated target duration and judge again until the second cache corresponding to the second state can be updated, and then directly update the second cache corresponding to the second state.
[0164] In summary, by updating the second cache after the thread reading the second cache has completed processing, the consistency of the cache data read by the threads in the same transaction process is ensured, and the accuracy of data reading is improved.
[0165] Based on the same idea, the embodiments of this specification also provide a device corresponding to the above method, Figure 5 For the structural schematic diagram of the data processing device in the cache provided by the embodiments of the present application, as Figure 5 shown, the device provided in this embodiment may include:
[0166] An acquisition module 501, configured to acquire a service processing request corresponding to a target service, and allocate at least one target thread for the service processing request corresponding to the target service;
[0167] A processing module 502, configured to allocate the same target cache identifier for the at least one target thread according to a system state and an association relationship between target threads, where the system state is a first state or a second state, data is acquired from a first cache in the first state, data is acquired from a second cache in the second state, different caches correspond to different cache identifiers, the first cache and the second cache are used to store the same data, the first state indicates that the data in the first cache has been updated according to a data update request, but the update of the data in the second cache has not been completed, and the second state indicates that the data in the second cache has been updated according to the data update request;
[0168] The processing module 502 is further configured to acquire target data from a cache corresponding to the target cache identifier through the at least one target thread, and implement the target service according to the target data.
[0169] In another embodiment, the processing module 502 is further configured to:
[0170] Determine a system state, and determine a target cache identifier according to the system state.
[0171] For each target thread, determine whether there is a cache identifier allocated by an associated thread for the target thread.
[0172] If the target thread does not have a cache identifier allocated by an associated thread, allocate the target cache identifier for the target thread.
[0173] In this embodiment, the processing module 502 is further configured to:
[0174] If the system state is the first state, determine that the target cache identifier is the cache identifier of the first cache corresponding to the first state.
[0175] If the system state is the second state, determine that the target cache identifier is the cache identifier of the second cache corresponding to the second state.
[0176] In this embodiment, the processing module 502 is further configured to:
[0177] If the target thread has a cache identifier allocated by an associated thread, determine the cache identifier allocated by the associated thread as the target cache identifier.
[0178] In this embodiment, for the processing module 502, where the cache identifier is stored in an inheritable variable of a thread, it is further configured to:
[0179] Create a child thread of the associated thread to obtain at least one initial target thread.
[0180] Set the inheritable thread variables of the at least one initial target thread to the inheritable thread variables of the associated thread to obtain at least one target thread.
[0181] Allocate at least one target thread for the service processing request corresponding to the target service.
[0182] In this embodiment, the processing module 502, where the cache identifier is stored in the custom attribute information of the thread, is further configured to:
[0183] Obtain at least one initial target thread from the thread pool.
[0184] Set the custom attribute information of the at least one initial target thread to the custom attribute information of the associated thread to obtain at least one target thread.
[0185] Allocate at least one target thread for the service processing request corresponding to the target service.
[0186] In this embodiment, the processing module 502 is further configured to:
[0187] Clear the target cache identifier of the at least one target thread.
[0188] In another embodiment, the processing module 502 is further configured to:
[0189] Receive a data update request, where the data update request includes an identifier of data to be updated and a corresponding value of the data to be updated.
[0190] According to the data update request, update the data value corresponding to the identifier of the data to be updated in the first cache corresponding to the first state to the value of the data to be updated, and set the system state to the first state.
[0191] Determine whether the thread that has been allocated the cache identifier corresponding to the second state has finished processing.
[0192] If it has finished processing, update the data value corresponding to the identifier of the data to be updated in the second cache corresponding to the second state to the value of the data to be updated.
[0193] Update the system state to the second state.
[0194] In this embodiment, the processing module 502 is further configured to:
[0195] If it has not finished processing, randomly wait for a target duration.
[0196] After waiting for the target duration, re - execute the steps of determining whether the thread that has been assigned the cache identifier corresponding to the second state has been processed and subsequent steps.
[0197] The device provided by the embodiments of the present application can implement the method of the above - mentioned Figure 2 shown embodiments. The implementation principle and technical effects are similar and will not be elaborated here.
[0198] Figure 6 is a schematic hardware structure diagram of the electronic device provided by the embodiments of the present application. As Figure 6 shown, the device 600 provided in this embodiment includes: a processor 601, and a memory communicatively connected to the processor. Among them, the processor 601 and the memory 602 are connected through a bus 603.
[0199] In a specific implementation process, the processor 601 executes the computer - executable instructions stored in the memory 602, so that the processor 601 executes the method in the above - mentioned method embodiments.
[0200] The specific implementation process of the processor 601 can be referred to in the above - mentioned method embodiments. The implementation principle and technical effects are similar and will not be elaborated here in this embodiment.
[0201] In the above - mentioned Figure 6 shown embodiments, it should be understood that the processor may be a central processing unit (English: Central Processing Unit, abbreviated: CPU), or may also be other general - purpose processors, digital signal processors (English: Digital Signal Processor, abbreviated: DSP), application - specific integrated circuits (English: Application Specific Integrated Circuit, abbreviated: ASIC), etc. The general - purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in combination with the invention can be directly embodied as being executed by a hardware processor, or executed by a combination of hardware and software modules in the processor.
[0202] The memory may include high - speed RAM memory, and may also include non - volatile storage NVM, such as at least one disk memory.
[0203] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For the sake of convenience of representation, the buses in the drawings of the present application are not limited to only one bus or one type of bus.
[0204] An embodiment of the present application further provides a computer-readable storage medium, in which computer-executable instructions are stored. When the processor executes the computer-executable instructions, the data processing method in the cache of the above method embodiment is implemented.
[0205] An embodiment of the present application further provides a computer program product, including a computer program. When the computer program is executed by a processor, the data processing method in the cache as described above is implemented.
[0206] The above computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, a magnetic disk or an optical disc. The readable storage medium can be any available medium accessible by a general-purpose or special-purpose computer.
[0207] An exemplary readable storage medium is coupled to the processor, so that the processor can read information from the readable storage medium and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can be located in an Application Specific Integrated Circuits (ASIC). Of course, the processor and the readable storage medium can also exist as discrete components in a device.
[0208] Those of ordinary skill in the art can understand that all or part of the steps of implementing the above method embodiments can be completed by hardware related to program instructions. The foregoing program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above method embodiments are executed; and the foregoing storage medium includes various media that can store program codes, such as ROM, RAM, magnetic disks, or optical discs.
[0209] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than limiting them; although the present application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements on some or all of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the various embodiments of the present application.
Claims
1. A method for data processing in a cache, characterized in that, it includes: Obtain a service processing request corresponding to a target service, and allocate multiple target threads for the service processing request corresponding to the target service; Allocate a target cache identifier corresponding to the same system state for the multiple target threads according to the association relationship between the target threads, where the same system state is the system state determined when the cache identifier is allocated for the first target thread; wherein, the system state is a first state or a second state, data is obtained from a first cache in the first state, and data is obtained from a second cache in the second state, and different caches correspond to different cache identifiers, the first cache and the second cache are used to store the same data, the first state indicates that the data in the first cache has been updated according to a data update request, but the update of the data in the second cache has not been completed, and the second state indicates that the data in the second cache has been updated according to the data update request; Obtain target data from the cache corresponding to the target cache identifier through the multiple target threads, and implement the target service according to the target data.
2. The method according to claim 1, characterized in that, The step of allocating a target cache identifier corresponding to the same system state for the multiple target threads according to the association relationship between the target threads includes: Determine the system state, and determine the target cache identifier according to the system state; For each target thread, determine whether there is a cache identifier allocated by an associated thread for the target thread; If the target thread does not have a cache identifier allocated by an associated thread, allocate the target cache identifier for the target thread.
3. The method according to claim 2, characterized in that, The step of determining the target cache identifier according to the system state includes: If the system state is the first state, determine that the target cache identifier is the cache identifier of the first cache corresponding to the first state; If the system state is the second state, determine that the target cache identifier is the cache identifier of the second cache corresponding to the second state.
4. The method according to claim 2, characterized in that, It further includes: If the target thread has a cache identifier allocated by an associated thread, determine the cache identifier allocated by the associated thread as the target cache identifier.
5. The method according to claim 4, characterized in that, The cache identifier is stored in the inheritable variable of the thread, and the step of allocating multiple target threads for the service processing request corresponding to the target service includes: Create a child thread of the associated thread to obtain at least one initial target thread; Set the inheritable thread variable of the at least one initial target thread to the inheritable thread variable of the associated thread to obtain at least one target thread; Allocate at least one target thread for the service processing request corresponding to the target service.
6. The method according to claim 4, characterized in that, The cache identifier is stored in the custom attribute information of the thread, and the step of allocating multiple target threads for the service processing request corresponding to the target service includes: Obtain at least one initial target thread from a thread pool; Set the custom attribute information of the at least one initial target thread to the custom attribute information of the associated thread to obtain at least one target thread; Allocate at least one target thread for the service processing request corresponding to the target service.
7. The method according to any one of claims 1-6, characterized in that, further comprising: Receive the data update request, wherein the data update request includes the data identifier to be updated and the corresponding data value to be updated; Update the data value corresponding to the data identifier to be updated in the first cache corresponding to the first state to the data value to be updated according to the data update request, and set the system state to the first state; Determine whether the thread that has been allocated the cache identifier corresponding to the second state has completed processing; If the processing has been completed, update the data value corresponding to the data identifier to be updated in the second cache corresponding to the second state to the data value to be updated; Update the system state to the second state.
8. The method according to claim 7, characterized in that, further comprising: If the processing has not been completed, randomly wait for a target duration; After waiting for the target duration, re-execute the steps of determining whether the thread that has been allocated the cache identifier corresponding to the second state has completed processing and subsequent steps.
9. The method according to any one of claims 1-6, characterized in that, After obtaining the target data from the cache corresponding to the target cache identifier through the multiple target threads, further comprising: Clear the target cache identifiers of the multiple target threads.
10. A data processing device in a cache, characterized in that, comprising: An acquisition module, configured to acquire a service processing request corresponding to a target service, and allocate a plurality of target threads for the service processing request corresponding to the target service; A processing module, configured to allocate a target cache identifier corresponding to the same system state for the plurality of target threads according to the association relationship between the target threads, where the same system state is the system state determined when the cache identifier is allocated for the first target thread; wherein, the system state is the first state or the second state, when in the first state, data is acquired from the first cache, when in the second state, data is acquired from the second cache, and different caches correspond to different cache identifiers, the first cache and the second cache are used to store the same data, the first state indicates that the data in the first cache has been updated according to the data update request, but the update of the data in the second cache has not been completed, and the second state indicates that the data in the second cache has been updated according to the data update request; The processing module is further configured to obtain target data from the cache corresponding to the target cache identifier through the plurality of target threads, and implement the target service according to the target data.
11. An electronic device, characterized in that, comprising a processor and a memory communicatively connected to the processor; the processor executes the computer execution instructions stored in the memory, so that the processor executes the method according to any one of claims 1-9.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, and when the processor executes the computer-executable instructions, the method described in any one of claims 1-9 is implemented.
13. A computer program product, wherein, it includes a computer program, and when the computer program is executed by a processor, the method described in any one of claims 1-9 is implemented.
Citation Information
Patent Citations
Cache system and cache access method
JP2011086230A