Redis common data types and usage scenarios

ac8147d8309d49578329c254777efc24.jpgRedis currently supports 5 data types, namely:

 

 

String (string)

List (list)

Hash (dictionary)

Set (collection)

Sorted Set (ordered collection)

The following are the five data types and their corresponding operation commands.

 

1. String (string)

String is a simple key-value key-value pair, and value can be not only String, but also numbers. String is stored in redis by default as a string, which is referenced by redisObject. When incr, decr and other operations are encountered, it will be converted into a numerical value for calculation. At this time, the encoding field of redisObject is int.

 

String is stored in redis by default as a string, which is referenced by redisObject. When incr, decr and other operations are encountered, it will be converted into a numerical value for calculation. At this time, the encoding field of redisObject is int.

 

Application Scenario

String is the most commonly used data type, and ordinary key/value storage can be classified into this category, so I won’t explain it here.

 

Related commands

SET key value set key=value

GET key or the value corresponding to the key key

GETRANGE key start end Get a substring of a string stored in a key

GETSET key value Set the string value of the key and return the old value

GETBIT key offset Returns the offset of the string value stored in the key value

MGET key1 [key2..] Get all values ​​for a given key

SETBIT key offset value Set or clear the bit at the offset of the string value stored at the key

SETEX key seconds value set value when key expires

SETNX key value Set the value of a key, only if the key does not exist

SETRANGE key offset value Overwrites the offset of a part of the string from the specified key

STRLEN key Get the length of the value stored at the key

MSET key value [key value...] set multiple keys and multiple values

MSETNX key value [key value...] set multiple keys multiple values, only when no key exists

PSETEX key milliseconds value sets the milliseconds value and expiration time of the key

INCR key Increments the integer value of the key once

INCRBY key increment Increments the integer value of the key by the given amount

INCRBYFLOAT key increment Increments the floating point value of the key by the given amount

DECR key Decrements the integer value of the key once

DECRBY key decrement Decrements the integer value of the key by the given number

APPEND key value Append value to a key

Among them, the commands used to operate the management key are:

 

DEL key delete key if present

DUMP key returns a serialized version of the value stored at the specified key

EXISTS key This command checks if the key exists

EXPIRE key seconds specifies the expiration time of the key

EXPIREAT key timestamp specifies the key expiration time. Here, time is in Unix timestamp format

PEXPIRE key milliseconds Set the key to expire in milliseconds

PEXPIREAT key milliseconds-timestamp Set key to expire in milliseconds specified in Unix timestamp

KEYS pattern finds all keys matching the specified pattern

MOVE key db move key to another database

PERSIST key remove expired key

PTTL key Gets the remaining time to expire the key in milliseconds.

TTL key Gets the remaining time until the key expires.

RANDOMKEY returns a random key from Redis

RENAME key newkey Change the name of a key

RENAMENX key newkey Rename key if new key does not exist

TYPE key returns the value of the data type stored at the key.

Example of use

redis 127.0.0.1:6379> set baidu http://www.baidu

OK

redis 127.0.0.1:6379> append baidu .com

(integer) 20

redis 127.0.0.1:6379> get baidu

"http://www.baidu.com"

redis 127.0.0.1:6379> set visitors 0

OK

redis 127.0.0.1:6379> incr visitors

(integer) 1

redis 127.0.0.1:6379> incr visitors

(integer) 2

redis 127.0.0.1:6379> get visitors

"2"

redis 127.0.0.1:6379> incrby visitors 100

(integer) 102

redis 127.0.0.1:6379> get visitors

"102"

redis 127.0.0.1:6379> type baidu

string

redis 127.0.0.1:6379> type visitors

string

redis 127.0.0.1:6379> ttl baidu

(integer) -1

redis 127.0.0.1:6379> rename baidu baidu-site

OK

redis 127.0.0.1:6379> get baidu

(nil)

redis 127.0.0.1:6379> get baidu-site

"http://www.baidu.com"

2. List (list)

The Redis list is a simple list of strings, which can be compared to std::list in C++. Simply put, it is a linked list or a queue. Elements can be added to a Redis list from the head or the tail. The maximum length of the list is 2^32 - 1, that is, each list supports more than 4 billion elements.

 

The implementation of Redis list is a doubly linked list, which can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Many internal implementations of Redis, including sending buffer queues, also use this data structure.

 

Application Scenario

There are many application scenarios of Redis list, and it is also one of the most important data structures of Redis. For example, twitter's attention list and fan list can be realized by using Redis list structure. For example, some applications use Redis list type to implement a simple Lightweight message queue, producer push, consumer pop/bpop.

 

Related commands

BLPOP

BLPOP key1 [key2 ] timeout fetch and get the first element in the list, or block until one is available

BRPOP

BRPOP key1 [key2 ] timeout fetch and get the last element in the list, or block until one is available

BRPOPLPUSH

BRPOPLPUSH source destination timeout pops a value from a list, pushes it to another list and returns it; or blocks until one is available

LINDEX

LINDEX key index Get the corresponding element from the index of a list

LENSED

LINSERT key BEFORE|AFTER pivot value inserts an element after or before other elements in the list

CURTAIN

LLEN key Get the length of the list

LPOP

LPOP key Gets and takes out the first element in the list

LPUSH

LPUSH key value1 [value2] Prepend a list of one or more values

LPUSHX

LPUSHX key value prepends a list of values, only if present in the list

LRANGE

LRANGE key start stop get various elements from a list

LREM

LREM key count value removes elements from the list

LSET

LSET key index value The index in the list to set the value of an element

LTRIM

LTRIM key start stop Trim the list to the specified range

RPOP

RPOP key out and get the last element in the list

RPOPLPUSH

RPOPLPUSH source destination removes the last element of a list, appends it to another list and returns it

RPush

RPUSH key value1 [value2] Adds one or more values ​​to the list

RPUSHX

RPUSHX key value Add a value list, only if the list exists

Example of use

redis 127.0.0.1:6379> lpush list1 redis

(integer) 1

redis 127.0.0.1:6379> lpush list1 hello

(integer) 2

redis 127.0.0.1:6379> rpush list1 world

(integer) 3

redis 127.0.0.1:6379> llen list1

(integer) 3

redis 127.0.0.1:6379> lrange list1 0 3

1) "hello"

2) "redis"

3) "world"

redis 127.0.0.1:6379> lpop list1

"hello"

redis 127.0.0.1:6379> rpop list1

"world"

redis 127.0.0.1:6379> lrange list1 0 3

1) "redis"

3. Hash (dictionary, hash table)

Similar to the dict type in C# or the hash_map type in C++.

 

Redis Hash is actually a HashMap corresponding to Value. In fact, there are two different implementations here. When the members of this Hash are relatively small, Redis will use a one-dimensional array-like method for compact storage in order to save memory, instead of using a real HashMap structure. The encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will be automatically converted into a real HashMap. At this time, the encoding is ht.

 

Application Scenario

Assuming that there are multiple users and corresponding user information, it can be used to store the user ID as the key, and serialize the user information into, for example, json format as the value for storage.

 

Related commands

HDEL

HDEL key field[field...] Delete one or several attribute fields of the object, and non-existing attributes will be ignored

HEXISTS

HEXISTS key field Check whether the object exists in the field

HGET

HGET key field Get the value of the field attribute field in the object

HGETALL

HGETALL key Get all attribute fields and values ​​​​of the object

HINCRBY

HINCRBY key field value Increase the value of the specified field in the object by the given value, atomic auto-increment operation, only integer attribute values ​​can be used

HINCRBYFLOAT

HINCRBYFLOAT key field increment Increase the value of the specified field in the object by the given floating point number

HKEYS

HKEYS key Get all attribute fields of the object

WHALES

HVALS key Get all attribute values ​​​​of the object

HLEN

HLEN key Gets the total number of all attribute fields of the object

HMGET

HMGET key field[field...] Get the value of one or more specified fields of the object

HSET

HSET key field value Set the value of the specified field of the object

HMSET

HMSET key field value [field value ...] Set the value of one or more fields in the object at the same time

HSETNX

HSETNX key field value only sets the value of the field when the specified field does not exist in the object

HSTRLEN

HSTRLEN key field Returns the string length of the value of the specified field of the object, if the object or field does not exist, returns 0.

HSCAN

HSCAN key cursor [MATCH pattern] [COUNT count] Similar to SCAN command

Example of use

127.0.0.1:637

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/131906817