Table of contents
1. Windows system installation
Five, five data structure types
5.5, Zset ordered collection type
1. What is Redis
Redis is an open source memory-based key-value pair database. Its main features and functions include:
1. Based on memory, the read and write speed is extremely fast, and it can handle a large number of read and write requests.
2. Support a variety of data structures, such as strings, hashes, lists, sets, ordered sets, etc., with rich data representation capabilities. 3. Support master-slave replication, providing data redundancy and fault recovery capabilities.
4. Persistence is supported, and memory data can be saved to disk.
5. Support transactions, you can execute multiple commands at one time.
6. Rich functions, which can be used in scenarios such as caching and message queues.
The main application scenarios include:
1. Cache common usage scenarios, such as caching query results, hot data, etc., which greatly reduces the database load.
2. Handle a large number of read and write requests, such as access statistics, message queues, etc.
3. Realization of functions such as leaderboards and counters.
4. pub/sub message subscription.
5. QUE planning task
6. Distributed locks, etc.
To sum up, Redis is an in-memory database with extremely high performance. It supports rich data structures and provides functions such as persistence and transactions. It is very suitable for scenarios such as caching and message queues, and is widely used in various large-scale systems. Its high performance and rich features make it one of the important choices for non-relational databases.
2. Install Redis
1. Windows system installation
The Redis official website has stopped maintaining the Windows version. I uploaded it directly to the cloud disk, and you can download it directly:
https://pan.baidu.com/s/1_2vMncYBVI3jKkh9II9Kag?pwd=8hjr
1. After decompression, double-click redis-server.exe to start the server
run successfully
2. Double-click redis-cli.exe to start the command window
In this way, Redis can be operated on Windows.
2. Linux system installation
You can directly read my blog, the steps are complete and detailed: Linux system installation and deployment Redis complete tutorial (detailed explanation with pictures and texts)
3. Redis common commands
1. Redis has 16 databases by default, switch to the second database
select 1
2. View the number of keys in the current database
DBSIZE
3. Set a key as username and value as mike's data
set username mike
4. Get the value whose key is username
get username
5. Get all keys
keys *
6. Clear the current database
flushdb
7. Clear all databases
flushall
Four, Redis basic commands
1. Query whether the key is username exists
exists username
2. Specify the key as username to move to the No. 1 database
move username 1
3. The specified key expires after username10s
expire username 10
4. Check how long the key is username and how long it will expire
ttl username
5. Check what type the key is username
type username
Five, five data structure types
5.1, String type
1. Set the value of key as name to htt
set name htt
2. Get the value whose key is name
get name
3. The splicing key is the value of name: httstudy
append name study
4. Get the length of the value whose key is name
strlen name
5. Set the key to the value of view plus 1
incr view
6. Set key to the value of view minus 1
decr view
7. Set the key to the value of view plus 10
incrby view 10
8. Set the key to the value of view minus 10
decrby view 10
9. Intercept the string whose subscript is between 0-3, for example: abcdef, abcd after interception
getrange name 0 3
10. Replace the string with the subscript 1, for example: abcdef, a000efg after replacement
setrange name 1 000
11. Set the value of key as name to hello, and it will expire after 10s
setex name 10 hello
12. If there is no title whose key is title, the value is set to redis. If it exists, the set fails
setnx title redis
13. Set multiple values at once
mset k1 v1 k2 v2 k3 v3
mset user:1:name htt user:1:age 2
14. Get multiple values at once
mget k1 k2 k3
mget user:1:name user:1:age
15. If k1 already exists, then k1 and k4 all fail to be set, refer to the atomic operation of the transaction
msetnx k1 v1 k4 v4
16. If there is no value whose key is username, return nil, and then set it in; if there is a value, get the original value and set a new value
getset username htt
It will be better understood by looking at the picture!
5.2, List collection type
1. Insert a value or multiple values into the head of the list
lpush list 1
2. Insert a value or multiple values at the end of the list
rpush list 4
3. Get the specific value through the interval
lrange list 0 -1
4. Remove the first element of the list: 3
lpop list
5. Remove the last element of the list: 4
rpop list
6. Obtain a value in the list by subscript
lindex list 0
7. Get the length of the list
llen list
8. Remove the specified number of values from the list set, remove one with a value of 2, and match exactly
lrem list 1 2
9. Intercept the element set between subscript 1 and subscript 2 in the list collection, and overwrite the original list collection
ltrim list 1 2
10. Update the value of subscript 0 in the list set to bbb, if the value of subscript 0 does not exist, an error will be reported
lset list 0 bbb
11. Insert a specific value before or after a specific element (default first)
linsert list BEFORE kkk aaa
linsert list AFTER kkk aaa
5.3, Set collection type
1. Add an element to the set collection
sadd set hello
2. View all elements in the set collection
smembers set
3. Check whether an element exists in the set collection
sismember set world
4. Randomly extract 1 element
srandmember set
5. Randomly extract 2 elements
srandmember set 2
6. Randomly delete an element in the set collection
spop set
7. Move the world element in the set collection to the set2 collection
smove set set2 world
8. Make the difference of the set2 set minus the set set
sdiff set2 set
9. The intersection of set and set2
sinter set set2
10. Set and set2 are combined and deduplicated
sunion set set2
5.4, Hash collection type
1. Store key-value pair data in the hash collection
hset hash username mike
2. Obtain data from the hash collection
hget hash username
3. Add multiple values to the hash collection at the same time
hmset hash username jack age 2
4. Obtain multiple values from the hash collection at the same time
hmget hash username age
5. Obtain all key-value pairs in the hash collection
hgetall hash
6. Delete the key field specified in the hash collection
hdel hash username
7. Get the length of the hash set
hlen hash
8. Determine whether the specified field exists in the hash set
hexists hash username
9. Get all the keys in the hash collection
hvals hash
10. Get all the values in the hash collection
hkeys hash
11. Specify the increment in the specified hash set
hincrby hash views 1
12. If it does not exist, set the value directly, if it exists, the setting fails
hsetnx hash password 123456
5.5, Zset ordered collection type
1. Add a value
zadd zset 1 first
2. Add multiple values
zadd zset 2 second 3 third 4 four
3. Get all elements in the zset collection
zrange zset 0 -1
4. Sort the elements in the zset collection from small to large, -inf: negative infinity, +inf: positive infinity
zrangebyscore zset -inf +inf
5. Sort from small to large and output key values
zrangebyscore zset -inf +inf withscores
6. Specify the range from negative infinity to 1
zrangebyscore zset -inf 1 withscores
7. Remove the specified elements in the zset collection
zrem zset four
8. View the number of elements in the zset collection
zcard zset
9. Invert the specified range
zrevrange zset 1 2
6. Summary
The above is the detailed note summary of the Redis introductory tutorial, and the follow-up blog homepage will continue to update the content about Redis!