[database] redis (2) — common data types and commands of redis

Time:2021-4-3

1. Classification of main data types of redis

There are five data types commonly used to store data in redis: string, list, set, sorted set and hash. It is very important for us to master the command of these five data structures to store data in redis for us to understand the API of operating redis using Java framework encapsulation class later. So here we summarize these five data structures one by one, and also briefly introduce the three types of bitmaps, hyperlogs and geospatial.

 

1.1.string type

Common operations of string type in redis:

① Get / set / exists / append / strlen command:

127.0.0.1:6379 > set key1 V1 # set value
OK 
127.0.0.1:6379 > get key1 # get value
"v1"
127.0.0.1:6379 > keys * # get all the values
1) "key1"
127.0.0.1:6379 > exists key1 # judge whether key1 exists or not
(integer) 1
127.0.0.1:6379 > append key1 "hello" ා if key1 does not exist, it is equivalent to set key1
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379 > strlen key1 # get the length of the string
(integer) 7
127.0.0.1:6379> APPEND key1 ", fengye"
(integer) 15
127.0.0.1:6379> STRLEN key1
(integer) 15
127.0.0.1:6379> get key1
"v1hello, fengye"

② Auto increment / decrement, step size (Views, hits)

#+ 1 / - 1 and + n / - n127.0.0.1:6379 > set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379 > incr views # self increasing + 1
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379 > decr views # - 1
(integer) 1
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> decr views
(integer) -1
127.0.0.1:6379> get views
"-1"
127.0.0.1:6379 > incrby views 10 # increase n
(integer) 9
127.0.0.1:6379> INCRBY views 10
(integer) 19
127.0.0.1:6379 > decrby views 5 # n
(integer) 14

③ String interception:

127.0.0.1:6379> set key1 "hello, fengye"
OK
127.0.0.1:6379> get key1
"hello, fengye"
127.0.0.1:6379 > getrange key1.04 # intercepts the string of the closed interval [0,4]
"hello"
127.0.0.1:6379 > getrange key1 0 - 1 ා intercepts all strings, which is the same as get key1
"hello, fengye"

④ Replacement:

127.0.0.1:6379> set key2 abcddefg
OK
127.0.0.1:6379> get key2
"abcddefg"
127.0.0.1:6379 > setrange key2 2 XXX # replaces the string starting at the specified position
(integer) 8
127.0.0.1:6379> get key2
"abXXXefg"

⑤ Setex (set with expire) and setnx (set if not exist)

#Setex: set the expiration time # setnx: the value will be set only when there is no current key. 127.0.0.1:6379 > setex Key3 30 "hello" is often used in distributed locks to set the expiration time to 30s, and it will expire after 30s
OK
127.0.0.1:6379> ttl key3
(integer) 21
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379 > setnx MyKey "redis" ා set the value of MyKey to redis. When MyKey does not exist, it is set successfully
(integer) 1
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
3) "mykey"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379 > setnx MyKey "mongodb"? When MyKey already exists, setting MyKey will fail
(integer) 0
127.0.0.1:6379> get mykey
"redis"

⑥ Set multiple values Mset / mget / msetnx at the same time:

127.0.0.1:6379 > Mset K1 V1 K2 V2 K3 V3 ා setting multiple values at the same time
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379 > mget K1 K2 K3 # get multiple values at the same time
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379 > msetnx K1 Y1 K4 V4 # get multiple values at the same time, msetnx is an atomic operation, either succeed together or fail together (integer) 0 127.0.0.1:6379 > get K4 (NIL)

⑦ Set the value of the object:

127.0.0.1:6379> set  user:2  { name:lisi , age:3 }Use JSON string to save the value of user
OK
127.0.0.1:6379> get user:2
"{name:lisi,age:3}"
127.0.0.1:6379> mset  user:1 :name zhangsan  user:1 : age 2 # use user:1 : name and user1:1: age to save the value, user: {ID}: {field} as the key to save the value
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"

⑧ GetSet (get first and set later)

127.0.0.1:6379 > GetSet DB redis # if there is no value, return nil and set the initial value
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379 > GetSet DB mongodb # if there is a value, return that value and set a new value
"redis"
127.0.0.1:6379> get db
"mongodb"

 

1.2.list type

① Lpush / rpush / lrange:

127.0.0.1:6379 > lpush list one # put one or more values at the head of the list (left push)
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 1
1) "three"
2) "two"
127.0.0.1:6379 > rpush list four # put one or more values at the end of the list (right push)
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"

②lpop/rpop:

127.0.0.1:6379 > lpop list # remove an element from the list
"three"
127.0.0.1:6379 > rpop list # move an element in the list right
"four"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"

③ Lindex (short for list index)

127.0.0.1:6379 > lindex list 1 # obtain a value in the list by subscript
"one"
127.0.0.1:6379> LINDEX list 0
"two"

④llen:

127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379 > llen list # returns the length of the list
(integer) 3

⑤Lrem:

127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379 > lrem list 1 one # removes the specified number of elements in the list set, exactly matches the number and removes them
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrem list 1 three
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"

⑥Lrem:

127.0.0.1:6379> Rpush mylist "hello"
(integer) 1
127.0.0.1:6379> Rpush mylist "hello1"
(integer) 2
127.0.0.1:6379> Rpush mylist "hello2"
(integer) 3
127.0.0.1:6379> Rpush mylist "hello3"
(integer) 4
127.0.0.1:6379 > ltrim mylist 1 2 # intercepts the specified length by subscript. After intercepting, the list will be changed, leaving only the intercepted elements
OK
127.0.0.1:6379> LRANGE list 0 -1
(empty list or set)
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"

⑦rpoplpush:

127.0.0.1:6379> rpush mylist "hello"
(integer) 1
127.0.0.1:6379> rpush mylist "hello1"
(integer) 2
127.0.0.1:6379> rpush mylist "hello2"
(integer) 3
127.0.0.1:6379 > rpopplbush mylist myotherlist ා right click the stack to remove the last element in the list and put it into a new list element
"hello2"
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "hello1"
127.0.0.1:6379> lrange myotherlist 0 -1
1) "hello2"

⑧lset:

Lset: replace one element in the list with another element to update

127.0.0.1:6379> EXISTS list
(integer) 0
127.0.0.1:6379 > lset list 0 item # judge whether the list exists, if not, the current update will report an error
(error) ERR no such key
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> LRANGE list 0 0
1) "value1"
127.0.0.1:6379 > lset list 0 item # if there are elements in the list, you can update the elements in the current list
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
127.0.0.1:6379 > lset list 1 other # if the update range exceeds the limit, an exception will be thrown
(error) ERR index out of range

⑨Linsert:

Linsert inserts a specific value before or after an element in the list

127.0.0.1:6379> Rpush mylist "hello"
(integer) 1
127.0.0.1:6379> Rpush mylist "world"
(integer) 2
127.0.0.1:6379> LINSERT mylist before "world" "other"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
127.0.0.1:6379> LINSERT mylist after "world" "new"
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"

 

1.3.set type

The elements in the set set in redis cannot be duplicated.

①sadd/smembers/sismember/scard

127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "fengye"
(integer) 1
127.0.0.1:6379> sadd myset "love fengye"
(integer) 1
127.0.0.1:6379 > members myset # view all the values in myset
1) "fengye"
2) "love fengye"
3) "Hello" 127.0.0.1:6379 > sismember myset Hello # judge whether a value is in the set
(integer) 1
127.0.0.1:6379> SISMEMBER myset world
(integer) 0

127.0.0.1:6379 > scar myset # get the number of content elements in the set
  (integer) 4

②srem

127.0.0.1:6379> SMEMBERS myset
1) "fengye"
2) "lovekuangshen2"
3) "love fengye"
4) "hello"
127.0.0.1:6379 > SREM myset lovekuangshen2 # remove an element
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "fengye"
2) "love fengye"
3) "hello"

③srandmember

127.0.0.1:6379> SRANDMEMBER myset
"hello"
127.0.0.1:6379> SRANDMEMBER myset
"love fengye"
127.0.0.1:6379> SRANDMEMBER myset
"fengye"
127.0.0.1:6379 > srandmember myset 2 # randomly select a specified number of elements
1) "hello"
2) "love fengye"

④spop

127.0.0.1:6379 > pop myset # randomly delete some elements in the set
"fengye"
127.0.0.1:6379> spop myset
"love fengye"
127.0.0.1:6379> SMEMBERS myset
1) "hello"

⑤smove

127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "world"
(integer) 1
127.0.0.1:6379> sadd myset "fengye"
(integer) 1
127.0.0.1:6379> sadd myset2 "set2"
(integer) 1
127.0.0.1:6379 > smove myset myset2 "Fengye" # moves the specified element from one set to another set
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "world"
2) "hello"
127.0.0.1:6379> SMEMBERS myset2
1) "fengye"
2) "set2"

⑥sdiff/sinter/sunion

127.0.0.1:6379> sadd key1 a
(integer) 1
127.0.0.1:6379> sadd key1 b
(integer) 1
127.0.0.1:6379> sadd key1 c
(integer) 1
127.0.0.1:6379> sadd key2 c
(integer) 1
127.0.0.1:6379> sadd key2 d
(integer) 1
127.0.0.1:6379> sadd key2 e
(integer) 1
127.0.0.1:6379 > sdiff key1 key2 ා difference set
1) "a"
2) "b"
127.0.0.1:6379 > singer key1 key2 ා intersection
1) "c"
127.0.0.1:6379 > union key1 key2 ා Union
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

 

1.4.Hash

①hset/hget/hmget/hgetall:

127.0.0.1:6379 > hset myhash field1 Fengye # hset a specific key value
(integer) 1
127.0.0.1:6379> hget myhash field1
"fengye"
127.0.0.1:6379 > hmset myhash field1 Hello field2 world # set multiple key values
OK
127.0.0.1:6379 > hmget myhash field1 field2 ᦇ get multiple field values
1) "hello"
2) "world"
127.0.0.1:6379 > hgetall myhash # get all the data
1) "field1"
2) "hello"
3) "field2"
4) "World" # using hash to access object data

127.0.0.1:6379 > hmset myhash username Zhangsan sex ‘male’ age 24
OK
127.0.0.1:6379> HGETALL myhash
1) “field2”
2) “world”
3) “field1”
4) “hello”
5) “field3”
6) “5”
7) “filed3”
8) “1”
9) “field4”
10) “hello”
11) “username”
12) “zhangsan”
13) “sex”
14) “\xe7\x94\xb7”
15) “age”
16) “24”

②hdel:

127.0.0.1:6379 > HDEL myhash field1 # delete the key field specified in the hash, and the corresponding value will be lost
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "world"

③hlen:

127.0.0.1:6379> HGETALL myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
127.0.0.1:6379 > HLEN myhash # get the number of fields in the hash table
(integer) 2

④hexists:

127.0.0.1:6379 > HISTS myhash field1 # judge whether the key in the hash exists
(integer) 1
127.0.0.1:6379> HEXISTS myhash field3
(integer) 0

⑤Hkeys/Hvals:

127.0.0.1:6379 > hkeys myhash # query all keys
1) "field2"
2) "field1"
127.0.0.1:6379 > hvals myhash # query all Vals
1) "world"
2) "hello"

⑥hincrby/hsetnx:

127.0.0.1:6379> hset myhash field3 5
(integer) 1
127.0.0.1:6379 > hincrby myhash field3 1 # hash specifies the increment
(integer) 6
127.0.0.1:6379 > hincrby myhash field3 - 1
(integer) 5
127.0.0.1:6379 > hsetnx myhash field4 Hello # if not, set the value
(integer) 1
127.0.0.1:6379 > hsetnx myhash field4 world #, the distributed lock cannot be set
(integer) 0

 

1.5. Sorted set ordered set class

①zadd/zrange:

127.0.0.1:6379 > zadd myset 1 one # add a value
(integer) 1
127.0.0.1:6379 > zadd myset 2 two 3 three # add multiple values
(integer) 2
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"

②zrangebyscore:

127.0.0.1:6379> clear
127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 4000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 1000 fengye
(integer) 1
127.0.0.1:6379 > zrangebyscore salary - inf + inf # sort by salary in ascending order
1) "fengye"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379 > zhengebycore salary - inf + inf WithCores # sorted in ascending order, with salary 1) "Fengye" 2) "1000" 3) "Xiaohong" 4) "2500" 5) "Zhangsan" 6) "4000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores 1) "fengye" 2) "1000" 3) "xiaohong" 4) "2500"

③zrem:

127.0.0.1:6379 > zrem salary Zhangsan # removes the elements specified in the sorted set
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1
1) "fengye"

④zcard:

127.0.0.1:6379 > zcard salary # get the number of elements in an ordered set
(integer) 2

⑤zrevrage:

127.0.0.1:6379 > zrevrange salary 0 - 1 with cores
1) "xiaohong" 
2) "2500"
3) "fengye"
4) "1000"

⑥zcount:

127.0.0.1:6379> zadd myset 1 hello
(integer) 1
127.0.0.1:6379> zadd myset 2 world 3 fengye
(integer) 2
127.0.0.1:6379 > zcount myset 1 3 # get the number of members in the specified interval
(integer) 3
127.0.0.1:6379> zcount myset 1 2
(integer) 2

 

2. Several special types of redis

2.1 geographical location

Redis can find out the distance between the two places according to geographical location such as longitude and latitude through geospatial. It is mainly divided into the following commands:

①geoadd:

#Adding rules: except that the two poles cannot be added, other parameters can be added # key longitude latitude name # effective longitude from - 180 degrees to 180 degrees # effective latitude from - 85.05112878 degrees to 85.05112878 degrees
127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing
(integer) 1
127.0.0.1:6379> geoadd  china:city 121.47  31.23 Shanghai # add location
(integer) 1
127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqi 114.05 22.52 shengzhen
(integer) 2
127.0.0.1:6379> geoadd china:city 120.16 30.24 hangzhou 108.96 34.26 xian
(integer) 2

②geopos:

127.0.0.1:6379> geopos  china:city beijing    #Gets the longitude and latitude of the specified member
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"
127.0.0.1:6379> geopos china:city beijing xian
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"
2) 1) "108.96000176668167114"
   2) "34.25999964418929977"

③geodist:

Calculate the linear distance between two urban areas:

  • M is the unit meter
  • Km is the unit of kilometer
  • Mi is the unit mile
  • FT is in feet
127.0.0.1:6379> geodist china:city beijing shanghai
"1067378.7564"
127.0.0.1:6379> geodist  china:city beijing  Shanghai km # find the straight-line distance from Beijing to Shanghai, in km
"1067.3788"
127.0.0.1:6379> geodist china:city beijing hangzhou km
"1127.3378"

④georadius:

Taking the given latitude and longitude as the center, find out the elements within a certain radius

127.0.0.1:6379> GEORADIUS  china:city 110.35  30.39 1000 km # get the cities within 1000 km in the current longitude and latitude
1) "chongqi"
2) "xian"
3) "shengzhen"
4) "hangzhou"
127.0.0.1:6379> GEORADIUS china:city 110.35 30.39 500 km
1) "chongqi"
2) "xian"
127.0.0.1:6379> GEORADIUS  china:city 110.35  30.39 500 km with distance
1) 1) "chongqi"
   2) "383.1094"
2) 1) "xian"
   2) "449.8190"
127.0.0.1:6379> GEORADIUS  china:city 110.35  39 500 km withbird # get the urban disease within 500 km of this latitude and longitude, and display the longitude and latitude of the city
1) 1) "chongqi"
   2) 1) "106.49999767541885376"
      2) "29.52999957900659211"
2) 1) "xian"
   2) 1) "108.96000176668167114"
      2) "34.25999964418929977"
127.0.0.1:6379> GEORADIUS  china:city 110.35  30.39 500 km withdist withbird count 2 # count 2 means only two are displayed
1) 1) "chongqi"
   2) "383.1094"
   3) 1) "106.49999767541885376"
      2) "29.52999957900659211"
2) 1) "xian"
   2) "449.8190"
   3) 1) "108.96000176668167114"
      2) "34.25999964418929977"

⑤georadiusbymember:

127.0.0.1:6379> GEORADIUSBYMEMBER  china:city shanghai  400 km # find out the city with a distance of 400 km by taking the city as the center
1) "hangzhou"
2) "shanghai"

⑥geohash:

The command returns an 11 character geohash string

127.0.0.1:6379> geohash china:city beijing chongqi
1) "wx4fbxxfke0"
2) "wm5xzrybty0"

⑦ Delete the Zset command:

The bottom layer of geo is Zset, so we can use Zset to delete it

127.0.0.1:6379> ZRANGE china:city 0 -1
1) "chongqi"
2) "xian"
3) "shengzhen"
4) "hangzhou"
5) "shanghai"
6) "beijing"
127.0.0.1:6379> ZREM china:city beijing
(integer) 1

 

2.2.hyperloglogs

Redis provides a more efficient algorithm based on hyperlogs to calculate the number of elements in two sets, and the number of repetitions does not accumulate.

This efficient algorithm replaces the traditional method of using set set to access non duplicate elements.

Advantages of hyperlogs:

  • It only needs 12KB memory to access the cardinal number of different elements of 2 ^ 64
127.0.0.1:6379> PFADD mykey a b c d e f g h i j
(integer) 1
127.0.0.1:6379> PFCOUNT mykey
(integer) 10
127.0.0.1:6379> PFADD mykey2 i j z x c a b n m
(integer) 1
127.0.0.1:6379> PFCOUNT mykey2
(integer) 9
127.0.0.1:6379 > pfmerge mykey3 MyKey mykey2 # merge elements of two sets
OK
127.0.0.1:6379 > pfcount mykey3 # calculate the number of elements without repetition
(integer) 14

Note: using hyperlogs has an error rate of 0.81%. If fault tolerance is not allowed and accurate calculation is required, then hyperlogs cannot be used.

 

2.3.bitmaps type

Bitmaps uses bit 0 1 to store data.

Applicable scenarios: Statistics of user information, active and inactive, login and clock in

Use bitmaps to record the clock out time 7 days a week:

127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 0
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 1
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer) 0
127.0.0.1:6379> setbit sign 6 0
(integer) 0
127.0.0.1:6379 > getbit sign 3 # check whether there is a clock in on a certain day
(integer) 1
127.0.0.1:6379> getbit sign 6
(integer) 0

127.0.0.1:6379 > bitcount sign # count the number of days of clock in
  (integer) 3

 

Recommended Today

“Self test” stay up late to summarize 50 Vue knowledge points, all of which will make you God!!!

preface Hello everyone, I’m Lin Sanxin. A lot of things have happened these days (I won’t say what’s specific). These things have scared me to treasure my collection these yearsVue knowledge pointsI took out my notes and tried my best to recall them. Finally, I realized these 50Knowledge points(let’s not be too vulgar. It’s not […]