Redis - five data types and basic operations

Posted by natbrazil on Sun, 16 Jan 2022 02:11:13 +0100

Redis and other key - value caching products have the following three characteristics:

  • Redis supports data persistence. It can save the data in memory on disk and can be loaded again for use when restarting.
  • Redis not only supports simple key value data, but also provides storage of list, set, zset, hash and other data structures.
  • Redis supports data backup, that is, data backup in master slave mode.

There are five data types in Redis:

  • String: String
  • Hash: hash
  • List: List
  • Set: Set
  • zset(Sorted Set): ordered set

1, String (string)

  • As like as two peas, string is the most basic type of redis. You can understand it as a type that is exactly the same as Memcached, and a key corresponds to a value.
  • String types are binary safe. redis's string can contain any data. For example, jpg images or serialized objects.
  • String type is the most basic data type of Redis. The value of string type can store 512MB at most.

1. Basic operation

  • Add / modify data: set key value
  • Get data: get key
  • Delete data: del key
  • Add / modify multiple data: mset key value key1 value1
  • Get multiple data: mget key key1
  • Append information to the back of the original data (add if it does not exist): append key value
# Example
> set test "testString"
OK
> get test
"testString"
> del test
(integer) 1
> mset test1 "value1" test2 "value2"
OK
> mget test1 test2
1) "value1"
2) "value2"
> append test1 "111"
9
> mget test1
1) "value1111"

2. Increase / decrease

  • Set the value to increase the value of the specified range
    • By default, 1: incr key is added each time
    • Value added each time: incrby key value
  • Sets the value of the data reduction specified range
    • By default, 1: decr key is deducted each time
    • Decrease value every time: decrby key value
> incr testCount
(integer) 1
> get testCount
"1"
> incrby testCount 3
(integer) 4
> get testCount
"4"
> decr testCount
(integer) 3
> get testCount
"3"
> decrby testCount 4
(integer) -1
> get testCount
"-1"

3. Time effective operation

  • Set expiration time: setex key seconds value
> setex testExpire 10 "value"
OK
> get testExpire
"value"
# get after 10s, it has failed
> get testExpire
(nil)

2, Hash (hash)

  • Redis hash is a collection of key value (key = > value) pairs.
  • Redis hash is a mapping table of field and value of string type. Hash is especially suitable for storing objects.

1. Basic operation

  • Add / modify data: hset key field value
  • get data
    • hget key field
    • hgetall key
  • Delete data: HDEL key field1
  • Add / modify multiple data: hmset key field value field1 value1
  • Get multiple data: hmget key field1
  • Get the number of fields in the table: hlen key
  • Gets whether a field exists in the table: exists key field
> hset testHash name ywb
1
> hset testHash age 20
1
> hget testHash name
"ywb"
> hget testHash age
"20"
> hgetall testHash
1) "name"
2) "ywb"
3) "age"
4) "20"

> hset testHash name ywbb
0
> hget testHash name
"ywbb"
> hmset testHash name ywbbb age 22
OK
> hgetall testHash
1) "name"
2) "ywbbb"
3) "age"
4) "22"

> hlen testHash
2
> hexists testHash name
(integer) 1
> hexists testHash sex
(integer) 0

> hmget testHash name
1) "ywbbb"
> hmget testHash name age
1) "ywbbb"
2) "22"
> hmget testHash name age sex
1) "ywbbb"
2) "22"
3) (nil)

2. Extension operation

  • Get all the field keys in the hash table: hkeys key
  • Get all field values in the hash table: hvals key
  • Set the value of the specified field and increase the value of the specified range:
    • hincrby key field increment
    • hincrbyfloat key field increment
> hkeys testHash
1) "name"
2) "age"
> hvals testHash
1) "ywbbb"
2) "22"

> hincrby testHash age 2
(integer) 24
> hget testHash age
"24"

3, List

  • Data storage requirements: store multiple data and distinguish the order of data storage space
  • Required data structure: one storage space holds multiple data, and the entry order can be reflected through the data
  • List type: save multiple data, and the bottom layer is realized by using the two-way linked list storage structure

1. Basic operation

  • Add / modify data
    • lpush key value value1
    • rpush key value value1
  • get data
    • lrange key start end
    • lindex key index
    • llen key
  • Delete data
    • rpop key
    • lpop key
> lpush testList val1 val2
(integer) 2
> llen testList
(integer) 2
> lindex testList 1
"val1"
> lindex testList 2
(nil)
> lindex testList 0
"val2"
> lrange testList 0 0
1) "val2"
> lrange testList 0 1
1) "val2"
2) "val1"
> lpop testList
"val2"

2. Extension operation

  • Obtain and remove data within a specified time
    • blpop key1 key2 timeout
    • brpop key1 key2 timeout

4, Set

  • New storage requirements: store a large amount of data to facilitate query and provide higher efficiency
  • Required storage structure: it can save a large amount of data, and has an efficient internal storage mechanism to facilitate query
  • set type: exactly the same as the hash storage structure. Only keys are stored, and values (nil) are not stored. Duplicate values are not allowed

1. Basic operation

  • Add / modify data: sadd key member member1
  • Get data: smembers key
  • Delete data: srem key member1
  • Get total set data: scar key
  • Judge whether the set contains the specified data: sismember key member
> sadd testSet a b c d
(integer) 4
> srem testSet a
1
> smembers testSet
1) "b"
2) "c"
3) "d"
> scard testSet
3

> sismember testSet a
(integer) 0
> sismember testSet b
(integer) 1

2. Extension operation (random access, intersection, union, difference set)

  • Randomly obtain the specified amount of data in the collection: srandmember key count
  • Randomly obtain a data in the set and remove the modified data set from the set: spop key
> srandmember testSet 2
1) "b"
2) "c"
> srandmember testSet 2
1) "c"
2) "d"
> srandmember testSet 2
1) "c"
2) "d"
> srandmember testSet 2
1) "b"
2) "d"

> spop testSet
"c"
  • Intersection, union and difference sets of two sets
    • sinter key key1
    • sunion key key1
    • sdiff key key1
> sadd set1 a b c d
(integer) 4
> sadd set2 a b c e f
(integer) 5
> sinter set1 set2
1) "b"
2) "a"
3) "c"
> sunion set1 set2
1) "b"
2) "a"
3) "c"
4) "e"
5) "d"
6) "f"
# sdiff is equivalent to the difference set of left connection
> sdiff set1 set2
1) "d"
> sdiff set2 set1
1) "f"
2) "e"
  • The intersection, union and difference sets of two sets are stored in the specified set
    • sinterstore destination key1 key2
    • sunionstore destination key1 key2
    • sdiffstore destination key1 key2
> sinterstore setInter set2 set1
3
> smembers setInter
1) "a"
2) "b"
3) "c"
> sunionstore setUnion set2 set1
6
> smembers setUnion
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
> sdiffstore setDiff set2 set1
2
> smembers setDiff
1) "f"
2) "e"

5, ZSet (Sorted Set)

  • Sorting is not supported in the previous four types. Let's look at sorted_set type supports both big data storage and sorting

1. Basic operation

  • Add data: zadd key score member
  • get data
    • zrange key start stop
    • zrevrange key start stop
  • Delete data: zrem key member
> zadd zset1 10 setVal1
(integer) 1
> zadd zset1 9 setVal2
(integer) 1
> zadd zset1 10 setVal3
(integer) 1

> zrange zset1 0 -1
1) "setVal2"
2) "setVal1"
3) "setVal3"
> zrange zset1 0 -1 withscores
1) "setVal2"
2) 9.0
3) "setVal1"
4) 10.0
5) "setVal3"
6) 10.0
> zrevrange zset1 0 -1 withscores
1) "setVal3"
2) 10.0
3) "setVal1"
4) 10.0
5) "setVal2"
6) 9.0

> zrem zset1 setVal1
1

2. Extension operation

  • Get data by conditions:
    • zrangebyscore key min max
    • zrevrangescore key max min
  • Delete data conditionally:
    • zremrangebyrank key start stop
    • zremrangebyscore key min max
  • Get total set data:
    • zcard key
    • zcount key min max
> zrangebyscore test 2 3
1) "val2"
2) "val3"
> zrevrangebyscore test 3 2
1) "val3"
2) "val2"

> zremrangebyrank test 0 1
(integer) 2
> zremrangebyscore test 0 1
0

> zcard test
3
> zcount test 1 3
3
  • Collection, intersection, and operation:
    • zinterstore destination numkeys key
    • zunionstore destination numkeys key
    • (this instruction will not be demonstrated, but you can view the document yourself. It is similar to set, but it will add up the sum of all intersecting sets. Then there is a numkeys parameter here. This parameter is a total of several keys, and several keys are required for calculation.)
  • Get the index corresponding to the data:
    • zrank key member
    • zrevrank key member
  • socre value acquisition and modification:
    • zscore key member
    • zincrby key increment member

Reference link: one article deals with the five data types and application scenarios of Redis

Topics: Redis