hash, list and set operations in redis

1, hash operation Data structure: key: similar to the dic...

1, hash operation

Data structure: key: similar to the dictionary in Python

For example: Info:

  • hset key k1 v1 setup / creation (Dictionary)
  • hget key k1 gets the value corresponding to k1 in key1

Batch setting acquisition

  • hmset key k2 v2 k3 v3 setting multiple K-values simultaneously
  • hmget key k1 k2 k3 getting multiple values at the same time

Get all keys

  • hkeys key

Get all values

  • hvals key

View the number of k keys below the key value

  • hlen key
127.0.0.1:6379> hset info name lina (integer) 1 127.0.0.1:6379> hget info name "lina" 127.0.0.1:6379> hmset info age 22 sex F OK 127.0.0.1:6379> hmget info name age sex 1) "lina" 2) "22" 3) "F" 127.0.0.1:6379> hkeys info 1) "name" 2) "age" 3) "sex" 127.0.0.1:6379> hvals info 1) "lina" 2) "22" 3) "F" 127.0.0.1:6379> hlen info (integer) 3

1.2 several common methods under hash

Delete the specified key value under key

  • hdel key k1 k2 k3 can delete one or more

Judge whether the specified key value under key exists

  • hexists key field
127.0.0.1:6379> hdel info sex (integer) 1 127.0.0.1:6379> hexists info sex (integer) 0

In the hash operation, a key can store 20 billion pieces of data. If you use the hmget key command, it will occupy memory and the speed will be too slow
Therefore, using scanning, the class is the same as the generator in python:

  • hscan key cursor match pattern COUNT count
    : para key: that hash
    : para cursor: cursor, where to start scanning, generally from 0
    : para match: pattern, fuzzy matching, regular expression
    : para COUNT: count how many matches? Just enter a number
127.0.0.1:6379> hscan info 0 match na* count 1 1) "0" 2) 1) "name" 2) "lina"

2, List operation

-lpush key value1 value2 value3 create a list and put data in sequence from left to right (FILO) - rpush key value value2 is the same as above, right to left (FIFO)

  • lrange key start stop takes the values in the list, from start to stop 0 -1 takes all
  • Len key view the length of the list
127.0.0.1:6379> lpush list 1 2 3 (integer) 3 127.0.0.1:6379> rpush list 1 2 3 (integer) 6 127.0.0.1:6379> lrange list 0 -1 1) "3" 2) "2" 3) "1" 4) "1" 5) "2" 6) "3" 127.0.0.1:6379> llen list (integer) 6

2.2 insert elements into the list, delete elements, etc

  • linsert key where refvalue value inserts the data value into the two options before and after the where(before and after) of the corresponding refvalue benchmark value in the key
    : para key: corresponding list
    : para where: before or after means before or after the benchmark value
    : para refvalue: benchmark value
    : para value: the value to be inserted

Note: if there are duplicate values, take the first one found as the benchmark

(integer) 6 127.0.0.1:6379> linsert list before 1 insert (integer) 7 127.0.0.1:6379> lrange list 0 -1 1) "3" 2) "2" 3) "insert" 4) "1" 5) "1" 6) "2"

Modify the value of the specified index:

  • lset key index value

Delete the value at the specified location:

  • lrem key count value parameter count is the number to delete, and value is who to delete

Get a return value from the left side of the list and delete it

  • lpop key

Gets the value of the specified index

  • lindex key index

Deletes all elements outside the specified range

  • ltrim key start end
127.0.0.1:6379> lrange list 0 -1 1) "3" 2) "2" 3) "insert" 4) "1" 5) "1" 6) "2" 7) "3" 127.0.0.1:6379> lset list 1 'lina' OK 127.0.0.1:6379> lrem list 2 1 (integer) 2 127.0.0.1:6379> lpop list "3" 127.0.0.1:6379> lrange list 0 -1 1) "lina" 2) "insert" 3) "2" 4) "3" 127.0.0.1:6379> ltrim list 0 1 OK 127.0.0.1:6379> lrange list 0 -1 1) "lina" 2) "insert" 127.0.0.1:6379> lindex list 1 "insert"

2.3 several uncommon commands, but very powerful

Add the pop on the right of list 1 to the left of lpush in list 2

  • rpoplpush key1 key2

Delete a data on the left. If the list is empty, wait for the timeout time. Within the timeout time, the elements in the list will be deleted. Like a queue in a queue

  • blpop key timeout

3, Set set

Create a collection:

  • sadd key value value value value

To view members in a collection:

  • smembers key

Returns the number of elements in the collection:

  • scard key

Difference set:

  • sdiff key1 key2

Put the difference set from the two sets into the third set

  • sdiffstore new_dest key1 key2

Intersection:

  • sinter key1 key2

Union:

  • sunion key1 key2

Check whether the element is a member of the collection:

  • sismember key value

  • spop key

  • sscan key cursor match pattern

4, Ordered set

  • zadd key weight value member weight value 2 member weight value 3 member 3

  • zrange key start end the optional parameter is WithCores

  • zcount key min max statistics weight min to Max

127.0.0.1:6379> zadd zz 1 shiqi 2 zengyue 3 yuan (integer) 3 127.0.0.1:6379> zrange zz 0 -1 1) "shiqi" 2) "zengyue" 3) "yuan" 127.0.0.1:6379> zrange zz 0 -1 withscores 1) "shiqi" 2) "1" 3) "zengyue" 4) "2" 5) "yuan" 127.0.0.1:6379> zcount zz 1 2 (integer) 2
  • zrank key member get member ranking

  • zrem key member member2 delete the specified member

14 October 2021, 17:28 | Views: 1241

Add new comment

For adding a comment, please log in
or create account

0 comments