We all shop online. And sometimes we add products to our cart, even if we don't buy them at that moment. The length of time our products stay in the cart varies depending on the company's strategies. I will share information about where the product information in the cart should be kept, which is important for us software developers, and I will explain with source codes where I keep it myself.
Where can we store cart information ?
Database
If we want, we can keep cart information in a database. But I don't recommend this. Since every change made by users will create a need to update the cart, constant requests will need to be made to the database. It will create unnecessary traffic and fill the database.
Localstorage
If we wish, we can keep the cart information in localstorage, but when the user clears his/her browser or uses a different browser, it will not be possible to access this information. That's why I don't recommend this either.
Cookie
The most common known method is the cookie method. Many e-commerce sites store cart information in the browser through cookies. This method has advantages as well as disadvantages. Again, the information I said about localstorage in the previous section is valid here as well. If the user does not log in to the browser, uses another browser, or clears the browser memory, it will not be possible to access the cart information. However, keeping the information here will be faster and more accessible for the system.
Redis
Redis (for REmote DIctionary Server) is an open source, in-memory, NoSQL key/value store that is used primarily as an application cache or quick-response database.Yes, redis is also a database type, but it is an in-memory database model that can work with NOSQL logic. It is a fast no-sql database model that can work with key-value logic. This will be my choice. Today, I will explain how we can use cart information with redis by showing examples with codes.
Project
First of all, you need to install redis on your computer. There are many different ways to set this up. You can install it using the Power Shell on Redis' website or via Docker. You can easily find various resources on the internet on how to install Redis on your computer.I will be developing an e-commerce site with backend .netcore 7.0 version.
In order to use Redis in our project, we need to download the StackExchange.Redis package from Nuget Gallery. We are added connectionstring for redis on file appsettins.json
"ConnectionStrings": {
"Redis":"localhost"
},
After that, we need to add the following setting to the program.cs file to use Redis.
builder.Services.AddSingleton<IConnectionMultiplexer>(c=>
Now let's create a class for the product information we will keep in the cart. For my case I will need these fields.
{
var options=ConfigurationOptions.Parse(builder.Configuration.GetConnectionString("Redis"));
return ConnectionMultiplexer.Connect(options);
});
public class BasketItem
{
public string ProductCode { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public string PicturePath { get; set; }
public string Category { get; set; }
public string SubCategory { get; set; }
}
Since there will be more than one cart information, let's create a class called CustomerBasket to list and use them separately.
public class CustomerBasket
{ public CustomerBasket()
{
}
public CustomerBasket(string id)
{
Id=id;
}
public string Id { get; set; }
public List<BasketItem> Items { get; set; }=new List<BasketItem>();
}
Now let's create an interface that contains the cart methods. Such as UpdateCart,GetCart,DeleteCart etc.
We will need another class to fill the cart methods that we created.
Now Let's create a cart control for cart transactions and prepare the appropriate endpoints.
You can easily update and delete card information with a single key-value structure as you see here. Thanks to Redis, you can easily adjust how long product information can remain in users' carts. Large companies such as Amazon generally keep basket information for 30 days. You can access and review the entire project here.