Disclosure of Invention
Aiming at least one defect or improvement requirement in the prior art, the invention provides a method, a system and a storage medium for realizing inventory management, which do not need to frequently access the inventory quantity of commodities in a database, do not block other users, realize safety reduction of inventory quantity and memory access of inventory quantity, and reduce database interaction.
To achieve the above object, according to a first aspect of the present invention, there is provided a method for achieving inventory management, including the steps of:
s1, receiving order request from user, reading stock data and version number;
s2, receiving an order submission request and adding the stock version number read in the step S1 to the order submission request, wherein the order submission request also comprises the order placing quantity;
s3, performing redis distributed lock operation on all order submission requests, wherein each order submission request is stored in a transaction queue as a transaction, and the step S4 is sequentially performed on each order submission request according to the sequence stored in the transaction queue;
and S4, judging whether the order submitting request can be processed successfully, and updating the stock data and the stock version number by using an optimistic lock mechanism after the order submitting request is processed successfully.
Preferably, the S4 includes the sub-steps of:
s41, reading the current stock version number again, comparing whether the stock version number in the order submitting request is consistent with the current stock version number, if so, executing the step S42, and if not, executing the step S43;
s42, returning order submission success information to the front-end ordering interface and updating the stock data and the stock version number by using an optimistic lock mechanism;
s43, covering the last read version number with the stock version number in the order submission request, reading the stock data and the stock version number again, judging whether the stock is enough according to the stock data, if so, comparing whether the stock version number in the order submission request is consistent with the stock version number, if so, executing the step S42, and if not, executing the step S43 again until the preset end condition is met.
Preferably, the S42 further includes: if an item removal request is received, a trigger mechanism is used to trigger an item sales volume.
Preferably, the S42 further includes: and if an order cancellation request is received or no article removal request is received within preset time, triggering the stock data to be restored to the stock data corresponding to the previous stock version number by using a trigger mechanism, and updating the stock version number.
Preferably, the method is performed on a server.
According to a second aspect of the present invention, there is provided a system for implementing inventory management, comprising:
the reading module is used for receiving a user order placing request and reading the inventory data and the inventory version number at the moment;
the receiving module is used for receiving the order submitting request and adding the inventory version number read by the reading module to the order submitting request, and the order submitting request also comprises the order placing quantity;
the distributed lock operation module is used for performing redis distributed lock operation on all order submission requests, wherein each order submission request is stored in a transaction queue as a transaction, and the step S4 is sequentially performed on each order submission request according to the sequence stored in the transaction queue;
and the order processing module is used for judging whether the order submitting request can be processed successfully or not and updating the stock data and the stock version number by using an optimistic lock mechanism after the order submitting request is processed successfully.
Preferably, the order processing module is configured to implement the following sub-steps:
s41, reading the current stock version number again, comparing whether the stock version number in the order submitting request is consistent with the current stock version number, if so, executing the step S42, and if not, executing the step S43;
s42, returning order submission success information to the front-end ordering interface and updating the stock data and the stock version number by using an optimistic lock mechanism;
s43, covering the last read version number with the stock version number in the order submission request, reading the stock data and the stock version number again, judging whether the stock is enough according to the stock data, if so, comparing whether the stock version number in the order submission request is consistent with the stock version number, if so, executing the step S42, and if not, executing the step S43 again until the preset end condition is met.
Preferably, the S42 further includes the steps of: if an article removal request is received, a trigger mechanism is used to trigger the sales volume of the newly added article.
Preferably, the S42 further includes: and if an order cancellation request is received or no article removal request is received within preset time, triggering the stock data to be restored to the stock data corresponding to the previous stock version number by using a trigger mechanism, and updating the stock version number.
According to a third aspect of the invention, there is provided a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the method of any of the above.
In general, compared with the prior art, the invention has the following beneficial effects:
(1) according to the method for realizing the canteen dish inventory management based on the trigger, provided by the invention, a database trigger, redis atomic operation and sql optimistic locking mechanism is adopted, so that the number of commodity inventories in the database does not need to be frequently accessed, other users are not blocked, the safe reduction of the inventory quantity and the memory access inventory quantity are realized, and the database interaction is reduced;
(2) the method for realizing the canteen dish inventory management based on the trigger can realize the functions of dish management, dish shelf loading and unloading, dish statistics and the like, can be accessed to a system for ordering, user payment and the like, and has expandability;
(3) the method for realizing the canteen dish inventory management based on the trigger can be additionally optimized for high-concurrency meal ordering, reduces the system operation burden and effectively improves the user experience.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention. In addition, the technical features involved in the embodiments of the present invention described below may be combined with each other as long as they do not conflict with each other.
Terms related to the embodiments of the present invention are explained as follows.
Redis (remote Dictionary Server), a remote Dictionary service, is an open-source log-type and Key-Value (Key-Value) database written in ANSIC language, supporting network, based on memory and persistent, and provides API of multiple languages. Redis is a memory-based data structure store that can be used as database, cache, and message middleware. Redis also exists in the form of message queues, and exists as an embedded List, so that the real-time high concurrency requirement is met.
A trigger is a database object associated with a table that triggers when a defined condition is satisfied and executes a set of statements defined in the trigger. This characteristic of the trigger may assist the application in ensuring data integrity on the database side. It differs from the stored procedure, mainly performed by event triggering, i.e. not actively invoked; the stored procedure needs to actively call the name to execute. Data can be forcibly checked or converted (data protection is ensured) before data is written, and when the trigger has an error, the operation which is successfully executed by the previous user can be cancelled.
As shown in fig. 1, a system architecture of an application of a method for implementing inventory management according to an embodiment of the present invention includes a user layer, a presentation layer, a business logic layer, and a data layer.
The users of the user layer are divided into managers and users; the presentation layer is used for providing the user to input the dish inventory and reserve and order the meal operation.
The data layer classifies the data collected by the facility layer, and the data is mainly classified into user data, basic data and business data for classified storage, so that various data processing and interface service management are realized, a data exchange mechanism is provided for different data access, and a data unified access interface is provided for the outside.
The business processing layer is used for monitoring an order placing request (an instruction of adding commodities to the shopping cart) and an order submitting request of a user and sending out a commodity inventory request; and the stock management module is used for transmitting the stock management module to a background processing stock operation logic function, processing multiple concurrent ordering requests by using a redis atomic operation + sql optimistic lock mechanism, and processing stock changes caused by sales volume and order state changes by using a trigger. The business process layer may be implemented on a server.
As shown in fig. 2, the method for implementing inventory management according to the embodiment of the present invention may be applied to inventory management of canteen dishes, but may also be applied to other inventory management, and the principle of the inventory management method in different scenarios is the same. The following takes the management of the inventory of dishes as an example.
An operational flow diagram at the administrator side comprising the steps of:
s1: adding dish information at the rear end of management;
s2: and setting the shelf date and the number of meals of the dishes, and meanwhile, setting the stock of the dishes on the shelf.
As shown in fig. 3, a flowchart of a method for implementing inventory management based on a trigger according to an embodiment of the present invention on a user operation side includes the following steps:
s1: selecting the meal ordering date and the meal times;
s2: selecting food ordering dishes to be added into the shopping cart, and editing the quantity of the dishes in the shopping cart;
s3: judging whether the quantity of the shopping cart dishes exceeds the current inventory, if so, entering a step S2, otherwise, entering a step S4;
s4: submitting a meal ordering order;
s5: judging whether the quantity of the ordered dishes exceeds the current inventory, if so, entering a step S2, otherwise, entering a step S6;
s6: the system generates an order that the user can cancel before the order is confirmed or pay to complete the meal.
As shown in fig. 4, a business logic implementation flow of the method for implementing inventory management based on triggers according to the embodiment of the present invention includes the following steps:
and S1, receiving order placing request of the user, and reading the inventory data and the inventory version number at the moment.
The order placing request of the user can be, for example, an instruction for adding commodities to a shopping cart, and after the order placing request of the user is monitored by the business layer, the inventory data and the inventory version number at the moment are read from the background database. And returning the stock data to a front-end interface to be displayed to a user, wherein the user can only click to submit the order if the added order placing quantity is less than the stock data.
S2, receiving an order submission request and adding the stock version number read in the step S1 to the order submission request, wherein the order submission request also includes the order placing quantity.
The service layer receives an order submission request from the user, where the order submission request includes the order quantity and the inventory version number read in step S1.
S3, performing redis distributed lock operation on all order submission requests, wherein each order submission request is stored in a transaction queue as a transaction, and the step S4 is sequentially performed on each order submission request according to the sequence stored in the transaction queue.
Specifically, the method comprises the following steps: distributed locks are one way to control the synchronous access to shared resources between distributed systems. In a distributed system, it is often necessary to coordinate the actions of the systems. If one or a group of resources are shared between different systems or different hosts of the same system, then access to these resources often requires mutual exclusion to prevent interference with each other to ensure consistency, in which case a distributed lock is used. The contention of the distributed lock control thread for resources can ensure high-efficiency concurrency and correct operation.
Implementation of a redis distributed lock: the mainly used redis setnx (), firstly, a certain task identification name is stored into the redis as a key, and an expiration time is set for the task identification name; for the request again, see whether the insertion into the redis can be done through setnx () first, if so, return true, and if not, return false.
And S4, judging whether the order submitting request can be processed successfully, and updating the stock data and the stock version number by using an optimistic lock mechanism after the order submitting request is processed successfully.
And performing an optimistic locking mechanism on all the order submission requests which are processed successfully, namely updating the stock data in the dish stock data table after each order request is processed successfully, modifying a data version number version field to indicate the number of times the data is modified, and adding one to the version value when the stock data is modified.
Preferably, the step S4 includes the sub-steps of:
s41, reading the current stock version number again, comparing whether the stock version number in the order submission request is consistent with the current stock version number, if so, executing step S42, otherwise, executing step S43.
Since there is a delay between receiving the order request and processing the order submission request, for the same user, the inventory data and the inventory version number read when receiving the order request may be different from the inventory data and the inventory version number when processing the order submission request, and thus it is necessary to compare the inventory version numbers to determine whether there is a change in the inventory.
And S42, returning order submission success information to the front-end ordering interface and updating the stock data and the stock version number by using an optimistic lock mechanism.
If the comparison in step S41 shows that the inventory version number in the order submission request is consistent with the inventory version number at that time, which indicates that the inventory data has not been modified since the order was placed, the inventory must satisfy the order requirement, and the inventory data and the inventory version number are updated using the optimistic lock mechanism.
By adopting an optimistic locking mechanism, the inventory data can be modified and the version number is increased by 1 when each order submission request is successfully processed, and whether the inventory data are consistent or not can be known through comparison of the version numbers.
S43, covering the last read version number with the stock version number in the order submission request, reading the stock data and the stock version number again, judging whether the stock is enough according to the stock data, if so, comparing whether the stock version number in the order submission request is consistent with the stock version number, if so, executing the step S42, and if not, executing the step S43 again until the preset end condition is met.
If the comparison in step S41 shows that the stock version number in the order submission request is not consistent with the stock version number at that time, it means that the stock data is modified since the order is placed, and the stock does not necessarily meet the order requirement, the last read version number is used to cover the stock version number in the order submission request, then the stock data and the stock version number at that time are read again, it is determined whether the stock is sufficient according to the stock data at that time, if so, it is compared whether the stock version number in the order submission request is consistent with the stock version number at that time, and if so, the stock data and the stock version number are updated by using an optimistic lock mechanism.
The preset end condition may be set as the number of failed retries, for example, the processing of step S43 is executed again for 3 times, and if the processing still fails after 3 times, the user is prompted that the order processing fails, and the user is requested to submit again.
For example, assuming that user a adds 10 shopping carts and the read inventory of goods is 100, user a may submit an order, assuming that after user a adds a shopping cart and before it submits an order, user B also places 20 orders and submits an order first, and the order submission request of user B is located before the order submission request of user B in the transaction queue, user B will process the order submission request first, step S41 is executed, the inventory version number at this time is read, if it is determined that the inventory version number in the order submission request of user B is consistent with the inventory version number at this time, step S42 is executed if it is consistent, and step S43 is executed if it is not consistent. And then, the order submission requests of the user A are processed again according to the sequence stored in the transaction queue, the current inventory version number is read again, and the inventory version number in the order submission requests of the user A is compared with the current inventory version number to judge whether the inventory version number is consistent. If the order submission request of the user B is successfully processed before that, the inventory version number in the order submission request of the user a and the inventory version number at that time are definitely not consistent, and step S43 is executed.
Step S42 further includes: if an item removal request (e.g., a meal fetch request) is received, a trigger mechanism is used to trigger the item sales.
Step S42 further includes: if an order cancellation request is received or no article removal request is received within a preset time, a trigger mechanism is used for executing rollback processing, namely, the stock data is triggered to be restored to the stock data corresponding to the previous stock version number, and the stock version number is added with 1.
The system for realizing inventory management in the embodiment of the invention comprises:
the reading module is used for receiving a user order placing request and reading the inventory data and the inventory version number at the moment;
the receiving module is used for receiving the order submitting request and adding the inventory version number read by the reading module into the order submitting request, and the order submitting request also comprises the order placing quantity;
the distributed lock operation module is used for performing redis distributed lock operation on all order submission requests, wherein each order submission request is stored in a transaction queue as a transaction, and the step S4 is sequentially performed on each order submission request according to the sequence stored in the transaction queue;
and the order processing module is used for judging whether the order submitting request can be processed successfully or not and updating the stock data and the stock version number by using an optimistic lock mechanism after the order submitting request is processed successfully.
Preferably, the order processing module is configured to implement the following sub-steps:
s41, reading the current stock version number again, comparing whether the stock version number in the order submitting request is consistent with the current stock version number, if so, executing the step S42, and if not, executing the step S43;
s42, returning order submission success information to the front-end ordering interface and updating the stock data and the stock version number by using an optimistic lock mechanism;
s43, covering the last read version number with the stock version number in the order submission request, reading the stock data and the stock version number again, judging whether the stock is enough according to the stock data, if so, comparing whether the stock version number in the order submission request is consistent with the stock version number, if so, executing the step S42, and if not, executing the step S43 again until the preset end condition is met.
Preferably, the step S42 further includes the steps of: if an article removal request is received, a trigger mechanism is used to trigger the sales volume of the newly added article.
Preferably, the S42 further includes: and if an order cancellation request is received or no article removal request is received within preset time, triggering the stock data to be restored to the stock data corresponding to the previous stock version number by using a trigger mechanism, and updating the stock version number.
The embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, where the computer program is executed by a processor to implement any of the above-mentioned technical solutions of the embodiments of the inventory management method. The implementation principle and technical effect are similar to those of the above method, and are not described herein again.
It must be noted that in any of the above embodiments, the methods are not necessarily executed in order of sequence number, and as long as it cannot be assumed from the execution logic that they are necessarily executed in a certain order, it means that they can be executed in any other possible order.
It will be understood by those skilled in the art that the foregoing is only a preferred embodiment of the present invention, and is not intended to limit the invention, and that any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the scope of the present invention.