admin管理员组文章数量:1023025
Our site has a lengthy AJAX function that appends content to the user's cart, which is stored as an array in the usermeta
table.
The function looks something like this:
$cart = get_user_meta($userID, 'cart', true);
$cart[] = $newItemID;
update_user_meta($userID, 'cart', $cart);
We're running into a problem where, when the user tries to add multiple items to their cart in rapid succession, the final call to update_user_meta will sometimes overwrite the cart with old data.
From what I understand, this is a race condition which is partly caused by the fact that WordPress stores array data as a serialized string rather than as a set of table rows.
I know that I could just create a cart
table and use queries to manage it, but I was hoping that there was a way to do it with the existing get_user_meta/update_user_meta functions.
Our site has a lengthy AJAX function that appends content to the user's cart, which is stored as an array in the usermeta
table.
The function looks something like this:
$cart = get_user_meta($userID, 'cart', true);
$cart[] = $newItemID;
update_user_meta($userID, 'cart', $cart);
We're running into a problem where, when the user tries to add multiple items to their cart in rapid succession, the final call to update_user_meta will sometimes overwrite the cart with old data.
From what I understand, this is a race condition which is partly caused by the fact that WordPress stores array data as a serialized string rather than as a set of table rows.
I know that I could just create a cart
table and use queries to manage it, but I was hoping that there was a way to do it with the existing get_user_meta/update_user_meta functions.
- Have you got any additional plugin for handling e-commerce or you're doing the whole thing on your own? – Kumar Commented Apr 24, 2019 at 2:54
- @sven - The plugin is completely in-house. Even though this specific issue is about e-commerce, the question is more of a general one. It could be applied to any situation where update_user_meta is called frequently. – Pikamander2 Commented Apr 24, 2019 at 3:00
1 Answer
Reset to default 0This happens due to the nature of ajax calls - they are asynchronous. When you have a lengthy function (or heavy plugins and slow site loading), several ajax calls are running at the same time, and nobody can predict, which one will get access to db data faster.
Usage of own tables is not a solution in this situation.
The easiest way to solve the problem is to block further adding of a product by Javascript until current ajax is finished.
So, sequence of actions in js should be:
by the click on the add to cart button set flag blocking other clicks
run ajax
in plete function delete the flag so allowing further add to cart clicks
Our site has a lengthy AJAX function that appends content to the user's cart, which is stored as an array in the usermeta
table.
The function looks something like this:
$cart = get_user_meta($userID, 'cart', true);
$cart[] = $newItemID;
update_user_meta($userID, 'cart', $cart);
We're running into a problem where, when the user tries to add multiple items to their cart in rapid succession, the final call to update_user_meta will sometimes overwrite the cart with old data.
From what I understand, this is a race condition which is partly caused by the fact that WordPress stores array data as a serialized string rather than as a set of table rows.
I know that I could just create a cart
table and use queries to manage it, but I was hoping that there was a way to do it with the existing get_user_meta/update_user_meta functions.
Our site has a lengthy AJAX function that appends content to the user's cart, which is stored as an array in the usermeta
table.
The function looks something like this:
$cart = get_user_meta($userID, 'cart', true);
$cart[] = $newItemID;
update_user_meta($userID, 'cart', $cart);
We're running into a problem where, when the user tries to add multiple items to their cart in rapid succession, the final call to update_user_meta will sometimes overwrite the cart with old data.
From what I understand, this is a race condition which is partly caused by the fact that WordPress stores array data as a serialized string rather than as a set of table rows.
I know that I could just create a cart
table and use queries to manage it, but I was hoping that there was a way to do it with the existing get_user_meta/update_user_meta functions.
- Have you got any additional plugin for handling e-commerce or you're doing the whole thing on your own? – Kumar Commented Apr 24, 2019 at 2:54
- @sven - The plugin is completely in-house. Even though this specific issue is about e-commerce, the question is more of a general one. It could be applied to any situation where update_user_meta is called frequently. – Pikamander2 Commented Apr 24, 2019 at 3:00
1 Answer
Reset to default 0This happens due to the nature of ajax calls - they are asynchronous. When you have a lengthy function (or heavy plugins and slow site loading), several ajax calls are running at the same time, and nobody can predict, which one will get access to db data faster.
Usage of own tables is not a solution in this situation.
The easiest way to solve the problem is to block further adding of a product by Javascript until current ajax is finished.
So, sequence of actions in js should be:
by the click on the add to cart button set flag blocking other clicks
run ajax
in plete function delete the flag so allowing further add to cart clicks
本文标签: performanceIs there a way to append meta data without creating a race condition
版权声明:本文标题:performance - Is there a way to append meta data without creating a race condition? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560682a2156143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论