当前位置:网站首页>Five basic types of redis
Five basic types of redis
2022-06-27 08:16:00 【Little moon 6】
redis Official website https://redis.io/commands All operation commands can be found

Redis Five basic types
Redis Five data types are supported :string( character string ),hash( Hash ),list( list ),set( aggregate ) And zset(sorted set: Ordered set )
1、String
String ( character string )
- string yes redis Most basic types , One individual key Corresponding to one value.
- string Type is binary safe . intend redis Of string Can contain any data . such as jpg Picture or serialized object .
- string The type is Redis Basic data types , One individual redis Middle string value At most 512M
Specific examples :
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> set k2 1234567
OK
127.0.0.1:6379> getrange k2 0 -1
"1234567"
127.0.0.1:6379> getrange k2 0 2
"123"
127.0.0.1:6379> setex k3 20 v3
OK
127.0.0.1:6379> ttl k3
(integer) 17
127.0.0.1:6379> ttl k3
(integer) 16
127.0.0.1:6379> get k3
"v3"
127.0.0.1:6379> ttl k3
(integer) 7
127.0.0.1:6379> get k3
(nil)
127.0.0.1:6379> ttl k3
(integer) -2
127.0.0.1:6379> setex k1 20
(error) ERR wrong number of arguments for 'setex' command
127.0.0.1:6379> setnx k4 v4
(integer) 1
127.0.0.1:6379> get k4
"v4"
127.0.0.1:6379> setnx k1 vvvv
(integer) 0
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379>
127.0.0.1:6379> setrange k2 2 x
(integer) 7
127.0.0.1:6379> get k2
"12x4567"
127.0.0.1:6379> clear
127.0.0.1:6379> mset k6 v6 k7 v7 k8 v8
OK
127.0.0.1:6379> mget k6 k7 k8
1) "v6"
2) "v7"
3) "v8"
127.0.0.1:6379> msetnx k8 v8 k9 v9
(integer) 0
127.0.0.1:6379> get k9
(nil)
127.0.0.1:6379> msetnx k9 v9 k10 v10
(integer) 1
127.0.0.1:6379> mget k9 k10
1) "v9"
2) "v10"
127.0.0.1:6379> psetex k11 1000 v11
OK
127.0.0.1:6379> get v11
(nil)
127.0.0.1:6379> get k11
(nil)
127.0.0.1:6379> set k12 12
OK
127.0.0.1:6379> incr k12
(integer) 13
127.0.0.1:6379> incr k12
(integer) 14
127.0.0.1:6379> incr k12
(integer) 15
127.0.0.1:6379> incrby k12 5
(integer) 20
127.0.0.1:6379> incrby k12 5
(integer) 25
127.0.0.1:6379> set key 19.2
OK
127.0.0.1:6379> incrbyfloat key 10
"29.2"
127.0.0.1:6379> incrbyfloat key 10
"39.2"
127.0.0.1:6379> decr k12
(integer) 24
127.0.0.1:6379> decr k12
(integer) 23
127.0.0.1:6379> clear
127.0.0.1:6379> decrby k12 2
(integer) 21
127.0.0.1:6379> decrby k12 2
(integer) 19
127.0.0.1:6379> append k2 hhhhh
(integer) 12
127.0.0.1:6379> get k2
"12x4567hhhhh"
127.0.0.1:6379> setrange k2 2 ppppp
(integer) 12
127.0.0.1:6379> get k2
"12ppppphhhhh"
127.0.0.1:6379>
// to k1 Set expiration time , front setex Set the expiration time when creating
127.0.0.1:6379> expire k1 20
(integer) 1
127.0.0.1:6379> ttl k1
(integer) 16
127.0.0.1:6379> ttl k1
(integer) 15
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> ttl k1
(integer) 1
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379>
2、Hash
Hash ( Hash )
- Redis hash It's a collection of key-value pairs .( for example :Student.name = xiaoming ,Student.age = 18 Student.gender = man)
- Redis hash It's a string Type of field and value Mapping table , hash Ideal for storing objects .( for example :Student.name = xiaoming ,Student.age = 18 Student.gender = man)
- similar Java Inside Map<String,Object>
127.0.0.1:6379[1]> hset student name xiaoming
(integer) 1
127.0.0.1:6379[1]> hget student name
"xiaoming"
127.0.0.1:6379[1]> hmset student age 18 gender man
OK
127.0.0.1:6379[1]> hget student name
"xiaoming"
127.0.0.1:6379[1]> hget student age
"18"
127.0.0.1:6379[1]> hmget student name age gender
1) "xiaoming"
2) "18"
3) "man"
127.0.0.1:6379[1]> hlen student
(integer) 3
127.0.0.1:6379[1]> hkeys student
1) "name"
2) "age"
3) "gender"
127.0.0.1:6379[1]> hexists student age
(integer) 1
127.0.0.1:6379[1]> hgetall student
1) "name"
2) "xiaoming"
3) "age"
4) "18"
5) "gender"
6) "man"
127.0.0.1:6379[1]> hincrby student age 2
(integer) 20
127.0.0.1:6379[1]> hincrby student age 2
(integer) 22
127.0.0.1:6379[1]> hset student english 98.5
(integer) 1
127.0.0.1:6379[1]> hincrbyfloat student english 1.0
"99.5"
127.0.0.1:6379[1]> hincrbyfloat student english 1.1
"100.6"
127.0.0.1:6379[1]> hsetnx student age 90
(integer) 0
127.0.0.1:6379[1]> hget student age
"22"
127.0.0.1:6379[1]> clear
127.0.0.1:6379[1]> hvals student
1) "xiaoming"
2) "22"
3) "man"
4) "100.6"
127.0.0.1:6379[1]> hdel student age
(integer) 1
127.0.0.1:6379[1]> hget student age
(nil)
127.0.0.1:6379[1]>
3、List
- Redis List is a simple list of strings , Sort by insertion order . You can add a header to the element guide list ( On the left ) Or tail ( On the right ).
- Its bottom layer is actually a linked list
| Serial number | Command and description |
|---|---|
| 1 | BLPOP key1 [key2 ] timeout Move out and get the first element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found . |
| 2 | BRPOP key1 [key2 ] timeout Move out and get the last element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found . |
| 3 | BRPOPLPUSH source destination timeout Pop up a value... From the list , Insert the pop-up element into another list and return it ; If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found . |
| 4 | LINDEX key index Get the elements in the list by index |
| 5 | LINSERT key BEFORE|AFTER pivot value Insert an element before or after a list element |
| 6 | LLEN key Get list length |
| 7 | LPOP key Move out and get the first element of the list |
| 8 | LPUSH key value1 [value2] Insert one or more values into the list header |
| 9 | LPUSHX key value Insert a value into the existing list header |
| 10 | LRANGE key start stop Get the elements in the specified range of the list |
| 11 | LREM key count value Remove list elements |
| 12 | LSET key index value Set the value of the list element through the index |
| 13 | LTRIM key start stop Trim a list (trim), That is to say , Let the list keep only the elements in the specified range , Elements that are not in the specified range will be removed . |
| 14 | RPOP key Remove the last element of the list , The return value is the removed element . |
| 15 | RPOPLPUSH source destination Remove the last element of the list , And add the element to another list and return |
| 16 | RPUSH key value1 [value2] Add one or more values... To the list |
| 17 | RPUSHX key value Add value to existing list |
127.0.0.1:6379[2]> lpush list01 1 2 3 4 5
(integer) 5
127.0.0.1:6379[2]> LRANGE list01 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379[2]> rpush list02 1 2 3 4 5
(integer) 5
127.0.0.1:6379[2]> LRANGE list02 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379[2]> LPOP list01
"5"
127.0.0.1:6379[2]> LPOP list02
"1"
127.0.0.1:6379[2]> RPOP list01
"1"
127.0.0.1:6379[2]> RPOP list02
"5"
127.0.0.1:6379[2]> LRANGE list01 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379[2]> LRANGE list02 0 -1
1) "2"
2) "3"
3) "4"
127.0.0.1:6379[2]> LINDEX list01 2
"2"
127.0.0.1:6379[2]> LLEN list01
(integer) 3
127.0.0.1:6379[2]> LPUSH list03 1 1 1 1 1 1 1
(integer) 7
127.0.0.1:6379[2]> LREM list03 3 1 // Delete three 1
(integer) 3
127.0.0.1:6379[2]> LRANGE list03 0 -1
1) "1"
2) "1"
3) "1"
4) "1"
127.0.0.1:6379[2]> RPUSH list04 0 1 2 3 4 5 6 7 8
(integer) 9
127.0.0.1:6379[2]> LRANGE list04 0 -1
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"
8) "7"
9) "8"
127.0.0.1:6379[2]> LTRIM list04 3 5 // Intercept 3 to 5 The data of
OK
127.0.0.1:6379[2]> LRANGE list04 0 -1
1) "3"
2) "4"
3) "5"
127.0.0.1:6379[2]>
127.0.0.1:6379[2]> LPUSH list05 1 2 3
(integer) 3
127.0.0.1:6379[2]> LRANGE list04 0 -1
1) "3"
2) "4"
3) "5"
127.0.0.1:6379[2]> LRANGE list05 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379[2]> RPOPLPUSH list04 list05
"5"
127.0.0.1:6379[2]> LRANGE list04 0 -1
1) "3"
2) "4"
127.0.0.1:6379[2]> LRANGE list05 0 -1
1) "5"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379[2]>
127.0.0.1:6379[2]> LRANGE list04 0 -1
1) "3"
2) "4"
127.0.0.1:6379[2]> lset list04 1 x
OK
127.0.0.1:6379[2]> LRANGE list04 0 -1
1) "3"
2) "x"
127.0.0.1:6379[2]>
127.0.0.1:6379[2]> LRANGE list05 0 -1
1) "5"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379[2]> LINSERT list05 before 3 java
(integer) 5
127.0.0.1:6379[2]> LINSERT list05 after 3 mysql
(integer) 6
127.0.0.1:6379[2]> LRANGE list05 0 -1
1) "5"
2) "java"
3) "3"
4) "mysql"
5) "2"
6) "1"
127.0.0.1:6379[2]>
Performance summary :
- It's a list of strings ,left、 right You can insert and add ;
- If the key doesn't exist , Create a new list ;
- If the key already exists , The new content ;
- If all values are removed , The corresponding bond disappears .
- The operation of linked list is very efficient both in head and tail , But if it is to operate on the intermediate elements , The efficiency is very poor .
4、Set
- Redis Of Set yes string Unordered collection of type . It's through HashTable Realized
- Reproduced in :https://www.runoob.com/redis/redis-sets.html
- Collection members are unique , This means that duplicate data cannot appear in the collection . And the above list Very close to , But it needs to be unique
- Redis The middle set is implemented through a hash table , So add the , Delete , The complexity of searching is O(1).
- The maximum number of members in the collection is 232 - 1 (4294967295, Each collection can store 40 More than 100 million members ).
| Serial number | Command and description |
|---|---|
| 1 | SADD key member1 [member2] Add one or more members to the collection |
| 2 | SCARD key Get the number of members of the collection |
| 3 | SDIFF key1 [key2] Returns the difference between the first set and the others . |
| 4 | SDIFFSTORE destination key1 [key2] Returns the difference set of a given set and stores it in destination in |
| 5 | SINTER key1 [key2] Returns the intersection of a given set |
| 6 | SINTERSTORE destination key1 [key2] Returns the intersection of a given set and stores it in destination in |
| 7 | SISMEMBER key member Judge member Is the element a collection key Members of |
| 8 | SMEMBERS key Returns all members of the collection |
| 9 | SMOVE source destination member take member Elements from source The assembly moves to destination aggregate |
| 10 | SPOP key Remove and return a random element from the collection |
| 11 | SRANDMEMBER key [count] Returns one or more random numbers in a set |
| 12 | SREM key member1 [member2] Remove one or more members of the collection |
| 13 | SUNION key1 [key2] Returns the union of all given sets |
| 14 | SUNIONSTORE destination key1 [key2] The union of all given sets is stored in destination Collection |
| 15 | SSCAN key cursor [MATCH pattern] [COUNT count] Iterate over the elements in the collection |
127.0.0.1:6379[3]> sadd set01 1 1 2 2 3 3
(integer) 3
(0.58s)
127.0.0.1:6379[3]> smembers set01
1) "1"
2) "2"
3) "3"
127.0.0.1:6379[3]> sismember set01 1
(integer) 1
127.0.0.1:6379[3]> sismember set01 x
(integer) 0
127.0.0.1:6379[3]> scard set01
(integer) 3
127.0.0.1:6379[3]> srem set01 1
(integer) 1
127.0.0.1:6379[3]> smembers set01
1) "2"
2) "3"
127.0.0.1:6379[3]> sadd set02 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379[3]> srandmember 3
(nil)
127.0.0.1:6379[3]> srandmember set02 3
1) "6"
2) "3"
3) "2"
127.0.0.1:6379[3]> srandmember set02 3
1) "6"
2) "3"
3) "9"
127.0.0.1:6379[3]> srandmember set02 3
1) "5"
2) "8"
3) "7"
127.0.0.1:6379[3]> spop set02 2
1) "9"
2) "6"
127.0.0.1:6379[3]> smembers set02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "7"
7) "8"
127.0.0.1:6379[3]>
127.0.0.1:6379[3]> sadd set03 x y z
(integer) 3
127.0.0.1:6379[3]> sadd set04 1 2 3
(integer) 3
127.0.0.1:6379[3]> smove set03 set02 x
(integer) 1
127.0.0.1:6379[3]> smembers set03
1) "y"
2) "z"
127.0.0.1:6379[3]> smembers set04
1) "1"
2) "2"
3) "3"
127.0.0.1:6379[3]>
127.0.0.1:6379[3]> sadd set5 1 2 3 4 5
(integer) 5
127.0.0.1:6379[3]> sadd set6 1 2 3 a b
(integer) 5
127.0.0.1:6379[3]> sdiff set5 set6
1) "4"
2) "5"
127.0.0.1:6379[3]> sinter set5 set6
1) "1"
2) "2"
3) "3"
127.0.0.1:6379[3]> sunion set5 set6
1) "a"
2) "1"
3) "b"
4) "3"
5) "5"
6) "4"
7) "2"
127.0.0.1:6379[3]>
5、zset(sorted set: Ordered set )
- Redis zset and set The same is true. string Collection of type elements , And duplicate members are not allowed .
- The difference is that each element is associated with a double Score of type .
- redis It's the scores that sort the members of a collection from small to large .zset Members of are unique , But fractions (score) But it can be repeated .
stay set On the basis of , Add one more score Before it's worth it set yes k1 v1 v2 v3 Now? zset yes k1 score1 v1 score2 v2
127.0.0.1:6379[3]> zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5
(integer) 5
127.0.0.1:6379[3]> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
127.0.0.1:6379[3]> zrange zset01 0 -1 withscores
1) "v1"
2) "60"
3) "v2"
4) "70"
5) "v3"
6) "80"
7) "v4"
8) "90"
9) "v5"
10) "100"
127.0.0.1:6379[3]> zrangebyscore zset01 60 90
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379[3]> zrangebyscore zset01 60 (90
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379[3]> zrangebyscore zset01 (60 90
1) "v2"
2) "v3"
3) "v4"
127.0.0.1:6379[3]> zrangebyscore zset01 (60 (90
1) "v2"
2) "v3"
127.0.0.1:6379[3]> zrangebyscore zset01 60 90 limit 2
(error) ERR syntax error
127.0.0.1:6379[3]> zrangebyscore zset01 60 90 limit 2 2
1) "v3"
2) "v4"
127.0.0.1:6379[3]>
127.0.0.1:6379[3]> zrem zset01 v5
(integer) 1
127.0.0.1:6379[3]> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379[3]> zcard zset01
(integer) 4
127.0.0.1:6379[3]> zcount zset01 60 80
(integer) 3
127.0.0.1:6379[3]> zrank zset01 v4
(integer) 3
127.0.0.1:6379[3]> zscore zset01 v4
"90"
127.0.0.1:6379[3]> zrevrank zset01 v4
(integer) 0
127.0.0.1:6379[3]> zrevrange zset01 0 -1 // with re It means in reverse order
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379[3]> zrevrangebyscore zset01 90 60
1) "v4"
2) "v3"
3) "v2"
4) "v1"
边栏推荐
- 淘宝虚拟产品开店教程之作图篇
- Ue5 magic power - POI solution
- 爬一个网页的所有导师信息
- MySQL锁详解
- (原创)自定义Drawable
- What are the specialties of database system engineers?
- [batch dos-cmd command - summary and summary] - output / display command - echo
- Binary tree structure and heap structure foundation
- March into machine learning -- Preface
- win命令行中导入、导出数据库相关表
猜你喜欢

Lvgl description 3 about the use of lvgl Guide

二叉树结构以及堆结构基础

2022爱分析· IT运维厂商全景报告

【批处理DOS-CMD命令-汇总和小结】-输出/显示命令——echo

「短视频」临夏消防救援支队开展消防安全培训授课

Implementation of game hexagon map

Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?

Associated GIS: all roads lead to ue5 City

Zabbix部署说明(Server+Win客户端+交换机(H3C))

"Short video" Linxia fire rescue detachment carries out fire safety training
随机推荐
JS to print prime numbers between 1-100 and calculate the total number of optimized versions
JS performance reward and punishment examples
Ue5 magic power - POI solution
Coggle 30 days of ML July competition learning
Mysql-8 download, installation and configuration tutorial under Windows
【c ++ primer 笔记】第4章 表达式
【11. 二维差分】
Zabbix部署说明(Server+Win客户端+交换机(H3C))
正确的理解MySQL的MVCC
Set the address book function to database maintenance, and add user name and password
【论文阅读】Intrinsically semi-supervised methods
【10. 差分】
How to view program running time (timer) in JS
lvgl 说明3关于lvgl guider的使用
100%弄明白5种IO模型
05 observer mode
关于放大器失真的原因你了解多少呢?
MySQL environment variable configuration tutorial
PayPal账户遭大规模冻结!跨境卖家如何自救?
期货反向跟单—交易员的培训问题