Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the basic function of a shopping cart with redis

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to use redis to achieve the basic functions of a shopping cart, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Preparatory work:

Database table design (yj_product_specification: commodity specification table, user table, commodity table, etc. {No specification table, only specification value table,})

Testing tool: POSTMAN

Redis client: Redis Desktop Manager

I won't talk about all the other tools.

Logical processing:

Basic functions of shopping cart:

1. Shopping cart list

two。 add to cart

3. Delete an item from the shopping cart

4. Empty the shopping cart

First of all, let's analyze the addition of the shopping cart, which is also the most difficult function in the shopping cart (in my opinion)

When joining the shopping cart, we have to consider how to deal with different specifications and goods for a customer in memory. The necessary parameters are first of all user information to get (mem_Id), product information (prd_id), product corresponding specification value id, this information we have to get, we also need to consider for different users, different information, how to store in redis. I won't talk about the type of redis here. My division in redis is based on the user's id as the key of redis. Of course, for different goods, we also need to pay attention to the different specifications and quantity of a product (we are stored in hash), and to ensure that the key of hash is unique. If we make the key of hash composed of commodity id, specification value, etc., then we can easily distinguish it. The parameters passed by the front end are all fixed, so we use an entity to receive them directly.

Use debug to see what's in this cart. Pay attention to this and that multi-information assembly.

Private void addCart (Member member, Cart cart) throws Exception {

/ / listen to the current login user

Jedis jedis = jedisUtil.KEYS.watch ("cart_" + member.getId ()); / / listen for key

Transaction multi = jedis.multi (); / / start the transaction with the returned jedis object

String cartFiled = cart.getProductId () + "_" + cart.getSpecificationValueIds ()

/ / Shopping cart object string, which is used to query the shopping cart information of this specification on the current redis

String cartStr = jedisUtil.HASH.hget ("cart_" + member.getId (), cartFiled)

/ / convert String type to Cart object

Cart _ cart = Cart.parseObject (cartStr)

/ / Shopping cart exists and the quantity of goods is added up.

If (_ cart! = null) {

Integer num = cart.getNumber ()

_ cart.setNumber (_ cart.getNumber () + num)

_ cart.setPrice (cart.getPrice ())

_ cart.setFullName (cart.getFullName ())

_ cart.setImage (cart.getImage ())

} else {

_ cart = cart

}

/ / one shopping cart per user, one product specification corresponding to one shopping cart item, store the shopping cart in redis

Multi.hset ("cart_" + member.getId (), cartFiled, _ cart.toJSONString ())

/ / get the transaction execution result. If the value of the listening mahjongXA changes, the transaction will fail, and null will be returned.

List exec = multi.exec ()

/ / release monitoring

JedisUtil.KEYS.unwatch (jedis)

/ / when the transaction fails, the code is re-executed

If (exec = = null) {

AddCart (member, cart)

}

}

When users join the shopping cart, we need to see whether the current goods and specifications already exist in redis. If they exist, just add or subtract those that do not exist in the quantity.

/ / Shopping cart exists and the quantity of goods is added up.

If (_ cart! = null) {

Integer num = cart.getNumber ()

_ cart.setNumber (_ cart.getNumber () + num)

_ cart.setPrice (cart.getPrice ())

_ cart.setFullName (cart.getFullName ())

_ cart.setImage (cart.getImage ())

} else {

_ cart = cart

}

In terms of user operations on the client side, it is possible to cause concurrency if the user clicks continuously at the front end, so when I join the shopping cart, I pay attention to concurrent events. Redis provides me with watch. I can monitor through watch to avoid continuous clicks.

When the redis cache is fetched, if the value changes, the event under the watch will be interrupted, in which case

If the exec object is null, it will go to redis to get it again.

It's easy to delete and empty the shopping cart. We only need to understand the storage of redis to solve these two functions.

Of course, for users, it is divided into login users and non-login users. The above are logged-in users. Non-logged-in users need to store the cookie first and take it out from the cookie when logging in.

On how to use redis to achieve a shopping cart basic functions to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report