redis data type
There are 5 basic data types and three special types in redis
String type
For example: distributed locks, session-related, verification code-related, count-related (clicks/reads/following) and other single values.
value assignment
Keywords: set get append strlen
127.0.0.1:6379> set stu1 zhangsan
OK
127.0.0.1:6379> get stu1
zhangsan
127.0.0.1:6379> append stu1 shigehaoren
19
127.0.0.1:6379> get stu1
zhangsanshigehaoren
127.0.0.1:6379> STRLEN stu1
19
Addition and subtraction
incr ement self-increment decr ement self-decrement
incrby fixed increment custom
decrby fixed decrement custom
##值++
incr key
##值--
decr key
##增长指定的长度
incrby key number
##减少指定的长度
decrby key number
127.0.0.1:6379> set fans 0
OK
127.0.0.1:6379> incr fans
1
127.0.0.1:6379> incr fans
2
127.0.0.1:6379> get fans
2
127.0.0.1:6379> decr fans
1
127.0.0.1:6379> decr fans
0
127.0.0.1:6379> decr fans
-1
127.0.0.1:6379> DECR fans
(integer) 0
127.0.0.1:6379> DECR fans
(integer) -1
127.0.0.1:6379> INCRBY stu1 100 ##值固定+N
(integer) 100
127.0.0.1:6379> INCRBY stu1 100
(integer) 200
127.0.0.1:6379> INCRBY stu1 100
(integer) 300
127.0.0.1:6379> DECRBY stu1 50 ##值固定-N
(integer) 250
127.0.0.1:6379> DECRBY stu1 50
(integer) 200
Range operation range
getrange key index1 index2 Get the current specified range if the maximum length index2 can be replaced with -1
127.0.0.1:6379> set content hellojiazongnihao
OK
127.0.0.1:6379> get content
"hellojiazongnihao"
127.0.0.1:6379> STRLEN content
(integer) 17
127.0.0.1:6379> GETRANGE content 5 11
"jiazong"
127.0.0.1:6379> get content
"hellojiazongnihao"
127.0.0.1:6379> GETRANGE content 0 -1 # 获取全部字符串
"hellojiazongnihao"
127.0.0.1:6379>
replace operation
setrange key offset index content ## offset offset specifies the position to start replacing
127.0.0.1:6379> GETRANGE content 0 -1
"hellojiazongnihao"
127.0.0.1:6379> SETRANGE content 5 sunzong #将sunzong覆盖jiazong
(integer) 17
127.0.0.1:6379> GETRANGE content 0 -1
"hellosunzongnihao"
127.0.0.1:6379> SETRANGE content 5 wang #将wang覆盖jiaz
(integer) 17
127.0.0.1:6379> GETRANGE content 0 -1
"hellowangongnihao"
127.0.0.1:6379>
judge whether there is
EXISTS t6 Judging whether the value exists
#set with expire #If it exists, set the disappearance time and information value – the order
setex key time v can increase the expiration time and re-specify a new value. If the current key does not exist, create a new key and value and specify
an expiration time
#如果一个值不想改变只想重新设定时间
127.0.0.1:6379> set message 1089
127.0.0.1:6379> expire message 300
#如果既想刷新时间又想改变值
127.0.0.1:6379> setex message 300 8888
#set if not expire #If it does not exist, a new one will be created by default, if it exists, do not operate/and judge whether it exists or not, and
further operations can be performed according to the results
setnx key v
# 如果值存在 ,不作处理
127.0.0.1:6379> setnx message ooooooooooooooo
(integer) 0
# 如果值不存在, 执行创建
127.0.0.1:6379> setnx mess ooooooooooooooo
(integer) 1
Batch value operations
more set
mset k1 v1 k2 v2
mget k1 k2
127.0.0.1:6379> mset b 123 c 123 d 123
OK
127.0.0.1:6379> get c
"123"
127.0.0.1:6379> mget a b c d
1) "123"
2) "123"
3) "123"
4) "123"
127.0.0.1:6379>
About object storage
#普通的json对象
{
id:1,name:zhangsan,age:40}
#redis存储的对象
{
class:[{
},{
}]}
{
id:1,name:zhangsan,age:40}
{
id:2,name:lisi,age:14}
Store this object in redis, key user Note: the string needs to remove the quotation marks
127.0.0.1:6379> set user {
id:1,name:"zhangsan",age:40}
Invalid argument(s)
127.0.0.1:6379> set user {
id:1,name:zhangsan,age:40}
OK
127.0.0.1:6379> get user
"{id:1,name:zhangsan,age:40}"
127.0.0.1:6379>
I hope to store another Li Si, and the key is called user, but it will be overwritten if used directly, and it is not convenient to get the value.
Suggestion: Combination key (the purpose of this combination key is to break up the attributes and values in the object, and read different values in redis by combining different keys) Method 1: It is possible to
read a certain object according to the ID later
127.0.0.1:6379> set user:1 {
id:1,name:zhangsan,age:40}
OK
127.0.0.1:6379> set user:2 {
id:2,name:lisi,age:14}
OK
127.0.0.1:6379> keys user*
1) "user:2"
2) "user:1"
127.0.0.1:6379> get user:2
"{id:2,name:lisi,age:14}"
127.0.0.1:6379>
Method 2: Query certain data through the object-id-attribute
set user:1:id 1
set user:1:name zhangsan
set user:1:age 14
set user:2:id 2
set user:2:name lisi
set user:2:age 14
# 查询所有
127.0.0.1:6379> keys user*
1) "user:2:age"
2) "user:1:age"
3) "user:1:id"
5.1.8 取值赋值操作
getset 先取值,再赋值
5.1.9 关于浮点类型的增减操作
incr decr 直接使用会报错
关键词: incrbyfloat 不支持直接+1-1操作 支持固定增减量操作
5.1.10 删除数据
del key ...
5.2 List列表类型
4) "user:2:id"
5) "user:1:name"
6) "user:2:name"
# 根据id查询
127.0.0.1:6379> keys user:1:*
1) "user:1:age"
2) "user:1:id"
3) "user:1:name"
# 根据具体条件查询
127.0.0.1:6379> get user:2:name
"lisi"
127.0.0.1:6379>
value assignment operation
getset takes value first, then assigns
127.0.0.1:6379> getset student zhangsan
(nil)
127.0.0.1:6379> getset student lisi
"zhangsan"
127.0.0.1:6379> getset student wangwu
"lisi"
127.0.0.1:6379> get student
"wangwu"
127.0.0.1:6379>
Addition and subtraction operations on floating-point types
incr decr will report an error if used directly
127.0.0.1:6379> incr price
(error) ERR value is not an integer or out of range
Keyword: incrbyfloat does not support direct +1-1 operations, supports fixed increment and decrement operations
127.0.0.1:6379> incrbyfloat price 9.9
"19.8"
delete data
del key …
127.0.0.1:6379> del ww
127.0.0.1:6379> del nn oo
List list type
Basic assignment and retrieval operations
Reserved case: navigation information storage
Default left insertion value: lpush l means list
right insertion value: rpush r means right
Query data: lrange key start end
get length llen key
127.0.0.1:6379> lpush stu zhangsan lisi
(integer) 2 #返回list长度
127.0.0.1:6379> lrange stu 0 -1 #取值
1) "lisi"
2) "zhangsan"
127.0.0.1:6379> lpush stu wangwu
(integer) 3
127.0.0.1:6379> lrange stu 0 -1
1) "wangwu"
2) "lisi"
3) "zhangsan"
127.0.0.1:6379> rpush stu zhaoliu # 结尾插入值
(integer) 4
127.0.0.1:6379> lrange stu 0 -1
1) "wangwu"
2) "lisi"
3) "zhangsan"
4) "zhaoliu"
127.0.0.1:6379> llen stu # 获取当前列表长度
Delete data operation of list structure
delete the entire list list
del key #这种是直接将整个列表都删除 del删除删除任意类型
Delete some data in the list
Keyword: pop Pop up and throw
Syntax:
lpop count Pop up on the left
# 没给count值,默认左 弹出1 返回的信息是弹出的值
127.0.0.1:6379> lpop stu
"lisi"
# count 提供值,可以弹出多个,返回值就是弹出的值得列表
127.0.0.1:6379> lpop stu 10
1) "zhangsan"
rpop count pop up on the right
127.0.0.1:6379> rpop stu
"zhangsan"
127.0.0.1:6379> rpop stu 1
1) "lisi"
127.0.0.1:6379>
Delete the specified value list remove
keyword: lrem key count value ---- Query and delete from the left by default
127.0.0.1:6379> lpush stu wangwu zhaoliu zhangsan lisi wangwu tianqi zhangsan
zhangsan
127.0.0.1:6379> lrem stu 2 zhangsan
# count正数 从左向右删除 count负数, 从右向左删除
127.0.0.1:6379> lrange stu 0 -1
Index related index
Get data through
index lindex key index
127.0.0.1:6379> lindex stu 1
To intercept a part of the value in the list
trim is to remove the leading and trailing spaces in Java and scripts, which itself means trimming and tidying up. In redis, the scope intercepts part of the value, and only
the obtained content is retained after the interception is completed.
ltrim key start end
127.0.0.1:6379> ltrim stu 1 4
replace value in list
lset
127.0.0.1:6379> lset stu 1 zhaoliu
other
Insert a value before a specified position (not an index)
Insert a value after a specified position (not an index)
linsert key before|after original value position new value
127.0.0.1:6379> linsert stu before zhaoliu lisi
127.0.0.1:6379> linsert stu after lisi wangwu
The exchange of two list values requires two lists, the last value in the first list is inserted into the first position in the second list
rpoplpush r pop l push (right pop left push) If list1 and list2 are the same, the last A value top
rpoplpush list1[first list] list2[second list]
127.0.0.1:6379> rpoplpush stu teacher
#置顶数据
127.0.0.1:6379> rpoplpush teacherteacher
set collection type
Features: Unordered, cannot repeat all commands.
Uses at the beginning of s: Like, check-in, like and other functions, lottery function
basic operation
Pass the test: Duplicate values cannot be added successfully, and the value order is not fixed
Add value: sadd key v...
127.0.0.1:6379> sadd book sanguoyanyi jinpingmei hongloumeng
Query value: smembers members member composition
127.0.0.1:6379> smembers book
Universal delete del key Delete the entire collection directly
Delete the specified element srem key value
127.0.0.1:6379> srem book jinpingmei
other operations
Determine whether the specified value s is member exists in the current collection, whether it is a member
Syntax sismember key v
127.0.0.1:6379> sismember book jinpingmei
(integer) 1
127.0.0.1:6379> sismember book jinpingmei2
(integer) 0
View the number of values in the current collection s card
Syntax: sacrd key
127.0.0.1:6379> scard book
(integer) 4
Random acquisition and random deletion
Random acquisition: srandmember [ s random member ]
Syntax: srandmember key count
127.0.0.1:6379> srandmember book
"jinpingmei"
127.0.0.1:6379> srandmember book 2
1) "hongloumeng"
2) "jinpingmei"
Delete randomly: spop
Syntax: spop key count
127.0.0.1:6379> spop book
"jinpingmei"
127.0.0.1:6379> spop book 10
1) "hongloumeng"
2) "sanguoyanyi"
special operation
Solve Chinese problems
[root@lxip ~]# redis-cli -h 127.0.0.1 -p 6379 --raw
difference set diff The difference only returns the difference of the first set
127.0.0.1:6379> sadd zb1 xiaozhi dasima caixukun lijiaqi
4
127.0.0.1:6379> sadd zb2 liziqi xiaozhi dasima wangdaxian xuxubaobao
5
127.0.0.1:6379> sdiff zb1 zb2
caixukun
lijiaqi
127.0.0.1:6379> sdiff zb2 zb1
wangdaxian
liziqi
xuxubaobao
127.0.0.1:6379>
intersection sinter intersection
127.0.0.1:6379> sinter zb2 zb1
dasima
xiaozhi
And set s union union ---- to repeat
127.0.0.1:6379> sunion zb1 zb2
lijiaqi
dasima
xuxubaobao
xiaozhi
caixukun
wangdaxian
liziqi
Hash type (map structure)
key-value (map) is more suitable for object type data storage key - (key-value)
basic grammar
Store value (key (key - value))
syntax:
hset key k1 v1 k2 v2 k3 v3
hget key k – get a single value
# book redis的key name 值得key pingandeshijie 值中的值 (key key value)
127.0.0.1:6379> hset book name pingandeshijie
127.0.0.1:6379> hget book name
pingandeshijie
127.0.0.1:6379> hset book name pingandeshijie price 30 author luyao
# 如果存储对象
127.0.0.1:6379> hset student name zhangsan age 19 hobbies chouyanhejiutangtou
# 如果存储对象集合
{
id:1,name:zhangsan}
{
id:2,name:lisi}
127.0.0.1:6379> hset student 1 {
id:1,name:zhangsan} 2 {
id:2,name:lisi}
127.0.0.1:6379> hget student 2
hmget name kkk #get multiple keys
127.0.0.1:6379> hmget student 1 2
Get all values hgetall name k
127.0.0.1:6379> hgetall student
1
{
id:1,name:zhangsan}
2
{
id:2,name:lisi}
Delete all data by key - generic
127.0.0.1:6379> del student
Delete the specified data by the key of the value
127.0.0.1:6379> hdel student 2
1
Read the number of data in the current hash table (map collection)
127.0.0.1:6379> hlen student
other usage
judge whether there is
127.0.0.1:6379> hexists student 10
0
127.0.0.1:6379> hexists student 3
1
Read all the keys ----- 1w pieces of data to get some specific data, get all the keys, and cycle through the value judgment and screening.
127.0.0.1:6379> hkeys student
1
3
2
Take all the values — Case: In Java, I want to convert the hash structure in redis into a set structure
127.0.0.1:6379> hvals student
{
id:1,name:zhangsan}
{
id:1,name:zhangsan}
{
id:2,name:lisi}
Set
Ordered non-repeatable set, add a value set k1 * v1 (zset k1 sorted value (score) v1) on the basis of set The main usage direction
of the ordered and non-repeatable set set
: salary, class grades, etc. data. Or weight treatment 0 normal 1 important
basic grammar
# emps k
{
id:1,name:zhangsan,sal:8000}
{
id:2,name:lisi,sal:5000}
{
id:3,name:wangwu,sal:12000}
#增加数据
127.0.0.1:6379> zadd emps 8000 {
id:1,name:zhangsan,sal:8000}
1
127.0.0.1:6379> zadd emps 5000 {
id:2,name:lisi,sal:5000}
1
127.0.0.1:6379> zadd emps 12000 {
id:3,name:wangwu,sal:12000}
1
127.0.0.1:6379> zrange emps 0 -1 #查询 - 默认升序
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
127.0.0.1:6379> zrandmember emps 1 #随机读取
{
id:1,name:zhangsan,sal:8000}
Delete data
Delete the entire key del key
zrem k v
127.0.0.1:6379> zrem emps {
id:2,name:lisi,sal:5000}
1
zset complex query
#Display all information, from small to large (range) -inf negative infinity+inf positive infinity
Syntax: ** z range by score** key min max — min max uses the value of the sort field
127.0.0.1:6379> zrangebyscore emps 8000 10000
{
id:1,name:zhangsan,sal:8000}
127.0.0.1:6379> zrangebyscore emps -inf +inf
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
Ascending operation
# 默认取所有数据
127.0.0.1:6379> zrange emps 0 -1
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
# 根据当前排序结果取所有数据
127.0.0.1:6379> zrangebyscore emps -inf +inf
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
# 根据当前排序结果取所有数据 并显示排序字段
127.0.0.1:6379> zrangebyscore emps -inf +inf withscores
{
id:2,name:lisi,sal:5000}
5000
{
id:1,name:zhangsan,sal:8000}
8000
{
id:3,name:wangwu,sal:12000}
12000
# 根据当前排序结果取所有数据 并显示排序字段 并截取某一部分值
127.0.0.1:6379> zrangebyscore emps -inf +inf withscores limit 0 1
{
id:2,name:lisi,sal:5000}
5000
# 取排序字段值大于**的数据
127.0.0.1:6379> zrangebyscore emps 8000 +inf
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
# 取排序字段值小于**的数据
127.0.0.1:6379> zrangebyscore emps -inf 8000
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
127.0.0.1:6379>
The descending operation
z rev range k start stop start stop takes the range of the index.
127.0.0.1:6379> zrange emps 0 -1
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
127.0.0.1:6379> zrangebyscore emps -inf +inf
{
id:2,name:lisi,sal:5000}
{
id:1,name:zhangsan,sal:8000}
{
id:3,name:wangwu,sal:12000}
127.0.0.1:6379> zrevrange emps 0 -1
{
id:3,name:wangwu,sal:12000}
{
id:1,name:zhangsan,sal:8000}
{
id:2,name:lisi,sal:5000}
127.0.0.1:6379> zrevrangebyscore emps +inf -inf
{
id:3,name:wangwu,sal:12000}
{
id:1,name:zhangsan,sal:8000}
{
id:2,name:lisi,sal:5000}
127.0.0.1:6379> zrevrangebyscore emps +inf -inf withscores
{
id:3,name:wangwu,sal:12000}
12000
{
id:1,name:zhangsan,sal:8000}
8000
{
id:2,name:lisi,sal:5000}
5000
127.0.0.1:6379> zrevrangebyscore emps +inf -inf withscores limit 0 1
{
id:3,name:wangwu,sal:12000}
12000
127.0.0.1:6379> zrevrangebyscore emps +inf 8000
{
id:3,name:wangwu,sal:12000}
{
id:1,name:zhangsan,sal:8000}
127.0.0.1:6379> zrevrangebyscore emps 8000 -inf
{
id:1,name:zhangsan,sal:8000}
{
id:2,name:lisi,sal:5000}
127.0.0.1:6379>
geospatial geographic location
Functions: Moments positioning, nearby people, taxi distance calculation.
Add geographic location information GEOADD
geoadd key precision latitude name
# maps
127.0.0.1:6379> geoadd maps 116.22518499110029 39.951383565611934 xuejiaao
127.0.0.1:6379> geoadd maps 116.22119922850416 39.95185236700199 hongyun
127.0.0.1:6379> geoadd maps 116.32169891808317 39.894741892979205 beijingxizhan
127.0.0.1:6379> geoadd maps 116.77067924950407 39.830968132325836 huanhuxiaozhan
127.0.0.1:6379> geoadd maps 116.69798065636442 39.52723389254955 langfang
127.0.0.1:6379> geoadd maps 117.20129181359098 39.083057286402244 tianjing
127.0.0.1:6379> geoadd maps 121.47772003624723 31.19857972418721 shanghai
127.0.0.1:6379> geoadd maps 121.52029205773161 25.050634672686915 taiwan
Query the latitude and longitude GEOPOS of the specified location
127.0.0.1:6379> geopos maps taiwan
121.52029305696487427
25.05063527678047564
127.0.0.1:6379> geopos maps langfang
116.69798165559768677
39.52723385909389719
127.0.0.1:6379>
Query the distance between two locations GEODIST
geodist key location 1 location 2 unit (default meter)
127.0.0.1:6379> geodist maps xuejiaao hongyun M
343.8236
127.0.0.1:6379> geodist maps xuejiaao hongyun km
0.3438
127.0.0.1:6379> geodist maps beijingxizhan shanghai
1073394.0301
127.0.0.1:6379> geodist maps beijingxizhan shanghai km
1073.3940
127.0.0.1:6379>
Find nearby cities (location) GEORADIUS
127.0.0.1:6379> georadius maps 116.22518499110029 39.951383565611934 1500 KM
Query nearby cities (location) – display the distance to the middle GEORADIUS
Keyword: withdist
127.0.0.1:6379> georadius maps 116.22518499110029 39.951383565611934 500 KM withdist
langfang
62.1354
tianjing
127.8360
huanhuxiaozhan
48.4408
beijingxizhan
10.3666
hongyun
0.3440
xuejiaao
0.0002
Query nearby cities (positioning) – display other people’s precise information GEORADIUS
Keywords: withcoord
127.0.0.1:6379> georadius maps 116.22518499110029 39.951383565611934 500 KM withdist
withcoord
langfang
62.1354
116.69798165559768677
39.52723385909389719
tianjing
127.8360
117.20129281282424927
39.08305692729481251
huanhuxiaozhan
48.4408
Query nearby cities (positioning) – display the precise information of others and specify the number of returned GEORADIUS
keywords: count
127.0.0.1:6379> georadius maps 116.22518499110029 39.951383565611934 500 KM withdist
withcoord count 1
xuejiaao
0.0002
116.22518330812454224
39.95138395901303596
GEO RADIUS BY MEMBER Locating information by elements (not coordinates)
127.0.0.1:6379> georadiusbymember maps hongyun 10 KM
hongyun
xuejiaao
Geohash returns the hash value to convert the two-dimensional longitude and latitude into a one-dimensional string. The closer the string is, the closer the distance is.
127.0.0.1:6379> geohash maps hongyun
wx4ek9j8qj0
127.0.0.1:6379> geohash maps xuejiaao
wx4ekbbtej0
127.0.0.1:6379> geohash maps shanghai
wtw3kr92uh0
HyperLogLog cardinality statistics
What is cardinality?
For example, if the data set is {1, 3, 5, 7, 5, 7, 8}, then the cardinality set of this data set is {1, 3, 5, 7, 8}, and the cardinality repeating elements are 5 (each number).
Cardinality (not estimating is to quickly calculate the cardinality within the acceptable range of error.
Case: The number of webpage visits (a person visits a webpage multiple times but only counts as one person when recording)
this method can actually be implemented using the set method, but the set method will store a large number of user IDs and will consume more memory , it is more troublesome
, and our purpose is to count, so set is somewhat insufficient.
Add basic data
127.0.0.1:6379> pfadd clazz zhangsan lisi wangwu zhaoliu tianqi 99 2111 99.9
1
127.0.0.1:6379> pfadd clazz zhangsan lisi wangwu zhaoliu
0
127.0.0.1:6379> pfadd clazz zhangsan lisi wangwu zhaoliu 33
1
get total
127.0.0.1:6379> pfcount clazz
11
Merge – the rule merges the following data into the first specified key – after merging, it also de-duplicates by default.
127.0.0.1:6379> pfadd stu xiongda xionger zhangsan
1
127.0.0.1:6379> pfmerge clazz stu
OK
127.0.0.1:6379> pfcount clazz
11
127.0.0.1:6379> pfcount stu
3
Bitmaps
1Byte=8bit(bit)
1KB=1024Byte(byte)=8*1024bit
1MB=1024KB
1GB=1024MB
1TB=1024GB
1PB=1024TB
First increase the basic data setbit
127.0.0.1:6379> setbit kq 1 1
0
127.0.0.1:6379> setbit kq 2 1
0
127.0.0.1:6379> setbit kq 2 0
1
127.0.0.1:6379> setbit kq 3 1
0
127.0.0.1:6379> setbit kq 4 1
0
127.0.0.1:6379> setbit kq 5 0
0
127.0.0.1:6379> setbit kq 6 0
0
127.0.0.1:6379> setbit kq 7 0
0
127.0.0.1:6379>
View the check-in records of a certain day getbit
127.0.0.1:6379> get kq
X
127.0.0.1:6379> getbit kq 1
1
127.0.0.1:6379> getbit kq 6
0
Count the number of days clocked in bitcount
127.0.0.1:6379> bitcount kq
3
127.0.0.1:6379>