“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Basic concepts of Redis
Redis default 16 database memory based key-value database support data persistence based on C language, can support a variety of language API write a maximum of 110,000 times per second, read 81000 times command is case-insensitive default port: 6379
What is a redis
Redis (Remote Dictionary service) is an open source, network-enabled, memory – and persistent-based, logging, “key-value” database written in ANSI C, and provides multiple language apis. Redis periodically writes updated data to disk or changes to an appended record file.
To put it simply, we can regard Redis as a Map, and we need to obtain the Value of any data through Key. Value supports data types such as String, List, Set, ZSet, Hash, Geo, HyperLogLog and Bitmaps
Is Redis single-threaded or multi-threaded?
Redis is single-threaded (multi-threading is also supported after Redis 6.0)
Redis: Why is single threading so fast?
1, high-performance server is not necessarily multithreaded fast! The CPU is not the bottleneck of Redis because it is a memory-based operation. The bottleneck in Redis is likely to be the size of the machine’s memory or network bandwidth. Since single-threading is easy to implement and the CPU is not a bottleneck, it is logical to adopt a single-threaded solution.
Operate redis basic commands
Switching database: Select serial number + switch database insert data set to get the data get keys command to view all of the key expire key time (in seconds) is set a key expiration time | | time remaining TTL key check some key expiration time exists the key Flushdb Flushes all databases in flushall. Flushdb flushes all databases in flushall
Redis common data type operations
String
• Append key + append field adds a String to the end of the String. If there is no String key, Set • strlen length of the query string • Incr value +1 • DECr value -1 • Incrby key + Value How much to increase the key • Decrby key + Value how much to decrease the key • Getrange key + start subscript + end subscript (intercepts all strings if the end value is -1) • **setnx key **+ time (s) + set value another way to use setnx key + value, if the current key does not exist then create a new one, If there is a set failure • mset key1 value1 key2 value2 batch set • mget key1 key2 mset user:1:name zhangsan user:1:age 2(user:{id}:{filed})\
Note:
Both mset and MGET are atomic operations that either succeed or fail together
String usage scenarios:
A. counter B. number of multiple units c. Number of fans object cache storage
List
• In Redis, we can play lists as stacks, queues, and blocking queues! • Lpush list +key inserts one or more values into the head of a list (left) similar to a bidirectional list • Rpush list +key inserts one or more values, • lPOP list removes the first element of the list • rPOP List removes the last element of the list • lindex list + subscript get a value in the list by subscript! • llen List returns the length of the list • lrange list + number of removed keys + Removed keys Remove a specified number of values from the list set, matching exactly • ltrim List +start +end truncates the specified length by subscript, • rpoplpush removes the last element of the list and adds a new element ○ or rpoplpush + the removed list + the moved list # Removes the last element of the list, Move it to a new list • exists + list # Determine if the list exists \
If the key does not exist, create a new list. If the key does exist, add new content. If all values are removed, the list does not exist. Insert or change values on both sides for maximum efficiency! The middle element, which is less efficient, can be used as a message queue: lpush inserts from the left for each insert, rpush reads from the right for each read, or as a stack, first in, last out, last in, first out
Set
• all commands start with s • sadd set + element # add element to set • smember +set # Check the value of set • sismember +set +set • srandMember +set # Randomly select an element + number # randomly select a specified number of elements