Common commands for basic data types of reids

Redis basic data types

Redis is divided into 5 basic data types: String, Hash, List, Set, ZSet

One: spring

 

The String type is a special type that contains many types and is binary safe. For example, serialized objects are stored, such as a picture for binary storage, such as a simple string value and so on.

set and get methods:

Set name realValue 

Get name

Note: When set sets the name, if the name is repeated, the set value will be overwritten.

 

setnx method

Set value setnx name realValue 

Note: If the name already exists, it will not be overwritten, and 0 will be returned directly. If the name does not exist, a new value will be inserted.

 

setex method

Set value setex name time (seconds) realValue 

Description: Set the expiration time of the value of this name in the cache, and return nil after this time. In redis, nil means null.

 

setrange method: replace string

set email [email protected]

setrange email 10 The ww table is replaced with the following string from the first few digits.

Note: Replace [email protected] with [email protected] at this time

 

Two: Hash

 

Hash type is a mapping table of String type filed and value, or a combination of String, which is especially suitable for storing objects. In comparison, storing an object in the Hash type saves more space than storing it directly in the String. And it is convenient to store the entire object, the Hash type is also the most commonly used one in our work.

 

Form: hset user name ming means a Hash type is called user, and the value of the attribute name of this user is ming.

 

Use hget to get the value hget user name can get the value of the name attribute in this object.

 

hmset can store multiple key-value pairs in batches. hmset user age 15 sex man

 

hmget can obtain multiple key-value pairs in batches. hmget user name age sex

 

The Hash type also has hsetnx, which is similar to setnx.

 

The hincrby and hdcrby sets are incremented and decremented.

 

hexists returns 1 if it exists, returns 0 if it does not exist

 

hlen returns the numeric value of all keys in the hash.

 

hkeys returns all keys in the hash.

 

hvals returns all the values ​​in Hash.

 

hgetall returns all the keys and values ​​in the Hash.

Three: List

List type is a collection of linked list structure, its main functions are push, pop to get elements and so on. In more detail, the List type is a double-ended linked list structure. We can add and delete elements at the head or tail of the collection through related operations. The design of List is very simple and exquisite. It can be used as a stack and a queue. Meet most requirements.

 

lpush method: add elements from the head, (stack) first in and out.

Set value lpush list hello   

Description: Create a stack with name list, and push a hello into the stack

 

rpush method: add elements from the tail (queue) first in first out

Set value lpush list2 hello  

Description: Create a queue named list2, and push a hello to the stack

 

lrange method: view the value in the list

 

linsert list2 before [Elements of the collection] [Elements to be inserted]

 

The lset method replaces the element with the specified subscript

 

lrem method: delete the specified element, and return the number of deleted elements.

 

lpop method: delete elements from the head of the List, and return the deleted elements.

 

rpop method: delete elements from the end of the List, and return the deleted elements.

 

llen method: returns the number of elements.

 

lindex method: Returns the element at the index position of the element named key in the List. lindex list2 0 returns the first element

Four: Set type

The set collection is an unordered collection of String type. The set is implemented by hashtable. For the collection, we can take the intersection, union, and difference.

 

sadd method: add elements to the set named key.

Summary: set collection does not allow duplicate elements, smembers view all elements in the set.

 

The srem method deletes set collection elements. srem name value

 

The spop method randomly returns the deleted key

 

sdiff returns the different elements of the two sets, and which set is in front will be the standard.

 

sdiffstore stores the different elements returned in another collection. sdiffstore set3 set1 set2. The different elements of bar 1 and 2 are stored in 3

 

sinter returns the intersection of two sets. sinter set1 set2 returns the intersection element in set1 and set2.

 

sinterstore stores the returned intersection in a new set

 

Smove method: move elements from one set collection to another set collection smove set2 set1 bbb move bbb in set2 to set1.

 

scard method: View the number of elements in the collection.

Five: ZSet (ordered set)

Zset is an orderly adjustment based on set.

 

zadd method: add an element to the ordered set, if the element exists, update the order.

Summary: It will be updated according to the sequence attribute when it is inserted repeatedly.

 

Syntax: zadd set1 1 aaa where 1 represents the serial number. It is the sequence number of the sort. aaa represents the value of the set, and set1 represents the name of the set.

 

zrange method, view the value in the set zrange set1 0 -1 withscores

Note: withscores means that the serial number is also queried, and the serial number can be omitted if you don't want to display it.

 

The zrem method deletes elements in the collection.

Redis advanced commands

keys * return all names

 

exists whether the specified name exists

 

expire Set the expiration time of a key, use ttl to view the remaining time

 

persist cancel expiration time

 

Select select the database, the database is 0 to 15, a total of 16 databases, the default entry is 0 database.

 

move key [database subscript] Move to another database

 

randomkey randomly returns a key in the database

 

rename key newkey rename key

 

dbsize View the number of keys in the current database

 

flushdb clears the current database, and flushall clears all databases.

 

config get * Get the current redis configuration items.

 

info Get database information.

table of Contents

One: spring

Two: Hash

Three: List

Four: Set type

Five: ZSet (ordered set)

Redis advanced commands


Guess you like

Origin blog.csdn.net/weixin_46729085/article/details/108714341