This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Redis supports five data types: string, hash, list, set and zset(sorted set). These data types are described below.

Start with Redis to understand caching

Redis Hash (Hash)

Redis hash is a key-value pair mapping table, and hash is particularly useful for storing objects.

Common Commands

The command instructions
HEXISTS key field Check whether the specified field in the hash table key exists.
HGET key field Gets the value of the specified field stored in the hash table
HGETALL key Gets all the fields and values of the specified key in the hash table
HINCRBY key field increment Add increment to the integer value of the specified field in the hash table key.
HKEYS key Gets all fields in the hash table
HLEN key Gets the number of fields in the hash table
HMGET key field1 [field2] Gets the values of all given fields
HMSET key field1 value1 [field2 value2 ] Set multiple field-value pairs into the hash key at the same time.
HSET key field value Set the value of field in hash table key to value.

The sample

Execute the command
# set key to pig and peppa to 123
> HSET pig peppa 123

Key = pig, field = peppa
> HGET pig peppa
123
Copy the code
Stored in Redis is

Hash: pig

ID(Total:1) key Value
1 peppa 123

Redis String (String)

String is the Redis String data type

Common commands

The command instructions
SET key value Sets the value of the specified key
GET key Gets the value of the specified key.
GETRANGE key start end Returns a subcharacter of the string value in key
MGET key1 [key2..] Gets the values of all (one or more) of the given keys.
STRLEN key Returns the length of the string value stored by key.
MSET key value [key value …] Set one or more key-value pairs at the same time.
INCR key Increment the value of the number stored in the key by one.
INCRBY key increment Add the value stored in the key to the given increment.
DECR key Subtract the number stored in the key by one.
DECRBY key decrement key Values saved minus a given decrement value.
APPEND key value If the key already exists and is a string, the APPEND command appends the value to the end of the key’s original value.

The sample

Execute the command
# set key to pig and value to peppa
> SET pig peppa

Select * from key where key = pig
>GET pig
peppa
Copy the code
Store formats in Redis
key Value
pig peppa

Redis List (List)

Redis lists are simple lists, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list

Common commands

The command instructions
LINDEX key index Gets the elements in the list by index
LINSERT key BEFORE AFTER pivot value
LLEN key Get the list length
LPUSH key value1 [value2] Inserts one or more values into the list header
LPUSHX key value Inserts one or more values into an existing list header
LRANGE key start stop Gets the elements in the specified range of the list
LREM key count value Remove list elements
LSET key index value Set the value of a list element by index
RPOP key Removes and gets the last element of the list
RPUSH key value1 [value2] Adds one or more values to the list
RPUSHX key value Add a value to an existing list

The sample

Execute the command
# add element peppa to pig list
> LPUSH name_pig peppa
1
# add element PEppa1 to pig list
> LPUSH name_pig peppa1
2
Check pig for all elements
> LRANGE name_pig  0 -1
peppa1
peppa

Copy the code
Store formats in Redis

List: name_pig

ID(Total:1) Value
1 peppa1
2 peppa

Redis collection (Set)

Redis’ Set is an unordered collection of type string. Collection members are unique, which means that no duplicate data can occur in the collection. The collection in Redis is realized by hash table, so the complexity of adding, deleting and searching is O(1).

Common commands

The command instructions
SADD key member1 [member2] Adds one or more members to a collection
SCARD key Gets the number of members of the collection
SDIFF key1 [key2] Returns the difference set of all sets given
SDIFFSTORE destination key1 [key2] Returns the difference set for a given collection and stores it in destination
SINTER key1 [key2] Returns the intersection of all sets given
SINTERSTORE destination key1 [key2] Returns the intersection of all the given collections and stores them in destination
SISMEMBER key member Check whether the member element is a member of the collection key
SMEMBERS key Returns all members of the collection
SREM key member1 [member2] Removes one or more members of a collection
SUNION key1 [key2] Returns the union of all given sets
SUNIONSTORE destination key1 [key2] The union of all given collections is stored in the Destination collection
SSCAN key cursor [MATCH pattern] [COUNT count] Iterate over the elements in the collection

The sample

Execute the command
# add element PEppa1 to pig set
> SADD pig peppa1
1
# add element PEppa2 to pig set
> SADD pig peppa2
1
Select * from pig
> SMEMBERS pig
peppa1
peppa2

Copy the code
Store formats in Redis

Set: pig

ID(Total:1) Value
1 peppa1
2 peppa2

Sorted set Redis sorted set

  • Redis ordered collections, like collections, are collections of string elements and do not allow duplicate members.
  • The difference is that each element is associated with a double score. Redis uses scores to sort the members of a collection from smallest to largest.
  • The members of an ordered set are unique, but the score can be repeated.
  • Collections are implemented by hashing tables, so adding, deleting, and searching are O(1) complexity. The maximum number of members in a collection is 232-1 (4294967295, each collection can store more than 4 billion members).

Common Commands

The command instructions
ZADD key score1 member1 [score2 member2] Adds one or more members to an ordered collection, or updates the scores of existing members
ZCARD key Gets the number of members of an ordered collection
ZINTERSTORE destination numkeys key [key …] Computes the intersection of one or more ordered sets given and stores the result set in a new ordered set key
ZRANGE key start stop [WITHSCORES] Returns an ordered collection of the members of a specified interval through an indexed interval
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] Returns an ordered set of members within a specified interval by a fraction
ZRANK key member Returns the index of the specified member in the ordered collection
ZREVRANGE key start stop [WITHSCORES] Returns the members of an ordered set in a specified interval, indexed from high to bottom
ZSCORE key member Returns the score value of a member in an ordered set
ZUNIONSTORE destination numkeys key [key …] Computes the union of a given one or more ordered sets and stores it in a new key

The sample

Execute the command
Add elements p1 score 100 p2 score 90 to pig set
> ZADD pig  100 p1 90 p2
2
Select * from pig
> ZRANGE pig 0 -1
p2
p1
Copy the code
Store formats in Redis

ZSet: pig

ID(Total:1) Score Member
1 100 p1
2 90 p2

reference

  • www.redis.net.cn/tutorial/35…