Cache cleaning method and device based on order state machine, equipment and medium

When data modification occurs in the database, the cache key is determined based on the business logic and the target data field value, which solves the problem of cache cleaning in the existing technology that takes a long time and is easy to miss, and achieves efficient and accurate cache data cleaning.

CN119884187APending Publication Date: 2025-04-25XING LIN JU (BEI JING) KE JI YOU XIAN GONG SI
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411943198.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-26
Publication Date
2025-04-25

AI Technical Summary

Technical Problem

In the prior art, decoupling between business logic and cache cleaning cannot be achieved, resulting in cache cleaning taking a long time and easy to miss, especially in the airport scenario of order status.

Method used

When data modification occurs in the database, the change identification is determined according to the business logic of the data modification operation, and combined with the field value of the preset field under the target data corresponding to the modification operation, the cache key corresponding to the cache data that needs to be cleaned is determined, thereby achieving efficient cleaning of cache data.

Benefits of technology

It realizes decoupling between business logic and cache cleaning, and does not need to sort out all cached data every time the database changes, ensuring the efficiency of cached data cleaning and avoiding omissions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119884187A_ABST
    Figure CN119884187A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of data caching, and discloses a cache cleaning method, device and equipment based on an order state machine, and a medium, the method comprises the following steps: when a data modification operation occurs in a database, determining a first change identifier corresponding to the data modification operation, and according to the state machine, determining a first change identifier corresponding to the data modification operation under target data, presetting a first field value of the data field, searching a first cache key from the cache key set in combination with the first change identifier, and cleaning cache data corresponding to the first cache key; wherein the cache key in the cache key set is generated based on the change identifier determined based on the cache mapping relation and the cache type of the cache data and the field value of the data field when the cache data is generated. The cache can be cleaned in a targeted manner only by determining the change identifier corresponding to the change data and the value of the preset data field, so that decoupling between service logic and cache cleaning is realized, and the cache cleaning efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data cache technology, and in particular to a cache cleaning method, device, equipment and medium based on an order state machine. Background Art

[0002] When querying and using cache, if you use the solution of setting cache during query and deleting cache during update, you need to clean up the cache when updating data. Taking the e-commerce field as an example, a common cache cleaning solution is that when doing data update operations, you need to pay attention to which data changes are involved in this data update operation. On this basis, sort out which caches in the current business use this data. After the data update operation is processed, it is hard-coded to delete the caches that use this data.

[0003] In this process, the business logic and cache cleaning cannot be decoupled. Every time the data in the database is changed, all caches need to be combed according to the changed data. At the same time, the order state machine scenario involves data changes in various scenarios, which makes cache cleaning time-consuming, time-consuming and easy to miss. Summary of the invention

[0004] In view of this, the present invention provides a cache cleaning method, device, equipment and medium based on an order state machine to solve the problem that cache cleaning is time-consuming, time-consuming and easy to be missed due to the inability to decouple business logic and cache cleaning.

[0005] In a first aspect, the present invention provides a cache cleaning method based on an order state machine, the method comprising:

[0006] Monitor data modification operations in the order database;

[0007] When a data modification operation occurs in the database, a corresponding first change identifier is determined based on the business logic of the data modification operation, and a first field value of a preset data field under the target data corresponding to the data modification operation is determined according to the context information of the state machine;

[0008] Based on the first change identifier and the first field value, the first cache key is searched from the cache key set, and the cache data corresponding to the first cache key is cleared; wherein the cache keys in the cache key set are generated when the cache data is generated based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier corresponding to the cache data and the target data field are determined based on the cache mapping relationship and the cache type of the cache data.

[0009] The method provided by the present aspect determines the change identifier according to the business logic of the data modification operation when data modification occurs in the database, and determines the cache key corresponding to the cache data that needs to be cleared in combination with the field value of the preset field under the target data corresponding to the modification operation, wherein the cache key is generated when the cache data is generated based on the change identifier and the target data field determined by the cache type and cache mapping relationship of the cache data, and the field value of the target data field in the cache data, that is, when the cache data is generated, the naming of its cache key is set according to the preset rules, so that in the scenario where the cache data needs to be cleared subsequently, it is only necessary to determine the business logic of the modification operation and the field value of a specific field in the modification data to determine the cache key to be cleared and clear the corresponding cache data, thereby realizing the decoupling between business logic and cache clearing, and eliminating the need to comb through all cache data every time the database changes, thereby ensuring the efficiency of cache data clearing.

[0010] In an optional embodiment, the method further includes:

[0011] Monitor the data addition operations in the order database;

[0012] When a data adding operation occurs in the database, a second change identifier is determined based on the business logic of the data adding operation;

[0013] Obtaining a second field value of a preset data field according to the newly added data corresponding to the data adding operation;

[0014] Based on the second change identifier and the second field value, the second cache key is searched from the cache key set, and the cache data corresponding to the second cache key is cleared.

[0015] In this embodiment, when a data addition operation occurs in the database, the second change identifier is determined based on the business logic of the data addition operation, and the second field value corresponding to the preset data field in the newly added data is directly determined, so as to find the corresponding cache key and clean up the cached data, thereby ensuring the comprehensiveness of the cached data cleaning and avoiding omissions when cleaning the cached data.

[0016] In an optional implementation manner, the change identifier corresponding to the cache data and the target data field are determined as follows:

[0017] When generating cache data, determining the cache type of the cache data, the cache type including: order information cache and user order statistics cache;

[0018] If the cache type of the cache data is order information cache, the change identifier corresponding to the cache data is determined as a modified order, and the target data field corresponding to the cache data is determined as an order id;

[0019] If the cache type of the cache data is user order statistics cache, the change identifier corresponding to the cache data is determined as modified order and / or new order, and the target data field corresponding to the cache data is determined as user id.

[0020] This embodiment divides the cache types into two types: order information cache and order statistics cache. For the order information cache, it is necessary to determine its corresponding order id and then generate a corresponding cache key, so that when the corresponding order information changes in the subsequent database, the cache key can be targeted and the cache data can be cleaned up; for the user order statistics cache, it is necessary to determine its corresponding user id and then generate a cache key, so that when the order data of the corresponding user changes in the database, the cache key can be targeted and the cache data can be cleaned up, thereby ensuring targeted cleaning of the cache data.

[0021] In an optional implementation, the cache keys in the cache key set are generated in the following manner:

[0022] Determine a corresponding cache key generation formula according to the cache type of the cache data;

[0023] After determining the change identifier and the target data field corresponding to the cached data, determining the field value of the cached data relative to the target data field;

[0024] The change identifier corresponding to the cached data is written into the corresponding cache key generation formula relative to the field value of the target data field to obtain the cache key of the cached data.

[0025] In this embodiment, when generating a cache key for cache data, field values ​​and change identifiers are filled in based on the cache key generation formula corresponding to each cache type, so that the cache key is generated efficiently and accurately, ensuring the stability of subsequent cache data cleaning.

[0026] In an optional implementation, the context information in the order state machine is generated as follows:

[0027] When a user initiates a data modification operation on a database, the order state machine searches the database for target modification data corresponding to the data modification operation;

[0028] The field values ​​corresponding to the order id and the user id are determined in the target modification data, and the field values ​​corresponding to the order id and the user id are filled into the context of the order state machine.

[0029] In this embodiment, when a user modifies data in a database, the order state machine is used to search for the field values ​​corresponding to the order id and user id corresponding to the target modified data, and fills them into the context, thereby ensuring the accuracy of the first field value subsequently determined from the context information in the order state machine.

[0030] In an optional implementation manner, the cache type of the cached data is determined as follows:

[0031] When the user queries the target order information in the database, first cache data corresponding to the target order information is generated in the cache layer, and the cache type of the first cache data is determined as order information cache;

[0032] When a user queries order statistics information in a database, second cache data corresponding to the order statistics information is generated in a cache layer, and a cache type of the second cache data is determined as a user order statistics cache.

[0033] In this embodiment, when a user queries the target order information in the database, the cache data generated is determined as the order information cache. When the user queries the order statistics information in the database, the cache generated is determined as the user order statistics cache to ensure the accuracy of the subsequently generated cache keys. When the database is subsequently changed, the corresponding cache keys can be accurately queried and the cache data can be cleared.

[0034] In an optional implementation, the preset data fields include: order id and user id;

[0035] Determining the first field value of the preset data field under the target data corresponding to the current data modification operation according to the context information of the state machine includes:

[0036] Determine the field values ​​of the order id and user id of the target modification data corresponding to the current data modification operation in the context information of the state machine as the first field value;

[0037] The obtaining, according to the newly added data corresponding to the data adding operation, a second field value of a preset data field comprises:

[0038] The field values ​​of the order id and the user id in the newly added data are determined as the second field values.

[0039] This implementation, by including order id and user id in the preset data fields, can ensure that in the scenario corresponding to the order database, most cached data can generate targeted cache keys based on these two field values, thereby ensuring the comprehensiveness of the application.

[0040] In a second aspect, the present invention provides a cache cleaning device based on an order state machine, the device comprising:

[0041] Database monitoring module, used to monitor data modification operations in the order database;

[0042] An information determination module, used for determining a corresponding first change identifier based on the business logic of the data modification operation when a data modification operation occurs in the database, and determining a first field value of a preset data field under the target data corresponding to the data modification operation according to the context information of the state machine;

[0043] A cache key query module is used to search for a first cache key from a cache key set based on the first change identifier and the first field value, and to clean up cache data corresponding to the first cache key; wherein the cache keys in the cache key set are generated when generating cache data based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier corresponding to the cache data and the target data field are determined based on a cache mapping relationship and a cache type of the cache data.

[0044] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the cache clearing method based on an order state machine according to the first aspect or any corresponding embodiment thereof by executing the computer instructions.

[0045] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the cache clearing method based on an order state machine according to the first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0047] Figure 1 is a flow chart of a cache cleaning method based on an order state machine according to an embodiment of the present invention;

[0048] Figure 2 is a flow chart of another cache cleaning method based on an order state machine according to an embodiment of the present invention;

[0049] Figure 3 is an example diagram of a process for implementing cache cleaning according to an embodiment of the present invention;

[0050] Figure 4 is a structural block diagram of a cache cleaning device based on an order state machine according to an embodiment of the present invention;

[0051] Figure 5 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0052] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0053] When querying and using cache, if you use the solution of setting cache during query and deleting cache during update, you need to clean up the cache when updating data. Taking the e-commerce field as an example, a common cache cleaning solution is that when doing data update operations, you need to pay attention to which data changes are involved in this data update operation. On this basis, sort out which caches in the current business use this data. After the data update operation is processed, it is hard-coded to delete the caches that use this data.

[0054] In this process, the business logic and cache cleaning cannot be decoupled. Every time the data in the database is changed, all caches need to be combed according to the changed data. At the same time, the order state machine scenario involves data changes in various scenarios, which makes cache cleaning time-consuming, time-consuming and easy to miss.

[0055] To this end, an embodiment of the present invention provides a cache cleaning method based on an order state machine. When data modification occurs in the database, a change identifier is determined according to the business logic of the data modification operation, and at the same time, the cache key corresponding to the cache data to be cleaned is determined in combination with the field value of the preset field under the target data corresponding to the modification operation. The cache key is generated when the cache data is generated based on the change identifier and the target data field determined by the cache type and cache mapping relationship of the cache data, as well as the field value for the target data field in the cache data. That is, when the cache data is generated, the naming of its cache key is set according to the preset rules. Therefore, in the scenario where the cache data needs to be cleaned up later, it is only necessary to determine the business logic of the modification operation and the field value of a specific field in the modification data to determine the cache key to be cleaned up and clean up the corresponding cache data, thereby realizing the decoupling between business logic and cache cleaning. There is no need to comb through all cache data every time the database changes, thereby ensuring the cleaning efficiency of the cache data.

[0056] According to an embodiment of the present invention, a cache cleaning method embodiment based on an order state machine is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0057] In this embodiment, a cache cleaning method based on an order state machine is provided, which can be used for the above-mentioned cache cleaning. Figure 1 is a flow chart of a cache cleaning method based on an order state machine according to an embodiment of the present invention. Figure 1 As shown, the process includes the following steps:

[0058] Step S101, monitoring data updates in the order database.

[0059] For e-commerce scenarios, the order database is the main database in this application scenario. When users add, delete, or modify orders or services, the data in the order database will be updated. In this case, the cached data in the cache layer needs to be adaptively deleted. Therefore, the order database needs to be monitored so that the cached data can be cleaned up in a timely and targeted manner when the data in the database is updated.

[0060] Step S102, when a data modification operation occurs in the database, a corresponding first change identifier is determined based on the business logic of the data modification operation, and a first field value of a preset data field under target data corresponding to the data modification operation is determined according to the context information of the state machine.

[0061] In the process of monitoring data updates in the order database, if a data modification operation occurs in the database, such as modifying order information, deleting order data, modifying service information, etc., it is necessary to determine the corresponding first change identifier according to the specific business logic of the data modification operation, that is, what type of data modification occurred in this data modification operation.

[0062] For example, assuming that an order id in the database is 1001, when the order 1001 in the database has information modification, such as modification of address, product information, etc., then it can be determined that the first change identifier corresponding to this data modification operation is: modify order. The specific change identifier for the data in the database can be determined according to the specific business logic corresponding to the modification operation.

[0063] Exemplarily, the types of pre-maintained data change identifiers may include: adding new orders, modifying orders, adding new services, modifying services, etc. For an e-commerce order system, since users need to view historical orders, the data corresponding to each order in the order database is usually not deleted, that is, based on e-commerce business, user operations will not involve deleting orders from the order database.

[0064] However, out of consideration for special circumstances, in some cases, the change identifier of deleting an order can be pre-set. When it is detected that the data modification operation occurring in the database is deleting an order, the first change identifier of the corresponding data modification operation can be determined as deleting the order.

[0065] For the order state machine, when it receives a data processing request for order status change initiated by the user through the front-end application, this request will first be received by the server interface where the order state machine is located. The order state machine service will parse the received request and extract key information, such as the order number, the specific operation type of the request (payment, delivery, order cancellation, or other operations), etc. According to the order number in the request, the current state of the order is queried from the order table in the database, that is, the state before the target data is processed, and loaded into the context of the order state machine to respond to the user's specific data processing request.

[0066] The preset data fields may be understood as multiple data fields that are pre-set according to the actual application scenario, which may include, for example, user ID, order ID, service ID, etc., and may be pre-set according to actual needs.

[0067] Since the state before target data processing is filled in the context information of the state machine, the field values ​​corresponding to each preset data field before target data processing can be determined in the context information, that is, the specific values ​​corresponding to fields such as user ID and order ID corresponding to the target data.

[0068] For example, assuming that the preset data fields are user id and order id, when the user modifies the information under an order A in the database, the data corresponding to order A is the target data, and the relevant information before the modification will be recorded in the context information of the state machine, that is, the field value corresponding to the order id of order A and the field value corresponding to the user id. These two field values ​​are the first field values.

[0069] Step S103: based on the first change identifier and the first field value, searching for the first cache key from the cache key set, and clearing the cache data corresponding to the first cache key.

[0070] Among them, the cache key in the cache key set is generated when generating cache data based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier and target data field corresponding to the cache data are determined based on the cache mapping relationship and the cache type of the cache data.

[0071] It can be understood that, for a certain data modification operation on the database, according to the relevant content in the above step S102, the first change identifier corresponding to this data modification operation and the first field value of the preset data field under the corresponding modified target data can be determined. After obtaining the first change identifier and the first field value, it can be determined which cache data to be cleared for this data modification operation. Before clearing the cached data, it is first necessary to find the corresponding cache key, which can be understood as the label of the cached data.

[0072] For the cache key, it can be understood that when generating cache data, it is necessary to determine the data change identifier and data field corresponding to the cache data, that is, the target data field, according to the cache type of the cache data and the pre-configured cache mapping relationship, where the cache can be pre-configured according to actual business needs.

[0073] In the pre-configured cache mapping relationship, the change identifiers and target data fields corresponding to multiple cache types may be enumerated.

[0074] For example, according to the business needs of the e-commerce order system, for cache data such as order information cache, assuming that the cache data is A, when a certain order data in the database is modified, and the order id corresponding to the order data is the same as the order id corresponding to the cache data A, it means that the data in the cache data A is no longer accurate, that is, it is invalid data, and the cache data A needs to be cleared.

[0075] Therefore, in the pre-set cache mapping relationship, for the cache type of order information cache, its corresponding change identifier is to modify the order, and its corresponding target data field is the order id. The change identifier can be understood as a data change scenario that will affect the cache data under the corresponding cache type. The change identifiers and target data fields for different cache types in the specific cache mapping relationship can be pre-configured in combination with the actual business logic.

[0076] Therefore, for a certain cache data A, after determining its corresponding target data field and change identifier according to the cache mapping relationship, its corresponding cache key can be generated according to the field value of the target data field and the change identifier of the cache data A. For example, the change identifier corresponding to the cache data A is: a new order, which is identified by order_info_add in the system, the target data field is the order ID, and the order ID corresponding to the cache data A is 1001. Therefore, the cache key corresponding to the cache data A can be generated according to order_info_add and 1001.

[0077] Returning to the content in step S103, after determining the first change identifier corresponding to this data modification operation and the first field value of the preset data field under the corresponding modified target data, a cache key containing the first field value and the first change identifier can be searched in the cache key set, that is, the first cache key. After finding the first cache key, the corresponding cache data is cleared, wherein the first field value includes field values ​​corresponding to multiple fields. The first change identifier can be combined with these two fields respectively to obtain two combinations, and the corresponding cache key is searched according to the two combinations.

[0078] For example, the user id is user_001, the order id is order_1001, and the change identifier is modifying the order ORDER_INFO_EDIT. Therefore, two combinations user_001:ORDER_INFO_EDIT and order_1001:ORDER_INFO_EDIT can be obtained. The cache keys corresponding to these two combinations are searched in the cache key set, and are determined as the first cache key, that is, the first cache key includes two cache keys, and the cache data corresponding to the first cache key is cleared.

[0079] The cache clearing method based on the order state machine provided in this embodiment determines the change identifier according to the business logic of the data modification operation when data modification occurs in the database, and determines the cache key corresponding to the cache data to be cleared in combination with the field value of the preset field under the target data corresponding to the modification operation, wherein the cache key is generated when the cache data is generated according to the change identifier and the target data field determined by the cache type and cache mapping relationship of the cache data, and the field value for the target data field in the cache data, that is, when the cache data is generated, the cache key is named according to the preset rules, so that in the scenario where the cache data needs to be cleared subsequently, it is only necessary to determine the business logic of the modification operation and the field value of a specific field in the modification data to determine the cache key to be cleared and clear the corresponding cache data, thereby realizing the decoupling between business logic and cache clearing, and eliminating the need to comb through all cache data every time the database changes, thereby ensuring the efficiency of cache data clearing.

[0080] According to an embodiment of the present invention, another cache cleaning method embodiment based on an order state machine is provided, which can be used for the above-mentioned cache cleaning. Figure 2 is a flowchart of another cache cleaning method based on an order state machine according to an embodiment of the present invention. Figure 2 As shown, the process includes the following steps:

[0081] Step S201, monitoring the data update of the order database. Figure 1 Step S101 of the illustrated embodiment will not be described in detail here.

[0082] Step S202, when a data modification operation occurs in the database, a corresponding first change identifier is determined based on the business logic of the data modification operation, and a first field value of a preset data field under the target data corresponding to the data modification operation is determined according to the context information of the state machine.

[0083] Specifically, in step S202, the context information in the order state machine is generated as follows:

[0084] Step A: When a user initiates a data modification operation on a database, the order state machine searches the database for target modification data corresponding to the data modification operation.

[0085] It can be understood that when a user initiates a data modification operation through the client, for example, modifying the order information of an order 1001 under his own account, a data modification operation on the database will be initiated. After receiving this operation request, the state machine will find the target modification data that the user wants to modify in the database, which is the data corresponding to order 1001 in the above example.

[0086] Step B: determine the field values ​​corresponding to the order id and the user id in the target modification data, and fill the field values ​​corresponding to the order id and the user id into the context of the order state machine.

[0087] Due to its own operating mechanism, the order state machine will place the target modification data found above in its own context module, and process the target modification data accordingly according to the user's specific operation request, and update the processed data to the database. On this basis, the field values ​​corresponding to the fields such as order id and user id in the target modification data will also be filled in the context of the order state machine. When the corresponding data needs to be called later, it can be directly obtained from the context of the order state machine.

[0088] Furthermore, the preset data fields include: order id and user id.

[0089] It can be understood that, in this embodiment, the preset data fields are set to the order id and user id fields. In actual application scenarios, the preset data fields can be set in combination with actual conditions. For example, on the basis of the above two fields, a service id can be added as a preset field. Here, the preset data fields are set to the order id and user id only for the consideration of common application scenarios of e-commerce orders.

[0090] In the above step S202, the first field value of the preset data field under the target data corresponding to the current data modification operation is determined according to the context information of the state machine, including:

[0091] The field values ​​of the order id and the user id of the target modification data corresponding to the current data modification operation in the context information of the state machine are determined as the first field values.

[0092] It can be understood that since the context information of the order state machine has recorded the specific field values ​​of the order id and user id corresponding to the target modification data, the field values ​​corresponding to the order id and user id can be directly used as the first field value in the context information of the order state machine.

[0093] Step S203: Based on the first change identifier and the first field value, search for the first cache key from the cache key set, and clear the cache data corresponding to the first cache key. Figure 1 Step S103 of the illustrated embodiment will not be described in detail here.

[0094] Step S204, when a data adding operation occurs in the database, a second change identifier is determined based on the business logic of the data adding operation; and a second field value of the preset data field is obtained according to the new data corresponding to the data adding operation.

[0095] It can be understood that, in the process of monitoring data updates of the order database, when a data addition operation is monitored, such as adding an order or a service to the order database, the corresponding data change identifier, i.e., the second change identifier, can be determined according to the specific business logic. Exemplarily, the types of pre-maintained data change identifiers may include: adding an order, modifying an order, adding a service, modifying a service, and other change identifiers.

[0096] For the newly added data corresponding to the current data adding operation in the order database, the second field value corresponding to the preset data field can be determined according to its specific data information. Similarly, the preset data field can be the user id and the order id.

[0097] Specifically, obtaining the second field value of the preset data field according to the newly added data corresponding to the data adding operation includes:

[0098] The field values ​​of the order id and user id in the newly added data are determined as the second field values.

[0099] Determine the field values ​​corresponding to these two fields in the newly added data, which is the second field value. For the specific logic, please refer to Figure 1 The contents of step S102 of the illustrated embodiment differ only in the source of the field values, one is determined by the context of the order state machine, and the other is determined directly based on the newly added data.

[0100] Step S205: based on the second change identifier and the second field value, search for the second cache key from the cache key set, and clear the cache data corresponding to the second cache key.

[0101] After determining the second change identifier and the second field value, similarly, refer to Figure 1 The content of step S103 of the illustrated embodiment combines the second change identifier and the second field value, searches for the corresponding second cache key, and clears the corresponding cache data.

[0102] For the above method embodiment, the cache key in the cache key set is generated when generating cache data based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier and target data field corresponding to the cache data are determined based on the cache mapping relationship and the cache type of the cache data.

[0103] Specifically, the change identifier and target data field corresponding to the cached data are determined as follows:

[0104] Step 1001, when generating cache data, determine the cache type of the cache data, the cache type includes: order information cache and user order statistics cache.

[0105] It can be understood that, in this embodiment, for the e-commerce order system, two cache types of cache data are exemplary set, namely, order information cache and user order statistics cache. The order information cache mainly includes the specific information of a certain order, such as order address, order product, order price, etc.; the user order statistics cache mainly includes the order statistics information corresponding to a certain user, such as order list, order quantity statistics, order amount statistics, etc.

[0106] Specifically, the cache type of cached data is determined as follows:

[0107] When the user queries the target order information in the database, first cache data corresponding to the target order information is generated in the cache layer, and the cache type of the first cache data is determined as order information cache;

[0108] When the user queries the order statistics information in the database, second cache data corresponding to the order statistics information is generated in the cache layer, and the cache type of the second cache data is determined as the user order statistics cache.

[0109] It can be understood that in some cases, when a user queries the information of a certain order, since there is no corresponding cache data in the cache layer, the user will search for the order in the database, that is, the order information of the target order. At this time, the first cache data corresponding to the target order information will be generated in the cache layer, and the cache type of the cache data will be determined as the order information cache.

[0110] Similarly, in some cases, when a user queries his own order statistics, since there is no corresponding cache data in the cache layer, the user will search for the corresponding information in the database, namely the order statistics information. At this time, a second cache data corresponding to the target order information will be generated in the cache layer, and the cache type of the cache data will be determined as the user order statistics cache.

[0111] Step 1002: If the cache type of the cache data is order information cache, the change identifier corresponding to the cache data is determined as a modified order, and the target data field corresponding to the cache data is determined as the order ID.

[0112] When the cache data is generated, if the cache type of the cache data is the order information cache, the corresponding change identifier is the modified order, that is, when the modified order data is updated in the database, it may affect the cache data. At the same time, the target data field corresponding to the cache data is determined as the order id. The cache key determined by the combination of these two can make the modified order data update appear in the database, and when the order id corresponding to the updated data is the same as the order id value corresponding to the above cache data, the cache key corresponding to the cache data is found based on the technical solution of the embodiment of the present method, and the cache data is cleared.

[0113] Step 1003: If the cache type of the cache data is user order statistics cache, the change identifier corresponding to the cache data is determined as modified order and / or added order, and the target data field corresponding to the cache data is determined as user id.

[0114] Similarly, referring to the above step 1002, if the cache type of the cache data is a user order statistics cache, the corresponding change identifier is a modified order and / or a new order, that is, when a modified order and / or a new order appears in the database, the data of these two situations may be affected when updated. At the same time, the target data field corresponding to the cache data is determined as the user id. The cache key determined by combining the change identifier and the field value corresponding to the target data field can cause the corresponding data update to appear in the database, and when the value of the user id corresponding to the updated data is the same as the value corresponding to the cache data, the cache key corresponding to the cache data is found based on the technical solution of the embodiment of the present method, and the cache data is cleared.

[0115] Specifically, the change identifier and target data field for each cache type can be set according to the requirements of the actual application scenario. In some special scenario requirements, multiple change identifiers or multiple target data fields can also be set, and and / or calculated to determine the corresponding cache key.

[0116] Furthermore, in the above method embodiment, the cache key in the cache key set is generated in the following manner:

[0117] Step 2001, determine the corresponding cache key generation formula according to the cache type of the cache data.

[0118] It can be understood that the cache key generation formula corresponding to each cache type is pre-configured, and after determining the change identifier and the field of the target data field corresponding to a certain cache data, the corresponding cache key can be generated according to the cache key generation formula.

[0119] Step 2002: after determining the change identifier and the target data field corresponding to the cached data, determine the field value of the cached data relative to the target data field.

[0120] In the above-mentioned related steps, after the change identifier and the target data field corresponding to the cached data are determined, the field value of the target data field can be directly determined according to the related information in the cached data.

[0121] Step 2003, write the change identifier corresponding to the cached data relative to the field value of the target data field into the corresponding cache key generation formula to obtain the cache key of the cached data.

[0122] It can be understood that after obtaining the change identifier corresponding to a certain cache data and the field of the target data field, the cache key corresponding to the cache data can be directly generated in combination with the cache key generation formula determined in the above step S2001.

[0123] Specifically, the cache mapping relationship involved in the above method embodiment is maintained in the JAVA language as follows:

[0124]

[0125]

[0126] In the above code, the change flag corresponding to the order information data cache is modified order ORDER_INFO_EDIT, the target data field is the order ID, ORDER_ID ("%orderId%"), and the cache key generation formula is as follows:

[0127] ORDER_INFO("order:"+PlaceholderEnum.ORDER_ID.getPlaceholder(),Collections.singletonList(DataChangeFlagEnum.ORDER_INFO_EDIT)).

[0128] The change flag corresponding to the user order quantity statistics cache is: modify order ORDER_INFO_EDIT, the target data field is: user ID, USER_ID ("%userId%"), and the cache key generation formula is as follows:

[0129] USER_ORDER_STATISTICS("userOStat:"+PlaceholderEnum.USER_ID.getPlaceholder(),Arrays.asList(DataChangeFlagEnum.ORDER_INFO_ADD,DataChangeFlagEnum.ORDER_INFO_EDIT)).

[0130] In actual situations, the cache mapping relationship may be set in combination with actual situations. The above is only an exemplary implementation and is not limited here.

[0131] The cache clearing method based on the order state machine provided by the embodiment of the present invention determines the change identifier according to the business logic of the data modification operation when data modification occurs in the database, and determines the cache key corresponding to the cache data to be cleared in combination with the field value of the preset field under the target data corresponding to the modification operation, wherein the cache key is generated when the cache data is generated according to the change identifier and the target data field determined by the cache type and cache mapping relationship of the cache data, and the field value for the target data field in the cache data, that is, when the cache data is generated, the naming of its cache key is set according to the preset rules, so that in the scenario where the cache data needs to be cleared subsequently, it is only necessary to determine the business logic of the modification operation and the field value of a specific field in the modification data to determine the cache key to be cleared and clear the corresponding cache data, thereby realizing the decoupling between business logic and cache clearing, and eliminating the need to comb through all cache data every time the database changes, thereby ensuring the cleaning efficiency of the cache data.

[0132] In order to assist in understanding the above method embodiment, an example flow chart of implementing cache cleaning is provided according to an embodiment of the present invention, as shown in FIG. Figure 3 shown.

[0133] First, a new query for user order data needs to be added on the user side, that is, the user needs to initiate a query for user order data to trigger the subsequent generation of cached data.

[0134] At the same time, in the development stage, pre-define the cache key enumeration, including: the generation expression of the cache key, the list of data change identifiers involved in the cache. Furthermore, you can maintain a set of cache key enumerations in the project, the mapping relationship between the data change identifiers involved in the cache key, and the association relationship between the identification fields of the data involved in the cache key (such as order number, service ID, user ID, etc.).

[0135] When a user queries an order, the cache data is queried first. If the cache exists, it is returned directly. If the cache does not exist, the DB data is queried and the cache is set. The cache key is generated according to the expression in the cache key enumeration pre-defined in the development phase.

[0136] When the user updates the order data: Combined with the state machine scenario, when filling the state machine context, the identification fields of the data (order id and user id) are filled into the Threadlocal context.

[0137] When modifying the database, take the identification field of the data from the ThreadLocal context, build a data change object based on the data change scenario, and add it to the Threadlocal context.

[0138] After the save method of the state machine is executed, the public cache cleaning method in the above method embodiment is executed, and a list of data change objects is obtained from the Threadlocal context. According to the enumeration of the data change identifier in the change object, the cache key enumeration list involved in the change is screened out in the cache key enumeration. Finally, a cache key is generated according to the data identifier and the key generation expression, and targeted data cleaning is performed according to the generated cache key.

[0139] For the above embodiment, it can be understood that when adding a new cache or modifying a cache in a data query, the identifier of the data change involved in the cache key and the data identification field involved in the cache key are maintained. When the data is updated, the data update identifier and the data identification field involved this time are added to the context. After the data update is completed, the cache key enumeration that needs to be changed is filtered out according to the data update identifier in the context, and the cache key involved is assembled according to the identification field of the data in the context. All keys are cached and cleaned.

[0140] For the order state machine, for the update scenario, when the state machine context is filled in createContext(), first query the order data required in the context, and fill the order ID and user ID in the ThreadLocal context.

[0141] When the business logic is saved in bizSave(), the data identification fields of users and orders are obtained from the context in each DB update method. For new scenarios, they are directly obtained from the data. The data update identifier and the data identification fields involved in the data are constructed into a data change object and filled in the data change object list of the java ThreadLocal context.

[0142] In the public cache clearing method cacheClear(), the data change object list is read from the java ThreadLocal context, and the cache key list that needs to be cleared is assembled through the mapping relationship to perform cache clearing.

[0143] Perform java ThreadLocal data cleanup before the state machine releases the lock.

[0144] The above method embodiment combines the SaaS-based order state machine process to decouple the data update logic from the cache cleanup. There is no need to worry about which caches need to be cleaned every time a data change operation is performed; the cache cleanup is unified and the amount of code is reduced. There is no need to hard-code the cache cleanup work after each data change operation, and the cache relationship abstraction can avoid the omission of cache cleanup.

[0145] In this embodiment, a cache cleaning device based on an order state machine is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and will not be repeated hereafter. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.

[0146] This embodiment provides a cache cleaning device based on an order state machine, such as Figure 4 As shown, including:

[0147] The database monitoring module 401 is used to monitor the data modification operations occurring in the order database.

[0148] The information determination module 402 is used to determine the corresponding first change identifier based on the business logic of the data modification operation when a data modification operation occurs in the database, and to determine the first field value of the preset data field under the target data corresponding to this data modification operation according to the context information of the state machine.

[0149] The cache key query module 403 is used to search for the first cache key from the cache key set based on the first change identifier and the first field value, and clean up the cache data corresponding to the first cache key; wherein the cache key in the cache key set is generated when the cache data is generated based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier and the target data field corresponding to the cache data are determined based on the cache mapping relationship and the cache type of the cache data.

[0150] In some optional implementations, the information determination module 402 is further configured to determine a second change identifier based on the business logic of the data addition operation when a data addition operation occurs in the database; and obtain a second field value of a preset data field according to the new data corresponding to the data addition operation;

[0151] The cache key query module 403 is further used to search for the second cache key from the cache key set based on the second change identifier and the second field value, and clear the cache data corresponding to the second cache key.

[0152] In some optional implementations, the change identifier and the target data field corresponding to the cached data are determined as follows:

[0153] When generating cache data, determine the cache type of the cache data, which includes: order information cache and user order statistics cache;

[0154] If the cache type of the cached data is order information cache, the change identifier corresponding to the cached data is determined as modified order, and the target data field corresponding to the cached data is determined as order id;

[0155] If the cache type of the cached data is user order statistics cache, the change identifier corresponding to the cached data is determined as modified order and / or new order, and the target data field corresponding to the cached data is determined as user id.

[0156] In some optional implementations, the cache keys in the cache key set are generated as follows:

[0157] Determine the corresponding cache key generation formula according to the cache type of the cached data;

[0158] After determining the change identifier and the target data field corresponding to the cached data, determining the field value of the cached data relative to the target data field;

[0159] The change identifier corresponding to the cached data is written into the corresponding cache key generation formula relative to the field value of the target data field to obtain the cache key of the cached data.

[0160] In some optional implementations, the context information in the order state machine is generated as follows:

[0161] When a user initiates a data modification operation on the database, the order state machine searches the database for the target modification data corresponding to the data modification operation;

[0162] The field values ​​corresponding to the order id and the user id are determined in the target modification data, and the field values ​​corresponding to the order id and the user id are filled into the context of the order state machine.

[0163] In some optional implementations, the cache type of cached data is determined as follows:

[0164] When the user queries the target order information in the database, first cache data corresponding to the target order information is generated in the cache layer, and the cache type of the first cache data is determined as order information cache;

[0165] When the user queries the order statistics information in the database, second cache data corresponding to the order statistics information is generated in the cache layer, and the cache type of the second cache data is determined as the user order statistics cache.

[0166] In some optional implementations, the preset data fields include: order id and user id;

[0167] The information determination module 402, when determining the first field value of the preset data field under the target data corresponding to the current data modification operation according to the context information of the state machine, includes:

[0168] Determine the field values ​​of the order id and user id of the target modification data corresponding to the current data modification operation in the context information of the state machine as the first field value;

[0169] The information determination module 402, when obtaining the second field value of the preset data field according to the newly added data corresponding to the data adding operation, includes:

[0170] The field values ​​of the order id and user id in the newly added data are determined as the second field values.

[0171] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.

[0172] The cache cleaning device based on the order state machine in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.

[0173] The embodiment of the present invention also provides a computer device having the above Figure 4 The cache cleaning device based on the order state machine is shown.

[0174] See also Figure 5 , Figure 5 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Figure 5 As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process the instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as, a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 5 A processor 10 is taken as an example.

[0175] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.

[0176] The memory 20 stores instructions executable by at least one processor 10, so that at least one processor 10 executes the method shown in the above embodiment.

[0177] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0178] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.

[0179] The computer device also includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Figure 5 The example of connecting through bus is taken in the following.

[0180] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the computer device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device can be a touch screen.

[0181] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.

[0182] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.

Claims

1. A cache cleaning method based on an order state machine, characterized in that: The method comprises: Monitor data updates in the order database; When a data modification operation occurs in the database, a corresponding first change identifier is determined based on the business logic of the data modification operation, and a first field value of a preset data field under the target data corresponding to the data modification operation is determined according to the context information of the state machine; Based on the first change identifier and the first field value, the first cache key is searched from the cache key set, and the cache data corresponding to the first cache key is cleared; wherein the cache keys in the cache key set are generated when the cache data is generated based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier corresponding to the cache data and the target data field are determined based on the cache mapping relationship and the cache type of the cache data.

2. The method according to claim 1, characterized in that The method further comprises: When a data adding operation occurs in the database, a second change identifier is determined based on the business logic of the data adding operation; Obtaining a second field value of a preset data field according to the newly added data corresponding to the data adding operation; Based on the second change identifier and the second field value, the second cache key is searched from the cache key set, and the cache data corresponding to the second cache key is cleared.

3. The method according to claim 1, characterized in that The change identifier corresponding to the cache data and the target data field are determined in the following manner: When generating cache data, determining the cache type of the cache data, the cache type including: order information cache and user order statistics cache; If the cache type of the cache data is order information cache, the change identifier corresponding to the cache data is determined as a modified order, and the target data field corresponding to the cache data is determined as an order id; If the cache type of the cache data is user order statistics cache, the change identifier corresponding to the cache data is determined as modified order and / or new order, and the target data field corresponding to the cache data is determined as user id.

4. The method according to claim 1, characterized in that: The cache keys in the cache key set are generated as follows: Determine a corresponding cache key generation formula according to the cache type of the cache data; After determining the change identifier and the target data field corresponding to the cached data, determining the field value of the cached data relative to the target data field; The change identifier corresponding to the cached data is written into the corresponding cache key generation formula relative to the field value of the target data field to obtain the cache key of the cached data.

5. The method according to claim 1, characterized in that The context information in the order state machine is generated as follows: When a user initiates a data modification operation on a database, the order state machine searches the database for target modification data corresponding to the data modification operation; The field values ​​corresponding to the order id and the user id are determined in the target modification data, and the field values ​​corresponding to the order id and the user id are filled into the context of the order state machine.

6. The method according to claim 3, characterized in that The cache type of the cached data is determined in the following manner: When the user queries the target order information in the database, first cache data corresponding to the target order information is generated in the cache layer, and the cache type of the first cache data is determined as order information cache; When a user queries order statistics information in a database, second cache data corresponding to the order statistics information is generated in a cache layer, and a cache type of the second cache data is determined as a user order statistics cache.

7. The method according to claim 5, characterized in that The preset data fields include: order id and user id; Determining the first field value of the preset data field under the target data corresponding to the current data modification operation according to the context information of the state machine includes: Determine the field values ​​of the order id and user id of the target modification data corresponding to the current data modification operation in the context information of the state machine as the first field value; The obtaining, according to the newly added data corresponding to the data adding operation, a second field value of a preset data field comprises: The field values ​​of the order id and the user id in the newly added data are determined as the second field values.

8. A cache cleaning device based on an order state machine, characterized in that: The device comprises: Database monitoring module, used to monitor data modification operations in the order database; An information determination module, used for determining a corresponding first change identifier based on the business logic of the data modification operation when a data modification operation occurs in the database, and determining a first field value of a preset data field under the target data corresponding to the data modification operation according to the context information of the state machine; A cache key query module is used to search for a first cache key from a cache key set based on the first change identifier and the first field value, and to clean up cache data corresponding to the first cache key; wherein the cache keys in the cache key set are generated when generating cache data based on the change identifier corresponding to the cache data and the field value of the target data field; the change identifier corresponding to the cache data and the target data field are determined based on a cache mapping relationship and a cache type of the cache data.

9. A computer device, characterized in that: include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, computer instructions are stored in the memory, and the processor executes the cache clearing method based on the order state machine described in any one of claims 1 to 7 by executing the computer instructions.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the cache clearing method based on the order state machine described in any one of claims 1 to 7.