Syntax Redis finishing commonly used commands
Doc using the command environment:
Command keys
? Matches one character
* Matches any number (including zero) characters
[] Matches any character between the brackets, can use the "-" symbol indicates a range, such as a [bd] matches "ab", "ac", "ad"
\ X match the character x, the symbol used to escape, if you want to match "?" You need to use \?
Determining whether there is a key
exists key
If so, it returns an integer type 1, and 0 otherwise
Delete key
del key [key.....]
Can delete one or more keys, the return value is the number of deleted keys
Note: Delete does not support wildcards
Obtain key data types
type key
The return value may be a string (character string type) the hash (hash type) List (list type) set (collection) zset (ordered collection)
Assignment and values
set key value assignment
get key value
Incrementing number
incr key
When a string is stored as an integer when, redis provides a command incr role is to make use of the current key value is incremented, and returns the value incremented
whether incr
When the key does not exist to be operated default key value is 0, so the results after the first increment is 1, when the key is not an integer redis will prompt an error
Increase specified by the integer
incrby key increment
incrby command and incr command is basically the same, but the former can specify a numerical increase by increment parameters such as:
a incrby 2
a incrby 3
Reduction specified integer
decr key
decrby key increment
desc command incr command the same usage, just let the key descending
decrby command and use the same command incrby
Adds the specified float
incrbyfloat key increment
Similarly incrby incrbyfloat command command, the former difference is a double-precision floating-point numbers may be incremented, such as:
a incrbyfloat 2.7
Note: (subject to restrictions reids version, the version is greater than 2.6 version)
Added value to the tail
append key value
Action value is added to the end of the key, set the value of the change key, if the key is not present value, which is equivalent to set key value. The return value is the length of the string is added after
如:append foo " hello word!"
Get string length
strlen key
Returns the length of the key, if the key does not exist returns 0
While obtaining / providing a plurality of keys
mget key [key.....]
mset key value [key value .......]
Bit operating
getbit key offset
setbit key offset value
bitcount key [strart] [end]
bitop operation destkey key [key .....]
A byte consists of 8 binary bits, redis provides four command bits directly operate
getbit command to get the value of a bit position of the specified key character string type (0 or 1), the index starts from 0, the index bits if needed exceeds the acquired key
The actual length of the default bit value of bit 0
Long setbit command sets the value of the key bit string of the type specified position, the return value is the old value of the position, if the position set exceeds the required key bit binary
Degree, the command will automatically SetBit middle bit is set to 0, similarly provided a key value does not exist in the designated bins will automatically be assigned to the preceding bit 0
bitcount command to get the key type string value is the number of 1 bit, byte count may be limited by the range of parameters, such as statistical two bytes before we want (i.e.
"Aa") command: bitcount foo 0 1 Note: (reids by restricted version, the version is greater than 2.6)
bittop bit operation command may be a plurality of keys string type, and stores the result in parameter specifies destkey key. This command supports arithmetic operations have AND, OR,
XOR、 NOT,
As we OR arithmetic operation of the bar and aar:
set foo1 bar
set foo2 aar
bitop OR res foo1 foo2
get res
Note: (reids by restricted version, the version is greater than 2.6)
Hash type
Assignment and values
hset key field value
hget key field
hmset key field value [ field value ...... ]
hmget key field [ field ...... ]
hgetall key
hset command to assign to the field, and the command to obtain the value hget field
hset at a convenient command that does not distinguish between the insert and update operations, which means that data is not modified prior to determine whether the field is present is determined to perform the insertion operation or update operation
For, when the insertion operation is performed, the command returns HSET 1, when the operation is an update, the command returns HSET is 0, when the key itself does not exist, the command will HSET
He established automatically
hmset a plurality of keys
hmget obtaining a plurality of keys
hgetall get all the key fields and field values in use but do not know which fields have a key, the result returns a list of fields and field values consisting of
Determine whether the field exists
hexists key field
There is return 1, otherwise it returns 0
When the field is not present assignment
hsetnx key field value
hsetnx command hset command similar, except that if the field already exists, hsetnx command will do nothing
Increase the numbers
hincrby key field increment
So that the field specified by the integer value increases
Delete field
hdel key field [ field .....]
Deleting one or more fields, the return value is the number of fields deleted
Gets only the field name or field value
hkeys key
whales key
hkeys get the names of all fields
hvals values obtained in all the fields of the key
Obtain a number of fields
hlen key
List Type
Add elements to the ends of the list
lpush key value [ value ....... ]
rpush key value [ value ....... ]
lpush command to add elements to the left of the list, it returns a list of elements to increase the length of the
rpush command to add elements to the right of the list, it returns a list of elements to increase the length of the
Pop elements from both ends of the list
lpop key
rpop key
lpop eject command element from a list on the left, two steps lpop command, 1: list of elements removed from the list on the left, 2: Returns the value of the element is removed
rpop command can pop an element from the list on the right
Gets the number of elements in the list
Ad ditional key
When the key does not exist, llen returns 0
Get a list of fragments
lrange key start stop
Obtaining a sequence in the list, and returns the index from start to all the elements between the stop (including both ends of the element) based index 0
NOTE: lrange languages and many methods used to intercept the array of segments is an element with a difference value comprises return lrange rightmost
lrange command also supports negative index, the table is calculated from the right side of the ordinal numbers, such as '1' represents a rightmost element, '2' indicates the rightmost second element, and so once
Delete the value specified in the list of
lrem key count value
lrem command to delete the list of the first count value is value elements, the return value is the number of elements actually removed. Depending on how the different count values, lrem command will be executed
Slightly different
When the count> 0 Shi, lrem command will start removing elements of the first count value from the left is a list of
When the count <0, lrem command will start removing elements of the first count value value from the right list
When the count = 0 Shi, lrem command deletes all the elements of the value value
Get / set value specified index element
lindex key index
lset key index value
lindex command to return the element to the specified index, from index 0, it means that if the index is negative index is counted from the right, the right most element in the index is -1
lset through the command list index operation, it will index element is assigned index value
Leaving only the list of designated segments
ltrim key start end
ltrim command to remove all elements other than the specified range of indexes, specify a list of its methods and the scope of the same command lrange
When ltrim commandments and lpush command to limit the number of elements in the list together, such as when logging we want to keep only the most recent 100 logs, each time adding new elements
Command can be called once ltrim;
Insert elements to the list
linsert key before | after pivot value
linsert command may pivot from left to right to find the value of the element in the list, and in accordance with the second parameter is determined before or after the value of the element is inserted into
Before or after, if the command is successful, the operation returns the length of the list after the insertion is completed. If the pivot -1 if key is not found absent or empty, return 0
An element from a list to another list R
rpoplpush source destination
rpoplpush first implementation rpop command execution lpush command. rpoplpush first command brings up a list of source elements from the right side of the key type, then added to the left of the destination list of types of keys, and returns the value of the element, the whole process is atom
Collection Type
Increase Delete command
sadd key member [ member .... ]
srem key member [ member .... ]
sadd command to add one or more elements to the collection, if the key does not exist is created automatically. Because you can not have the same element in a set, so that if the element to be added
Su already exists in the collection will ignore this element. The return value is the number of elements successfully added (ignoring the elements are not counted)
srem command to delete one or more elements from the collection and returns the number of successfully deleted
Get all the elements in the collection
smembers key
Returns all elements in the collection
Determining whether the element in the collection
sismember key member
Determining whether an element in a set time complexity is O (1) operations, regardless of how many elements in the set, sismember command always fast return results. when
When the value 1 is present sismember command returns, returns 0 when the value does not exist or bond is absent
Inter set operations
sdiff key [ key ...... ]
sdiff command to perform a plurality of sets of the set difference operation. A difference with the current set of set B is represented as A- B, A belonging to the representative set of all elements not belonging to the B configuration, i.e.
A - B = {x | x∈A and x ∈ / B}
Command to use:
Sadd arrow 1 2 3 4 6 7 8
sadd setb 2 3 4
sdiff arrow SETB
The incoming command simultaneously a plurality of keys, the calculation order is calculated setb seta and difference sets in the calculation result setc
sadd setc 2 3 4
sdiff arrow SETB SETC
sinter key [ key ..... ]
This command is used to perform a plurality of set intersection operator. Intersection of the sets A and B is represented as a set of A∩B, representing all the elements belonging to the A and B belong to the configuration set
I.e. A∩B = {x | x∈A and x ∈B}
Command to use:
seta sinter SETB
The command also supports multiple keys at the same time passing
sunion key [ key ...... ]
This command is used to perform a plurality of sets and set operations. Set A and set B and set expressed as A∪B, A represents all or all of the elements belonging to the configuration set belonging to B
I.e. A∪B = {x | x∈A or x ∈B}
Command to use:
Sunion arrow SETB
The command also supports multiple keys at the same time passing
Obtaining the number of elements in the set
scard key
A set operation and store the result
sdiffstore destination key [ key ...... ]
sinterstore destination key [ key ...... ]
sunionstore destination key [ key ...... ]
sdiffstore sdiff command and command functions the same, the only difference is that the former does not return a direct result of the operation, but will result in the presence of key destination
sinterstore This command is similar to the sinter command, but it will save the results to a destination collection, rather than simply return a result set.
sunionstore This command is similar to sunion command, but it will save the results to a destination collection, rather than simply return a result set.
Random access to elements in the collection
srandmember key [ count ]
This command is used to obtain a random element from the collection
You can also pass parameters to count a random access to a plurality of elements, different positive or negative count, or different specific performance
When the count is positive, srandmember will randomly get a count does not get repeated elements from the collection. If the count value is larger than the number of elements in the set, then
srandmember returns all elements in the collection
When the count is negative, srandmember randomly obtained from the collection | count | one of the elements that may have the same
Note: When the transfer count parameter, parameter error in command prompt windows environment
Pops an element from the collection
spop key
Since the type of the collection element is disordered, so spop command randomly selected from a set of pop-up element, returns a random element value is removed, or if the key does not exist
When the key is an empty set, returns nil
Indexed collections (sorted set)
Add elements
zadd key score member [ score member ...... ]
zadd command to add a fraction of the element and to the ordered set of elements if the element is already present, will be replaced with a new score for the original score. Return zadd command
Is the new value added to the number of elements in the set (previously existing elements does not include)
Score points elements
zscore key member
Get ranked in a range of list elements
zrange key start stop [ withscores ]
zrevrange key start stop [ withscores ]
zrange command ascending order of the index from start to return all of the elements between the stop (containing the element ends) in accordance Element scores. zrange command and lrange life
So very similar, as are the index from 0, A negative number lookup from back to front (-1 is the last element). If you need to receive a score of elements, it can be
Tail zrange command parameters plus widthscores
NOTE: If the score of the same two elements, redis will be arranged in alphabetic order (i.e., 0 <9 <A <Z <a <z in that order). If the value of the element is Chinese, it depends
Chinese encoding, as shown:
zrevrange command and zrange only difference is that the zrevrange element according to descending order of scores given results
Gets the element within the specified range of scores
zrangebyscore key min max [ withscores ] [ limit offset count ]
The command element according fraction ascending sequence returns to the score between the max min (comprising elements of min and max)
If desired fraction range of values not inclusive, may be added "(" symbol before the score, for example: 80 want to return data assigned 100 points, 80 points may comprise 100 does not contain a single
命令:zrangebyscore scoreboard 80 (100 widthscores
min and max also supports infinite, like zadd command, -inf and + inf represent negative infinity and positive infinity. For example we want all the scores higher than 80 points (not
It contains 80 points) a list of people, but do not know how much is the highest score, which is you can use + inf
zrangebyscore scoreboard (80 +inf
SQL command limit offset and the usage count is substantially the same, i.e., the offset rearwardly offset elements on the basis of the list of elements of the first count and fetch only the element
Vegetarian
zrevrangebyscore order not only to give small element according to the results from the large fraction, and his parameters min and max of the command sequence and relative zrangebyscore
opposite
Increase the score of an element
zincrby key increment member
zincrby can increase the score of a command element, the return value is the score changes, like for example to add 4 points peter
zincrby scoreborder 4 peter
increment may be negative numbers Save points
zincrby scoreborder -4 peter
If the specified element does not exist, redis will first build it before executing command and his value assigned to perform operations 0
Get the number of elements in the collection
zcard key
Obtaining the number of elements within the range specified fraction
zcount key min max
Min max zcount characteristic parameters and commands in the same command zrangebyscore
Delete one or more elements
zrem key member [ member .... ]
The number of elements zrem command return value is successfully deleted (does not contain elements which never existed)
Delete elements in accordance with the position range
zremrangebyrank key start stop
According to the number of elements in ascending order of the score (i.e., the minimum value of the index indicates 0) removes all elements within the specified range of positions and returns the deleted element
Delete elements in accordance with the range of scores
zremrangebyscore key min max
zremrangebyscore command to delete all the elements within the specified range fraction, as the characteristic parameter and the min and max zrangebyscore command, the return value is deleted
The number of elements
Get ranked element
zrank key member
zrevrank key member
zrank command from small to large fraction of elements in accordance with the order to obtain the specified element rank (from zero, that is, the smallest element of ranking score of 0)
zrebrank command by contrast, scores ranked as the largest element 0
Computes the intersection ordered set
zinterstore destination numkeys key [ key ... ] [ weights weight [ weight ... ] ] [ aggregate SUM | MIN | MAX ]
zinterstore command to compute the intersection of the plurality of ordered sets disease store the result in the destination keys (likewise ordered set type is stored), the return value destination
The number of key elements, the key elements destination points is determined by the aggregate parameters
1. When the aggregate is SUM (i.e. default), the score is the destination of the key elements in each set of score elements involved in the calculation and
MIN 2. When the aggregate is, the destination key element score is the minimum of the set of elements involved in the calculation of the score
3. When the aggregate is MAX, the fractional destination is a collection of key elements involved in the calculation of the fraction of the maximum element
zinterstore command can set weights for each set of weights by the weight parameter, a score for each element in the set involved in the calculation of the weight is multiplied by a weight set
Such as:
Set between the calculated and set
zunionstore
Usage and usage zinterstore command like
Affairs
The principle of the transaction is the first part of a transaction command to redis, and then executes the commands in turn let redis
Error Handling
(1) syntax error. Syntax error means that the command does not exist or the number of command parameters wrong. In this case, the transaction as long as there is a command syntax error, after an exec command redis
Will directly return an error, even grammatically correct order will not be executed
Note: The previous version of 2.6.5 redis ignores the command syntax errors, and then execute a transaction other syntactically correct command.
(2) run-time error. Runtime error means an error in the execution command, the command type such as a hash key type set of operations, such errors prior to actual implementation is not redis
Discovery method, so this command will be accepted and executed transactions in redis, if the transaction is in command of a run-time error occurs, the transaction in other commands will still continue
Continued execution (including command after command error)
reids transaction rollback feature does not provide the relational database transaction, the developer must clean up their stalls in the rest of the mistakes that end after transaction execution
watch command
watch key [ key ... ]
Monitor one or more key, if this or these key changes are the implementation of other commands before the transaction, then the transaction will be interrupted, continued to monitor the exec command
unwatch
Cancellation watch command to monitor all the key
Survival time
expire
Use expire command to expire Key seconds, where seconds parameter represents the key lifetime in seconds, this parameter must be an integer
1 represents a setting command returns a success return 0 represents a bond or absence of failure is provided
If you want to know how long a key is deleted, you can use the ttl command. The return value is the remaining time key (in seconds),
If you want to cancel the survival time setting key (key recovery will soon become permanent), you can use the command persist. If the time is successfully cleared 1 is returned. Otherwise it returns 0
In addition to persist command, use the set, getset command key assignment will also clear key survival time
Note: incr, lpush, hset, zrem command will not survive time image key
Precise control of key survival time should be used pexpire command. The command unit is ms
Command may be used pttl milliseconds return key remaining time
Also less common commands: Deadline expireat and pexpireat, the command of the second parameter represents a bond survival time, expireat milliseconds in seconds pexpireat
sort
This command can list type, set type, and sort key indexed collections
List type:
When ordered collection type sort ignores elements of the score, only to be sorted for the value of the element itself
In addition to numbers may be arranged, sort command may also be implemented non-numeric elements arranged in alphabetic order by the parameter alpha
desc sort command parameter can be implemented elements arranged in descending order
sort command also supports the limit parameter to return the result of the specified range, and the use of the sql statement as limit offset count, skip the first offset represents the elements and get after
count elements
sort command by parameter, by default, sort uid directly ordered by values of uid through by parameters help uid in accordance with other key elements to sort
user_level_ * is a placeholder, first he takes a value in the uid, and then uses this value to locate the appropriate key
For example, in uid to sort the list, the program will first remove the uid value of 1, 2, 3, 4, then user_level_1, user_level_2, user_level_3
And as the value user_level_4 uid weight for weight.
Use get option, a corresponding key may be taken based on the results of the sorting
A sort command can get multiple parameters (but by only one parameter)
sort command will sort the results directly back to default, if you want to save sort the results, you can use store parameters, save the key of type list type
@ 2017-07-17 17:16. Posted MrSunny read (...) Comments (...) edit collections