1.1. Common implementation of shopping cart

Shopping cart is a function that will be used in any e-commerce project, and it is often asked in interviews. The traditional way is to use relational databases, such as mysql, to establish a CAT shopping cart table and put relevant shopping cart product information into the database.

CREATE TABLE goods_shopcar (
id int(4) NOT NULL ,
uid int(10) NULL ,
goods_id int(10) NULL ,
number int(10) NULL ,
status tinyint(4) NULL ,
create_time int(10) NULL ,
update_time int(10) NULL ,
PRIMARY KEY (id));
Copy the code

Such a design has no problem in function, but in practice, due to the large amount of shopping cart data, and frequent modification, will lead to increased pressure on the database, so in formal project development, generally will not directly use relational database to store shopping cart information.

Since you don’t use a relational database, but choose from a non-relational database, many architects will choose to use mongodb to store shopping cart data, which is also perfectly ok, mongodb has a very rich query API, performance is better than traditional relational database, using mongodb is indeed a good choice.

However, compared with mongodb, some companies also use Redis to store shopping cart information. Redis has better performance than Mongdb.

Here’s the idea of using Redis to complete the shopping cart.

1.2. Enable persistence

The shopping cart data, since it is not stored in a relational database, cannot be lost using Redis storage. Therefore, Redis must enable persistence, so that the data will not be lost even if Redis restarts.

Redis has two persistence mechanisms, one called RDB and one called AOF.

An RDB, also called a mirror file, stores a mirror image of redis memory data at a certain time

Aof is short for append Only File, which stores the log file. Specifically, it stores the RESP command log. RESP command is not the focus of this time, and will not be discussed here.

First, RDB is enabled by default. In the Redis configuration file, the configuration of RDB is as follows

Save 900 1: If at least one key changes within 900 seconds, save 300. 10: If at least 10 keys change within 300 seconds, save 60 10000. If at least 10000 keys change within 60 seconds, the value is savedCopy the code

The default configuration is at least 60 seconds and the image will be refreshed only when 10000 keys change. In this way, if redis breaks down unexpectedly before 60 seconds, all data will be lost during this period

You may be wondering if you can add a configuration

Save 1 1: If at least one key value changes within 1 second, the key is savedCopy the code

In this way, the data seems not to be lost, but this is certainly not possible. Every second, all the data in Redis memory is written to disk to become a mirror file, which has a significant impact on performance.

In order to prevent data loss, it is still not possible to start from RDB. By default, AOF is not enabled. Aof log file should be enabled

appendonly yes
Copy the code

With AOF logging enabled, the possibility of data loss is much reduced.

1.3. Business analysis

Taking the shopping cart of JINGdong as an example, according to the business analysis, the following functions need to be completed:

  1. Select all – gets all cart items for all users
  2. Quantity of items – The cart icon displays the total number of items in the cart
  3. Delete – Be able to remove an item from your cart
  4. Increase or decrease the quantity of a good

1.4. Data structure selection

Redis five kinds of commonly used data type is string, respectively the hash, list, set, zset, in this case we have chosen to use hash to complete the shopping cart function

This section describes common commands for the hash data type

If the key does not exist, create the key. If the key does exist, the original value will be overwritten

Redis 127.0.0.1:6379>hset myHash name enjoy ---- Set the key value of myset to name and value to EnjoyCopy the code

Hget to view hash in redis

Redis 127.0.0.1:6379>hget myhash name ---- "Enjoy ", obtain value redis 127.0.0.1:6379>hset myHash name xiangxue ---- Redis 127.0.0.1:6379>hget myHash name -- output: "xiangxue"Copy the code

The hash command hlen to retrieve the number of fields in the hash key

Redis 127.0.0.1:6379>hset myHash Age 20 -- Set the value of key to myHash key to age =20 redis 127.0.0.1:6379>hlen myHash -- output is 2, The number of fields whose key is myhash is 2Copy the code

The hexists command in redis to check whether the specified field in the specified key exists. The hexists command returns 1 if the field exists and 0 if the field does not exist

Redis 127.0.0.1:6379>hexists myhash name -- if 1 is returned, it exists redis 127.0.0.1:6379>hexists myhash name1 -- If 0 is returned, it does not existCopy the code

The hash delete command hdel in redis deletes one or more specified fields

Redis 127.0.0.1:6379>hset MyHash sex boy ---- Add data redis 127.0.0.1:6379>hset MyHash likes sports ---- add data redis 127.0.0.1:6379>hdel myHash likes ---- Delete a single data. Filed data for likesCopy the code

Hash in redis If the key or field does not exist, the insert is valid. Otherwise, no operation is taken

Redis 127.0.0.1:6379>hsetnx myHash sex boy redis 127.0.0.1:6379>hsetnx myHash sex boy Redis 127.0.0.1:6379>hget myHash sex -- print "boy"Copy the code

This command is used to add or subtract data when the hash value in redis is a numeric value

Redis 127.0.0.1:6379>del MyHash -- Delete the key redis 127.0.0.1:6379>hset MyHash Age 20 -- Set the key whose key is myhash to age and value to 20 redis 127.0.0.1:6379>hincrby myHash Age 10 Redis 127.0.0.1:6379>hincrby myHash age-10 -- Subtract 10 from the value of key myhash and key age to get 20Copy the code

Add key/value command hmset in batches

Redis 127.0.0.1:6379>hmset myHash name Enjoy Age 20 issingle yes ---- add multiple keys of name=enjoy, age=20, isSingle =yes to the hash whose key is myhashCopy the code

The command hmget to obtain keys/values in batches

Redis 127.0.0.1:6379>hmget myhash name age issingle ---- The command output is enjoy 20 yesCopy the code

The hgetall command to getall fields and values based on the myHash key

Redis 127.0.0.1:6379>hgetall myhash ---- The command output is name age issingle enjoy 20 yesCopy the code

Get the command hkeys for all fields

Redis 127.0.0.1:6379>hkeys myHash -- The output is name age issingleCopy the code

The command hVALS to get the values of all fields

Redis 127.0.0.1:6379>hvals myhash ---- The command output is: enjoy 20 yesCopy the code

1.5. Business design

In the shopping cart scenario, since you have chosen to hash, you can plan as follows

  1. Use the user ID as the key
  2. Take the commodity ID as the field
  3. Value is the quantity of the item

1.5.1. Add shopping cart items

Suppose now that the user ID is 1001, the user puts three items in the shopping cart, the product ID is 10021,10025,10079, and the corresponding instruction should be

hset cart:1001 10021 1

hset cart:1001 10025 1

hset cart:1001 10079 1
Copy the code

1.5.2. Full selection

Select all – getall shopping cart items for all users. This is easy to do, just use the hgetall directive.

Hgetall Cart :1001 this will list all the items in user 1001’s cart.

1.5.3. Quantity of goods

Number of items – The cart icon displays the total number of items in the cart. This is not too difficult, just use the hlen command.

Hlen CART :1001 displays 3, which shows the number of items in the cart.

1.5.4. Delete goods

Delete – Be able to remove an item from your cart.

Hdel Cart :1001 10079 Removes the item ID 10079 from the cart.

1.5.5. Add goods

Increase or decrease the quantity of a good.

Hincrby Cart :1001 10021 1 Increases the number of items in the cart with product ID 10021 by 1 with this command.

Designed this way, the shopping cart performs well and reduces database stress, which can be considered in a real project.

** Question: what is the persistence mechanism of redis?

Answer:

Redis supports two persistence mechanisms: AOF and RDB. By default, RDB is used. Aof needs to be enabled by modifying the configuration file. An RDB, also called a mirror file, stores a mirror image of redis memory data at a certain time. Aof is short for Append Only File. It stores log files, specifically RESP command logs, where every operation command to Redis is stored.Click on this to get a copy of Tencent’s Internal Redis Learning Notes!