basic
The Redis command is size insensitive
Basic commands
- keys
- Description of the command: Searches for all keys that match the specified pattern (regular expression)
- Command format:
KEYS pattern
- exists
- Description: Returns whether the key exists
- Command format:
EXISTS key [key ...]
- return
1
Exists.
- return
- expire
- Description of the command: Set the expiration time of a key. When the expiration time is exceeded, the key is automatically deleted
- Command format:
EXPIRE key seconds
- if
key
If yes, run it againRefresh expiration time. seconds
The unit is second.
- if
- ttl
- Description: The remaining key expiration time is returned
- Command format:
TTL key
- if
key
Does not exist or expired, return2 -
- if
key
Exists and no expiration time is set- 1
- if
- move
- The description of the command is as follows
key
Move to the given databasedb
among - Command format:
MOVE key db
- If the target database has a given
key
Or,key
Does not exist in the current database, that ismove
Will not take effect
- If the target database has a given
- The description of the command is as follows
- type
- Description Of the command: Return
key
The storage ofvalue
The type of data structure that it can returnstring, list, set, zset
和hash
And so on - Command format:
TYPE key
- Description Of the command: Return
- persist
- Description of the command: Removes the specified value
key
Expiration time of - Command format:
PERSIST key
- Description of the command: Removes the specified value
- del
- Description Of the command: Delete a specified batch
keys
, if delete some ofkey
If no, ignore it - Command format:
DEL key [key ...]
- Description Of the command: Delete a specified batch
String String
The command
- SET
- Description of the command: Sets the key to the specified string value
- Command format:
SET key value [EX seconds] [PX milliseconds] [NX|XX]
- if
key
Existing, overwrite directly - If there is an expiration time, it will refresh
EX
Indicates the expiration timesecondsFor the unitPX
Indicates the expiration timemsFor the unitNX
Say onlykey
When there is noTo set upkey
The value of theXX
Say onlykey
Presence ofTo set upkey
The value of the
- if
- Pay attention to
- Due to the
SET
Commands plus options have been completely replacedSETNX, SETEX, PSETEX
So in future releases, Redis may not recommend using andEventually abandonedThese commands
- Due to the
- GET
- Description Of the command: Return
key
的value
- Command format:
GET key
- if
key
Does not exist, returnnil
- if
key
的value
notstring
, returns an error.
- if
- Description Of the command: Return
- GETRANGE
- Gets a substring of a string
- Command format:
GETRANGE key start end
start
和end
It can be positive or negative. A positive number that is, fromOn the leftSo let’s start with negative numbersOn the rightLet’s start counting.- 1
That’s the last character,2 -
It’s the second to last.
- GETset
- Description of the command: Obtain the value first
key
The old value of, and then givekey
Set the new value - Command format:
GETSET key newvalue
newvalue
Said the new value
- Description of the command: Obtain the value first
- APPEND
- Description of the command: Append
value
To the originalvalue
At the end of the - Command format:
APPEND key value
- if
key
If yes, append the value to the end of the original value - if
key
If no, create a new onekey
Then append
- if
- Description of the command: Append
- SETRANGE
- Description of the command: Overwrite the corresponding
key
的value
The specified range of values - Command format:
SETRANGE key offset value
-
If the offset exceeds the length of the existing string value, 0 needs to be added to reach the offset. The maximum length of offset is 229=5368709112^{29} = 536870911229=536870911
-
- Description of the command: Overwrite the corresponding
- STRLEN
- Description Of the command: Return
key
的string
typevalue
The length of the - Command format:
STRLEN key
key
非string
Type, an error is returned
- Description Of the command: Return
- SETNX
- The meaning of this command is as follows
key
Set values for thevalue
If thekey
It doesn’t exist. It’s equivalent in this caseSET
Command. whenkey
There is,Do nothing.SETNX
Is short for SET if Not eXists. - Command format:
SETNX key value
- The meaning of this command is as follows
- SETEX
- Description of the command: Set
key
Corresponding stringvalue
And setkey
In a givenseconds
The timeout period expires - Command format:
SETEX key seconds value
- Description of the command: Set
- INCR
- The description of the command is as follows: Specifies the value of the storage
key
Performs the atomic increment operation on the numeric value of - Command format:
INCR key
- If the specified
key
Does not exist, then in executionincr
Before the operation, its value is set to0
- If you specify
key
Exists, but the value is not a character type orValues cannot be expressed as integersThat is, return an error - This operation is limited to
64
Bit signed integer data
- If the specified
- The description of the command is as follows: Specifies the value of the storage
- INCRBY
- The meaning of this command is as follows
key
The corresponding number plus the specified step sizedecrement
- Command format:
INCRBY key increment
key
If it does not exist, it is set to 0 firstkey
Characters that exist but are not numeric are returned in error- Maximum operation support
64
Bit a signed positive number
- The meaning of this command is as follows
- DECR
- Description: Yes
key
Subtract the corresponding number1
operation - Command format:
DECR key
- Description: Yes
- DECRBY
- The meaning of this command is as follows
key
The corresponding number minus the specified step sizedecrement
- Command format:
DECRBY key decrement
- The meaning of this command is as follows
List the List
The command
LPUSH
- Insert a value to the first position on the left of the list (if the list does not exist, a new list will be created automatically)
- Command format:
LPUSH key element1 element2 element3
RPUSH
- Insert a value to the first position on the right of the list (the list does not exist, a new list will be created automatically)
- Command format:
RPUSH key element1 element2 element3
LPUSHX
- When a list exists, insert a value to the first position on the left of the list
- Command format:
LPUSHX key element1 element2 element3
RPUSHX
- When a list exists, insert a value to the first position on the right of the list
- Command format:
RPUSHX key element1 element2 element3
LPOP
- The command is used to display the first left element in the list
- Command format:
LPOP key
- Return nil if key does not exist
RPOP
- Description of the command: Displays the element in the first position on the right of the list
- Command format:
RPOP key
- Return nil if key does not exist
- BLPOP
- The meaning of this command is a blocking list. If given
key
There’s at least one non-empty list, thenPops the head element of the first non-empty list encountered, along with the name key of the list to which the pop-up element belongsIs returned to the caller. If givenkey
Does not exist or contains an empty list,BLPOP
将Blocking the connection, until there is another client for the givenkey
Any one ofperformLPUSH
或RPUSH
So far the command. - Command format:
BLPOP list1 list2 list3 timeout
- If list1 is empty and list2 is not, the first element of List2 and the name of List2 are returned;
- If list1, list2, and List3 are empty or do not exist, a block is initiated waiting for the list to insert elements
- Timeout Specifies a non-zero timeout parameter for blocking. When timeout=0, the blocking time is unlimited.
- The meaning of this command is a blocking list. If given
- BRPOP
- Command meaning: The command has the same meaning
BLPOP
Consistent. The difference is from the listTail (right) startsPop-up element - Command format:
BRPOP list1 list2 list3 timeout
- According to the
list1 > list2 > list3
Sequential start check non-empty list iflist1
Is empty,list2
Not null, that is, returnlist2
The first element in the tail.
- According to the
- Command meaning: The command has the same meaning
LRANGE
- Description: Returns the elements in a specified range in a list.
- Command format:
LRANGE key start end
start
和end
Are allStart with zeroThe offset of.start
和end
Can beA negative number.- 1
Represents the last element of the list,2 -
It’s the penultimate element.- Such as:
LRANGE mylist 0 -1
Representing the output listAll the elements.
- Such as:
LINDEX
- Description Of the command: Return to the listSpecify the subscriptValue of, subscript from
0
Start index. - Command format:
LINDEX key index
key
Return when not a listerror
index
It could be negative.- 1
Represents the last element,2 -
Represents the penultimate element
- Description Of the command: Return to the listSpecify the subscriptValue of, subscript from
LLEN
- Description: Returns the length of a list.
- Command format:
LLEN key
key
Returns if it does not exist0
key
There is, butNot a listWhen to return toerror
LREM
- Removes a specified number of specific elements from a list
- Command format:
LREM list count element
list
Is the list namecount
Is the number of removalscount > 0
从Left to rightStart removing matched elementscount = 0
Removes all matching elementscount < 0
从Right to leftStart removing matched elements
element
Specifying element values
LTRIM
- Truncate a list of elements, that is, to find a list of subsets.
- Command format:
LTRIM key start stop
start
和stop
Is made up of0
Start counting.0
Represents the first element of the list.- Such as
LTRIM mylist 0 2
Only the list is retained as a resultbefore3
个Elements.
- Such as
RPOPLPUSH
–start
和stop
It could be a negative number,- 1
Represents what’s in the listThe last element.2 -
The penultimate- Eject the first element on the right of the source list and insert it into the first position on the left of the target list.
- Command format:
RPOPLPUSH source destination
source
Source list. ifsource
Does not exist, returnnil
No further operation is performed.destination
Target list.- if
source
和destination
Pointing to the same list is equivalent to removing the listThe last element(the first element on the right) and insert into the listHead (left)
BRPOPLPUSH
- Command meaning:
RPOPLPUSH
Blocked version of. - Command format:
BRPOPLPUSH source destination timeout
timeout
nonzeroThe blocking time of.timeout = 0
That is, the client connection is blocked indefinitely.
- Command meaning:
LSET
- Command description: Set the subscript position specified in the list.
index
) is the specified value (value
) - Command format:
LSET key index value
index
Out of range, returnserror
- Command description: Set the subscript position specified in the list.
LINSERT
- Inserts a value before or after the specified element value in the list.
- Command format:
LINSERT key BEFORE|AFTER pivot value
key
When it doesn’t exist,No action is performed.key
There is, but ** is not onelist
**, returns error
- Command example:
LINSERT mylist BEFORE "world" "There"
- Said to
mylist
In the listworld
Insert before elementThere
The element
- Said to
Set the Set
The command
-
SADD
- Add one or more specified member elements to the key of the collection
- Command format:
SADD key member [member ...]
- If the collection
key
Does not exist, a new collection is created and elements are added to the collection - If the element is already in the collection
key
If, it willIgnore that elementContinue to insert other elements - if
key
The type ofIs not set, an error is returned Redis
version> = 2.4
, can accept multiple parameters;2.4
Previous versionEvery timeOnly allowAdd an element
- If the collection
-
SPOP
- Description of the command: From
key
In the collectionRemove and returnOne or morerandomThe element - Command format:
SPOP key [count]
count
Represents the collection from whichkey
中Remove and returnHow many random elementscount
This parameter is not available in earlier versions2.6, 2.8, 3.0
This parameter cannot be used in the versioncount
When the parameter value is passedThe number of elements greater than the setReturns the entire collectionkey
Returns if it does not existnil
- Description of the command: From
-
SCARD
- Description: Returns the number of elements in a collection
- Command format:
SCARD key
key
Returns if it does not exist0
-
SREM
- Removes the specified element from the collection
- Command format:
SREM key member [member ...]
- if
key
It’s not a set, an error is returned - if
key
If the collection does not exist, return0
- Multiple element parameters can be specified, less than
2.4
Only one element parameter can be specified in the version of - if
key
In the collectionThere is noThe specified element is ignored
- if
-
SMOVE
- Move an element from the source collection to the target collection
- Command format:
SMOVE source destination member
- if
source
If the source collection does not exist, nothing is done and returns0
- if
source
The source collection does not contain the specified element, does nothing and returns0
- from
source
Removes the specified element from the source collection and adds it todestination
Target collection, returns on successful execution1
- if
destination
If the specified element exists in the target collection, the command is executedWill only removesource
Elements of the source collection - if
source
destination
Is not a collection type, an error is returned
- if
-
SISMEMBERS
- Check whether the specified element exists in the collection
- Command format:
SISMEMBER key member
- If it exists, it returns
1
; Returns if it does not exist0
- If it exists, it returns
-
SMEMBERS
- Description: Returns all elements of the key set
- Command format:
SMEMBERS key
-
SRANDMEMBER
- Description: Returns a specified number of random elements in a collection
- Command format:
SRANDMEMBER key [count]
- Command is executed without removing elements from the collection
Redis 2.6
Start, acceptablecount
parameter- if
count
是Positive and greater thanThe total number of elements of the collection, returns all elements of the collection,No extra elementsThere are no repeating elements - if
count
是Positive and less thanThe total number of elements in the collectioncount
Zero random elements, no repeating elements - if
count
是Negative and the absolute value is greater thanThe total number of elements in the collection, then the returned result appearsAn element occurs more than onceThe number of elements returned is alsoWill be equal tocount
The absolute value of
-
SSCAN
- Iterates incrementally over a collection element
- Command format:
SSCAN cursor [MATCH pattern] [COUNT count]
-
SDIFF
- Find the elements of the difference between a set and a given set
- Command format:
SDIFF key [key ...]
key
If it does not exist, the default value isAn empty set
SDIFFSTORE
- Take the difference between two sets and save the result to the target set
- Command format:
SDIFFSTORE destination key [key ...]
- Such as
SDIFFSTORE key key1 key2
In the commandkey
Is the target set, which is used to save the difference setReturns the number of elements of the difference set - If the target set
destination
If yes, override
- Such as
SUNION
- The command is used to obtain the union of multiple sets
- Command format:
SUNION key [key ...]
- There is nothe
key
Be regarded asEmpty collection
- There is nothe
SUNIONSTORE
- Fetch the union of multiple sets and save the result to the target set
- Command format:
SUNIONSTORE destination key [key ...]
destination
Represents the target set- If the target set
destination
If yes, thenCover to rewrite - After the command is executed successfully, the number of elements in the union is returned
SINTER
- Fetch the intersection of multiple sets
- Command format:
SINTER key [key ...]
- If only one empty set exists in the specified set, the result must be empty
key
If it does not exist, it is considered an empty set
SINTERSTORE
- Take the intersection of multiple sets and save the result to the target set
- Command format:
SINTERSTORE destination key [key ...]
destination
Is the target set- Overrides if the target collection already exists
Hash Hash
The command
HSET
- Description of the command: sets a key/value pair in a specified hash set
- Command format:
HSET key field value
- If you specify the hash set
key
If it does not exist, a new hash set will be created and associated withkey
associated - If the field already exists, its value is overridden
- If you specify the hash set
HMSET
- Description of this command: Indicates a specified pass
key
Hash set addedMultiple key-value pairs - Command format:
HMSET key field value [field value ...]
- if
key
If the hash set does not exist, a new hash set is created
- if
- Description of this command: Indicates a specified pass
HSETNX
- Description of the command: If the specified field does not exist in the specified key hash set, a field is added and its value is set
- Command format:
HSETNX key field value
- if
key
If the hash set does not exist, a new hash set is created - if
field
If a field exists, its value will not be set
- if
HGET
- Description Gets the value of the specified field in the specified hash set
- Command format:
HGET key field
- The field does not exist or
key
If no, returnnil
- The field does not exist or
HMGET
- Description: Gets the values of multiple fields in a specified hash set
- Command format:
HMGET key field [field ...]
- Returns each field that does not exist in the hash set
nil
值
- The return order is the same as the field request order
- Returns each field that does not exist in the hash set
HEXISTS
- Description Of the command: Checks whether the specified field exists in the hash set
- Command format:
HEXISTS key field
- Exists, return
1
- Does not exist, return
0
- Exists, return
HGETALL
- Description: Gets all the fields in the specified hash set and their corresponding values
- Command format:
HGETALL key
- If key does not exist, an empty list is returned
- Returns the value next to the name of each field in the list
HKEYS
- Description of the command: Returns the names of all fields in a hash set
- Command format:
HKEYS key
key
Returns an empty list if it does not exist
HVALS
- Description: Returns the values of all fields in the hash set
- Command format:
HVALS key
key
Returns an empty list if it does not exist
HLEN
- Description: Returns the total number of fields in a hash set
- Command format:
HLEN key
key
Returns if it does not exist0
HSTRLEN
- Returns the length of a string for the value of a specified field in the hash set
- Command format:
HSTRLEN key field
- if
key
Does not exist orfield
If no, return0
- if
HDEL
- Description of the command: Deletes a specified field in a hash set
- Command format:
HDEL key field [field ...]
2.4
The above version, accept multiplefield
Parameter, the lower version can only accept onekey
If no, return0
- The result returned, indicating how many fields were deleted
HSCAN
- Iterates incrementally over a collection element
- Command format:
HSCAN cursor [MATCH pattern] [COUNT count]
HINCRBY
- Adds a value to the value of a specified field in a given hash set.
- Command format:
HINCRBY key field increment
increment
Represents the increased step size- If the field does not exist, set it to
0
, and then perform the add operation
HINCRBYFLOAT
- Description Of the command: Indicates the specified value
key
Of the hash set offield
Field value executionfloat
Types of increment
value-added - Syntax: HINCRBYFLOAT Key field increment
- The value of the field must be numeric
increment
If it can be resolved intofloat
The value of the type
- Description Of the command: Indicates the specified value
Ordered set Zset
Commands to add and query
ZADD
- Description of the command: Add an element to an ordered collection.
- Command format:
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
- parameter
XX, NX, CH, INCR
Only in the> = redis 3.0.2
Version of - if
key
Type is not an ordered collection, an error is returned - canAdd multiple at a timeThe score
score
+ elementmember
】 - If the corresponding member element already exists, that is, to modify the member element
score
Value, andUpdate to theCorrect sort position - Score value
score
Is aA double - precision floating-point type
Numeric string.-inf
Minus infinity,+inf
That means plus infinity. Numerical range
到
- XX option, which updates existing elements. If none exists, no operation is performed
- NX option, which does not update existing elements, but simply adds new ones
- The CH option returns the total number of elements that have changed, including newly added elements and elements that already exist but have updated scores (the same scores will not be updated)
- INCR option, which increments the specified element
- Elements with the same score are sorted according to dictionary rules for the elements. Lexicographical ordering uses binary and compares byte arrays of strings.
- parameter
ZRANGE
- The command returns a specified range of elements in an ordered collection.
- Command format:
ZRANGE key start stop [WITHSCORES]
- The returned elements are sorted by fraction from lowest to highest, or by dictionary if the score is the same
start
和stop
Is a zero based index,0
Represents the first element,1
Represents the second element,- 1
That’s the last to last element,2 -
Represents the penultimate elementstart
和stop
是All containRange. Such asZRANGE myzset 0 1
Returns the first and second elements of the collectionstart
Is greater thanstop
Returns an empty list.stop
Greater than the length of the collection, returns the collection by defaultThe last oneThe elementWITHSCORES
Option to return elements and fractions together. Format, such as:Element_1, SCORE_1, ELement_2, SCORE_2... , element_N, score_N
ZRANGEBYLEX
- Description: Returns a member of a specified range in a collectionDictionaries are sorted in positive order, but requires elements ofThe scores have to be the same.
- Command format:
ZRANGEBYLEX key min max [LIMIT offset count]
- score
score
When inconsistent,Do not use this commandBecause the return result is inaccurate min
Represents the smallest element in the sort position[
The beginning can also be used(
Start, can be used-
Instead ofmax
Represents the largest element in the sort position[
The beginning can also be used(
Start, can be used+
Instead ofLIMIT
Indicates whether the return result is paged and must be specifiedoffset
和count
offset
Represents the starting position of the return resultcount
Represents the number of results
- score
- Command format:
- Description: Returns a member of a specified range in a collectionDictionaries are sorted in positive order, but requires elements ofThe scores have to be the same.
ZRANGEBYSCORE
- Description of the command: returns a score in an ordered set
min
和max
All elements between (including fractions andmin
或max
Equal) - Command format:
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
WITHSCORES
Option, which returns the element and its score- By default, min and Max are closed ranges. To specify an open range, run the
min
或max
prefix(
- Description of the command: returns a score in an ordered set
ZRANK
- Description: Returns the ranking of the specified elements in the collection, ascending from small to large
- Command format:
ZRANK key member
- Ranking in
0
To start,score
The smallest element is ranked as0
member
Returns if it is not an element of the collectionnil
- Ranking in
ZSCAN
- Iterates incrementally over a collection element
- Command format:
ZSCAN key cursor [MATCH pattern] [COUNT count]
ZSCORE
- The value of the specified element in the collection is returned
score
Score value - Command format:
ZSCORE key member
- The return value isString representationthe
double
Type of floating point Numbers
- The return value isString representationthe
- The value of the specified element in the collection is returned
Command for statistics
ZCARD
- Description: Returns the number of elements in a collection
- Command format:
ZCARD key
ZCOUNT
- Description: Returns the number of elements in a specified fraction range
- Command format:
ZCOUNT key min max
ZLEXCOUNT
- Description of the command: indicates a collection of statistics
Between specified members
Number of members of - Command format:
ZLEXCOUNT key min max
- Such as
zlexcount zset [member1 [member5
min
和max
It can be either a fraction value or an element value
- Such as
- Description of the command: indicates a collection of statistics
The command to delete an element
ZPOPMIN
- The description of the command is as follows: Delete and return theThe lowest scorethe
count
An element- Command format:
ZPOPMIN key [count]
count
The default value of1
. whencount
Is greater than1
, the element with the lowest score is first followed by the element with the lowest score, and then recursively.- The return value is a list of scores and elements
- Command format:
- The description of the command is as follows: Delete and return theThe lowest scorethe
ZPOPMAX
- Command meaning:
- Command format:
BZPOPMIN
- Command meaning:
- Command format:
BZPOPMAX
- Command meaning:
- Command format:
ZREM
- Command meaning:
- Command format:
ZREMRANGEBYRANK
- Command meaning:
- Command format:
ZREMRANGEBYSCORE
- Command meaning:
- Command format:
Command to sort elements
ZREVRANK
- Command meaning:
- Command format:
ZREVRANGE
- Command meaning:
- Command format:
ZREVRANGEBYLEN
- Command meaning:
- Command format:
ZREVRANGEBYSCORE
- Command meaning:
- Command format:
Commands for collection operations
ZINCRBY
- Command meaning:
- Command format:
ZINTERSTORE
- Command meaning:
- Command format:
ZUNIONSTORE
- Command meaning:
- Command format: