The perfect way to store product information in a shopping cart at asp.net

I am creating an e-commerce application in which I have a shopping cart.

My current structure is that whenever a user saves a product, I put the product in a datatable and datatable for the session, so that until the session is completed, the user can have a list that they saved.

However, I increased the session timeout to 600 minutes (10 hours), but I am worried that this is the best approach to store information about the shopping cart, because if there are, for example, 1000 users accessing the site at the same time, being 1000 session objects created on the server will not degrade the performance of my site.

I can’t save it in the database, because if the user is anonymous, I don’t have a unique thing about the user, so if 1000 users get access to the site, all products get merged, I can’t extract the product saved by the current user.

Is there any other better approach for this problem.

+6
source share
1 answer

You should not implement this. Sessions of 10 hours are very long, if you have 1000 users on your site, your server will suffer and create some performance problems.

On our e-commerce sites, we create cookies for anonymous users. After that, you have two solutions:

  • Store the entire basket in a cookie (basically a list of product identifiers + quantity, depending on your needs / strategy) or other methods (e.g. localStorage).
  • Save the unique identifier in the cookie and save the basket in the database (temporary table) with the unique cookie identifier as the identifier. After the user logs in, you move this data to the user’s recycle bin table.

You can easily change the cookie expiration time (or delete it) without affecting the overall performance of your server. For identifiers you can use Guid.NewGuid() .

UPDATE:

More on my second solution - scripts:

  • User Visitor Website
  • You create a cookie without expiration: cartId with the value Guid.NewGuid() (the name and value are up to you).
  • User clicked Buy
  • Get the back end of the cookie and add cartId, product / quantity to the AnonymousBasket table.
  • Imagine that the user leaves the website and returns in a month, the cookie will still be here.
  • If an anonymous user is sent to his cart, you can get his products and quantities based on the value of the cookie.
  • If the user is logged in, you delete the cookie and move the data from the Anonymous Recycle Bin table to the Recycle Bin.
  • Now your registered user can process the checkout

With this solution, everything is stored in the database and does not require sessions. You can change the expiration of your cookies, just like for sessions, or you can clear the temporary table yourself without any risks or create statistics for anonymous users (why they did not proceed to check how many items are on average, what item, ... )

+19
source

Source: https://habr.com/ru/post/952278/


All Articles