preface
Recently learned redis6.x, special notes to memo, and we learn together. The course is downloaded from the private school online, they put out the architect course, you can go to download and learn, free of charge, the address is sishuok.com/freecourses… The course is very good and worth learning! The key is no money, hee hee!
Introduction of Redis
Redis (REmote DIctionary Server)
Redis is a NoSQL database written in C, open source, key-value, memory based running and support persistence, is one of the most popular NoSQL database
Two basic ways to use Redis
One is to use it as a database cache and the other is to use it directly as a databaseCopy the code
Redis installation
IO/redis. IO/redis. IO/redis. IO
2: After decompressing, go to the decompressed folder
3: Redis installation is very simple, because there is already a ready-made Makefile, so directly make, then make install can be done
Note: GCC5.3 or above is required for compilation and installationCopy the code
4: The default installation location is /usr/local/bin.
Redis-check -aof: redis-check- RDB: redis-check-aof: redis-check- RDB: Redis-check -aof: redis-check Used to repair faulty RDB files (4) Redis -cli: Redis command line operation tool (5) Redis -sentinel: Redis cluster management tool (6) Redis -server: Redis server startup programCopy the code
5: Only one parameter is required to start Redis, that is, to specify the path of the configuration file Redis. Redis. conf is available in the unzip folder, copy it, modify it as needed, or use –port to specify the port
The following parameters are modified: #bind 127.0.0.1 # protect-mode yes #bind 127.0.0.1 # protect-mode yes Protected -mode yes # change this parameter, specify the data directory dir /mydata/redis6/data # change this parameter, Specify the log directory logfile /mydata/redis6/redis_6379.logCopy the code
6: Connect to Redis and operate using Redis -cli. If there are multiple instances, you can use Redis -cli -h server IP -p port -u user a password
7: shutdown Redis -cli shutdown. If multiple instances exist, you can specify a port to shutdown Redis -cli -p 6379 shutdown
Redis basics
Single threaded model
The Redis server program uses a single-threaded model to handle client requests. The response to events such as reads and writes is done by wrapping the epoll function.
The actual processing speed of Redis is entirely dependent on the execution efficiency of the main process. If multiple clients access the server concurrently, the processing capacity of the server will decrease under certain circumstances. If you want to increase the concurrency of your server, you can deploy multiple Redis processes on a single machine.
The I/O threads
Redis6 starts to support I/O multithreading.
One of the major performance bottlenecks in Redis is the read and write performance of I/O data. The “multi-threading” mechanism introduced in Redis6 is an optimization for this bottleneck.
The core idea is to split the I/O reading and writing tasks of the main thread into a group of independent threads, so that the reading and writing of multiple sockets can be parallelized.
Many database
1: Redis each database is named with an increasing number starting from 0. By default, there are 16 databases and the default database is 0. You can use the Select number to Select the database to use
2: use Dbsize to check the number of keys in the current database
3: can move data between multiple databases, use move key destination database number can be
4: Use flushdb to flush data from a database
5: Redis does not support custom database names
6: Redis does not support different access passwords for each database
7. Multiple databases are not completely independent from each other and can FlushAll data in FlushAll
Redis’s database is more like a namespace
The key of Redis
The key of Redis is a string. If there are Spaces or escape characters in the middle, use “”.
1: Naming suggestion: Object type: Object ID: Object property 2: Use ". "between multiple words. To separate 3: Key names, should be readable, as short as possibleCopy the code
Redis values support five types
1: String: a String that can store String, Integer, Float, and even binary data. The maximum capacity of a String is 512 MB
2: List: string List, the underlying implementation is not an array, but a linked List, that is, to insert a new element in the head and tail, its time complexity is constant level; The downside: element positioning is slower than array positioning
3: Set: the string Set, unordered and non-repeatable, is implemented using HashTable
4: Hash: Stores strings in Hash mode
5: ZSet: string Set, ordered and non-repeatable, sorted according to Score. The bottom layer is implemented using hash tables and skip tables, so reading the middle part of the data is also fast
Operation command on Key
1: Keys: obtain the list of key names in accordance with the rules in the format of Keys pattern, pattern support glob style wildcard format: (1)? Matches a character (2) * matches any character (3) [] Matches any character in parentheses, which can be represented by a range of - (4) \x Matches the character x, which is used to escape the symbol 2: exists: Determines whether a key exists in the format exists key 3: del: Delete key in the format del key. Tip: Del command does not support wildcards, can combine Linux pipeline and xargs command to customize deletion, the sample is as follows: redis - cli keys k * | xargs redis - cli Del 4: type: the data type of the key value obtained, format is type key 5: Rename: rename oldKey newKey. 6: renamenx: rename oldKey newKey if it does not exist. 7: randomkey: returns a randomkeyCopy the code
String operation
The Redis String can be a String, a number, or binary data (image, audio, video), but the value cannot exceed 512MB
Operation command on String
1: get,set, del: obtains the key value, sets the key and the value, and deletes the key. Similar set commands are as follows: setex,psetex,setnx 2: incr, decr: increment and decrement integer values, in the format of incr key value 3: incrby, decrby: Incrementing and decrement integer values, which can be specified in the format of incrby key value positive and negative values. 4: IncrbyFloat: incrementing and decrement floating point values. 5: Append: 6: getrange: obtains the value in the specified index range. The format is getrange Key value. Start index End index 7: setrange: obtains the value in the specified index range. Offset Index value 8: strlen: returns the length of the key value in the format of strlen key value 9: mget: obtains the value of multiple keys at the same time in the format of mget 10: mset: returns the value of multiple keys. 11: bitcount: retrieves the binary digits in the range of 1. The format is bitcount key [start end] 12: getbit: retrieves the binary digits in the range of 1. Getbit Key offset index 13: setbit: setbit key offset index 14: bitop: getbit key offset index 13: setbit key offset Operation purpose Key Key value 1 Key value 2 Operations include and, OR, xOR, and not. The key value can be multiple. 15: getSet: Atomic sets the value of the key and returns the old value of the key in the format getSet Key ValueCopy the code
Operation command on List type
1: lpush/rpush: add values in the form of the key item of the Rpush list. The values can be multiple. 2: lpushx/rpushx: append elements from left/right only if the list exists. Insert elements, format is linsert list of key before | after locating the value added of 4: lrange: according to get the value scope of index, format is lrange beginning a list of key index ending index, index of the last 5-1 said: lindex: Index number 6: LPOP/RPOP: pop-up value in the format of the key of the LPOP list 7: llEN: llen key of the llEN List 8: lrem: The value of the item can be positive or negative, indicating that it is deleted from the left or right. If the number is 0, it indicates that all items equal to the given value are deleted. The format is lTRIM list key start index End index 10: BLPOP/BRPOP: pop-up value. The format is blPOP list key expiration time. Rpoplpush: to transfer elements from one list to another in the format of rpoplpush, the key of the source list, the key of the destination list. Brpoplpoplpush the key of the source list. Expiration time of the destination list. 13: lSET: Sets the value of the specified indexCopy the code
Operation command on Set type
Sadd: an element is added in the format of the key item of the sadd set. Multiple items can be added. Smembers: an element is added in the format of the key item of the sadd set. Srem: delete the element. Srem: delete the element. Scard: obtain the number of elements in the set. Srandmember: Randomly fetchthe elements of the set in the form of the key of the srandMember set. If the number is positive, this number of non-repeating elements will be randomly fetched. If the number is greater than the number of elements in the set, return all; If the number is negative, this number of elements will be randomly obtained, and there may be duplicates 7: spop: pop-up element in the form of the key of the spop set 8: smove: move element in the form of the key of the source set 9: sdiff: The difference set, which returns an item in the first set but not in any subsequent set, in the form of the key of the sdiff set. 11: sinter: intersection of items that are returned in multiple sets. The format is sinter: keys of multiple sets. 12: Sinterstore: 13: sunion: sunion: key of multiple sets 14: sunionStore: Key of multiple sets Combine and retain the result in the form of the key of the set where sUnionStore stores the resultCopy the code
Hash operation command
1: hset: sets the value in the format of the Key value of the hset Hash Key. 2: hmset: sets the value in the format of the Key value of the Hset Hash Key. 3: hgetall: sets the value in the format of the Key value of the Hset Hash Key. Obtain all values of the Key in the format of hgeTall Hash Key. 4: hget: obtain the value in the format of hget Hash Key Key. 5: hmget: Obtain multiple values in the format of hmget Hash Key Key Key. Hdel: deletes an item. The format is the Key of the Key item of the Hdel Hash. 7: hlen: obtains the number of Key/value pairs in the Key. 10: hkeys: Obtain the keys of all items in the format of hkeys Hash 11: hvals: obtain the keys of all items in the format of hkeys Hash 12: Hincrby: adds or subtracts integer numbers. The format is the Key of the Hincrby Hash. Integer 13: hincrByFloat: adds or subtracts Float values. Hsetnx: assigns if the item does not exist, and does nothing if it does, in the form of the Key of the hsetnx HashCopy the code
ZSet operation
ZSet, like Set, is a collection of string elements and cannot be repeated. Instead, each element in a ZSet is associated with a number of type double, and members of the set are sorted by that number from smallest to largest
ZSet operation command
1: zadd: adds elements in the format of zadd key score value item of zset. Score and item can be multiple pairs. Score can be integer or floating point. Zrangebyscore = zrangeByScore = zrangeByScore = zrangeByScore Get the elements within the score range in the format of zrangebyscore. The key start score of zset and end score (withscores) are included by default. If "(" is added, it means not included; Zrem: delete elements in the format of zrem zset key, which can be multiple 5: zcard: obtain the number of elements in the set, in the format of zcard zset key 6: zincrby: 7: zcount: to obtain the number of elements in the Score range. The format is zcount: to obtain the key start Score and end Score 8: zrank: Zscore: returns the score of the key item in zset. Zscore: returns the score of the key item in zset. 10: zrevrank: Zrevrange: get the elements within the index range. Zrevrange: get the elements within the index range. Zrevrange: get the elements within the index range. Zrevrangebyscore: ZrevRangeByScore: zrevRangeByScore: Zpopmax: zpopMax: Eject the member with the highest score from the set, return that member and score, and then remove it from the set 14: zpopmin: eject the member with the lowest score from the set, return that member and score, and then remove it from the set 15: bzpopmax: blocks the connection if all sets in the argument are empty. If the parameter contains multiple ordered sets, the value of the member with the largest score in the first non-empty key and its corresponding score are returned in the order of the key in the parameter. The timeout parameter can be understood as the maximum number of seconds the client is blocked, with 0 indicating permanent blocking. 16: bzpopmin: blocks the connection if all sets in the argument are empty. If multiple ordered sets are included in the parameter, the member with the smallest score in the first non-empty key and its corresponding score are returned in the order of the key in the parameter. The timeout parameter can be understood as the maximum number of seconds the client is blocked, with 0 indicating permanent blocking. 17: zremrangeByrank: deletes elements in the index range in the format of zremrangeByrank zset key start index End index 18: zremrangeByScore: Score 19: zinterstore: intersection: zinterstore dest-key key-count key [key...] [WEIGHTS weight [weight...]] [AGGREGATE SUM | MIN | MAX] 20: zunionstore: and set, the format is zunionstore dest - key key - count the key [key]... [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]Copy the code
Sorting operation
1: sort: sort the values in List, Set, and ZSet. Format is to SORT the source – the key [BY the pattern] [LIMIT offset count] [GET pattern [GET pattern…]] [ASC | DESC] [ALPHA] [STORE dest – key]
2: by: specifies the reference key for sorting. It can be a string or an Item key in the Hash format :->Item key. With the BY reference key set, sort will no longer sort by the element’s value. Instead, for each element, sort will replace the first “” in the reference key with the element’s value, get the corresponding value, and sort the obtained value. If the reference key does not exist, the default is 0.
If the reference key is the same, sort by the value of the element itself.
Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash Hash If you want to return the value of an element, use get #.
4: Sorting a large amount of data will seriously affect the performance. Suggestions:
(1) Try to reduce the data in the set to be sorted
(2) Use limit to limit the amount of data obtained
(3) If the amount of data to be sorted is large, consider using the Store parameter to cache the results
Mechanism for handling expired keys
1: Delete periodically: Redis performs the following operations in the background 10 times per second by default: Randomly selects 100 keys to check whether they are expired. If more than 25 keys are expired, immediately selects the next 100 keys at random (not counted within 10 times). That is, if not many keys are out of date, Redis will recycle at most 200 or so keys per second, and if more than 25% of keys are out of date, it will do even more, so that data that is never accessed will be deleted even if it is out of date
2: Lazy deletion: When the client proactively accesses a key, the client checks that the key times out and deletes the outdated key immediately
The command to process expired keys
1: expire: sets the expiration time in the format of the expire key value in seconds. 2: expireat: sets the expiration time in the format of the expireat key value in seconds. 3: TTL: The format is TTL key. -1 indicates that the key will never expire and -2 indicates that the key will expire. 4: persist: Set the key to never expire and delete the key expiration setting in the format of persist key. In addition, the set or getset command also clears the expiration time of the key. 5: PTTL: checks the number of milliseconds to expire in the format of PTTL key value. 6: pEXPIRE: Sets the expiration time in the format of pEXPIRE key value. Set the expiration time in the format of the pexpireat key value to the millisecond timestampCopy the code
Afterword.
I will continue to take notes of my learning process of redis6.x and study with you.