web development source code, module notes that a qualified junior front-end engineer needs to master

Posted by Mce on Sun, 16 Jan 2022 19:04:38 +0100

Concept: Redis is an open source high-performance key value pair database developed in C language.

features:

  • There is no necessary connection between the data
  • The internal uses single thread mechanism to work
  • High performance
  • Multi data type support
    1. String type string
    2. List type list
    3. Hash type Map
    4. Collection type Set
    5. Ordered collection type SortedSet
  • Persistence support

Application scenario

  • Accelerate query for hot data; Such as hot goods, hot news, hot information and other high traffic information.
  • Task queue; Such as: second kill, rush purchase, ticket purchase, etc.
  • Instant information query; Such as: ranking list, etc.
  • Timeliness information control; Such as verification code, voting control, etc.
  • Distributed data sharing; For example, session in distributed architecture.
  • Message queue
  • Distributed lock

Basic data type: String

The storage space of a single string type is 512MB

string basic operation

  • Add or modify data
set key value
  • get data
get key
  • Delete data
del key
  • Set expiration time (default unit is seconds)
expire key second
  • Append information after initial value
append key value
  • Add / modify multiple data
mset key1 value1 key2 value2...
  • Get multiple data
mget key1 key2 ...
  • Get string length
strlen key
  • Set the lifecycle control lifecycle of the key
setex key seconds value           (second)
psetex key millisexxonds value     (MS)
  • key setting Convention

    It corresponds to the table - primary key - field in the database one by one

    Table namePrimary key namePrimary key valueField name
    eg1orderid443523454name
    eg2equireid435432543type
    eg3newsid45435454title

Basic data type: Hash

  • Storage requirements: arrange a series of stored data to facilitate management. Typical applications store object information
  • Storage structure: one storage space stores multiple key value pair data
  • Hash type: the bottom layer uses hash table structure to realize data storage

hash storage structure optimization

  • If the number of field s is small, the storage structure is optimized to class array structure
  • If the number of field s is large, the storage structure uses the HashMap structure

Basic operation of hash type

  • Add / modify data
hset key field value
  • get data
hget key field 
hgetall key
  • Delete data
hdel key field [field2 ...]
  • Add or delete multiple data
hmset key field1 value1 field2 value2 ...
  • Get multiple data
hmget key field1 field2 ...
  • Gets the number of fields in the hash table
hlen key
  • Gets whether the specified field exists in the hash table
hexists key field
  • Gets the field name or field value used in the hash table
hkeys key
hvalues key
  • Sets the numeric data of the specified field and increases the value of the specified range
hincrby key field increment
hincrbyfloat key field increment
  • If the field under the key value exists, no operation will be performed, and if it does not exist, it will be added
hsetnx key field value

Precautions for hash type data operation

  • The value under the hash type can only store strings. It is not allowed to store other data types. There is no nesting. If the data is not obtained, the corresponding value is (nil)

  • The upper limit of key value pairs stored in each hash is

    2 32 − 1 2^{32}-1 232−1

    Key value pairs

  • Hash type is very close to the storage form of objects, and object attributes can be deleted flexibly. However, hash is not designed to store a large number of objects. Remember not to abuse it, let alone use hash as an object list

  • hgetall operation can obtain all attributes. If there are too many internal field s, the overall data traversal efficiency will be very low, which may become a data access bottleneck

Application scenario

  • Design and implementation of shopping cart on e-commerce website

Basic data type: list

  • Data storage requirements: store multiple data and distinguish the key sequence of data entering the storage Jon
  • Required storage structure: one storage space stores multiple data, and the data can reflect the entry order
  • List type: save multiple data, and the bottom layer is realized by using the two-way linked list storage structure

The list type is stored in a two-way linked list

list basic operation

  • Add / modify data
lpush key value1 value2 [value3] ...   //Add from the left side of the list
rpush key value1 value2 [value3] ...   //Add from the right side of the list
  • get data
lrange key start stop       //Specify the value in the start and end position of the linked list
//When obtaining a list type of unknown length, if you want to view all value s, you can use - 1 to represent the penultimate lrange key start -1
lindex key index            //Gets the value of the specified position in the linked list
llen key                    //Get the length of the linked list
  • Get and remove data
lpop key
rpop key
  • Acquire and remove data within a specified time (blocking data acquisition)
blpop key1 [key2] timeout        //Take out and remove the value corresponding to the key value within the specified time. If the timeout is not taken out, a null value (nil) will be returned. If no other client added the value of the key while waiting, the operation task queue will be performed
brpop key1 [key2] timeout
  • Remove specified data
lrem key count value

Precautions for list type data operation

  • The data saved in the list is of string type, and the total data capacity is limited, with a maximum of 2 ^ 32-1 elements
  • list has the concept of index, but when operating data, it usually performs queue in and out operations in the form of queue, or stack in and out operations in the form of stack
  • The index to get all operation end data is set to - 1
  • List can perform paging operation on data. Usually, the information on the first page comes from list, and the second page and more data information are queried and loaded through the database

Basic data type: set

  • Storage requirements: store a large amount of data to provide higher efficiency in query
  • Storage structure: it can save a large amount of data and has an efficient internal storage mechanism to facilitate query
  • set type: exactly the same as the hash storage structure. Only keys are stored, no value (nil) is stored, and the value cannot be empty

Set storage structure is an unordered set of string type. Internal storage is a hash storage structure, so the complexity of adding, searching and deleting is O(1)

set basic operation

  • Add non duplicate data
sadd key value
  • Get all stored data
smembers key
  • Delete data
strem key member1 [member2]
  • Get total set data
scard key
  • Determines whether the set contains the specified data
sismember key member
  • Randomly gets the specified amount of data in the collection
srandmember key [count]
  • Randomly obtain a data in the set and move the data out of the set
apop key
  • Intersection, union and difference sets of two sets
sinter key1 [key2]
sunion key1 [key2]
adiff key1 [key2]
  • Find the intersection, union and difference sets of two sets and store them in the specified set
sinterstore destination key1 [key2]
sunionstore destination key1 [key2]
sdiffstore destination key1 [key2]
  • Moves the specified data from the original set to the target set
smove source destination member

matters needing attention

  • Duplicate data is not allowed for set type. If the added data already exists in set, only one copy will be retained

  • Although set has the same storage structure as hash, it cannot enable the space for storing values in hash

Basic data type: sortedSet

  • Storage requirements: data sorting is conducive to the display effect of data. It is necessary to provide a way to sort according to its own characteristics

  • Storage structure: you can save sorted data

  • Storage type: add sortable fields to the storage structure of set

basic operation

  • Add data
zadd key scorel member [score2 member2]
  • Get all data
zrange key start stop [witchscores]
zrevrange key star stop [witchscores]
  • Delete data
zrem key member [member ...]
  • Query data by criteria
zrangebyscore key min max [withscores] [limit]
zrevrangebyscore key max min [withscores]
  • Conditional delete data
zremrangebyrank key start stop   //start stop indicates the start and end position of the index
zremrangebyscore key min max     //min max indicates the minimum to maximum position of sorting
  • Get the number of collection data
zcard key
zcount key min max

member ...]

*   Query data by criteria

zrangebyscore key min max [withscores] [limit]
zrevrangebyscore key max min [withscores]

*   Conditional delete data

zremrangebyrank key start stop //start stop indicates the start and end position of the index
zremrangebyscore key min max //min max indicates the minimum to maximum position of the sort

*   Get the number of collection data

zcard key
zcount key min max

*   
[External chain picture transfer...(img-jOYBvAhQ-1627021539940)]


>**Conclusion:**Technology has no end and can't be learned. The most important thing is to live and not be bald. When starting with zero foundation, I read books or watch videos. I think adults, why do they have to do multiple-choice questions? Both. If you like reading, you can read. If you like watching videos, you can watch videos. The most important thing is that in the process of self-study, we must not aim high but practice low, put the learned technology into the project, solve problems, and then further refine our own technology.

Topics: Front-end Interview Programmer