Common data types for redis--(1) Redis __redis

Source: Internet
Author: User
As a key-value database with excellent performance, redis supports multiple data structures. There are several types of strings–list–set–sorts set–hash. It is necessary to understand common operations. strings

Strings: Strings are binary-safe, which means that a redis string can contain any type of data, such as a JPEG image or a serialized array or object. A string can contain up to 512M content.

SET key value [EX #] [NX | XX] // Set the key-value. NX indicates that the value is set when the key does not exist. Do not set if present. XX is the opposite. Add expiration time after EX. There are also PX types, milliseconds.
GET
INCR // Add or subtract numbers in key, which can only be operated on integers.
DECR
EXIST

SETEX key seconds value
Associate the value with the key and set the key's lifetime to seconds. If key already exists, the SETEX command will overwrite the old value.
Return value: OK when the setting is successful. When the seconds parameter is invalid, an error is returned.

SETRANGE key offset value
Use the value parameter to overwrite the string value stored by the given key, starting at offset. Non-existent keys are treated as empty strings.
-Return value: The length of the string after being modified by SETRANGE.

MSET key value [key value ...]
Set one or more key-value pairs at the same time. If a given key already exists, MSET will overwrite the old value with the new value. If this is not the effect you want, consider using the MSETNX command: it will only work if all the given keys do not exist Perform the setting operation.
-Return value: Always returns OK (because MSET cannot fail)

GETRANGE key start end
Returns the substring of the string value in key. The truncation range of the string is determined by the offsets of start and end (including start and end). A negative offset means counting from the end of the string, -1 means the last character, -2 means the second to last, and so on.
-Return value: The intercepted substring.

APPEND key value
If key already exists and is a string, the APPEND command appends value to the end of the original value of key. If the key does not exist, APPEND simply sets the given key to value, just as if executing SET key value.
-Return value: After appending value, the length of the string in key lists

Lists: The keys point to a container. You can add strings on the left and right, the main function is push.pop. Get all the values of a range, and so on. Lists can be treated as lists or stacks.

LPUSH // `LPUSH SS MON` defines a list, or adds value to the left. Returns the length of the list.
RPUSH // This is the right-hand increase value, the above is the right-hand increase.
LPOP // Left. What is returned is the popped element. It's gone after playing. If it does not exist, it will return null.
RPOP // Right.
LINDEX
LSET // LSET key index value Sets the value of the element whose index is indexed by list key to value. When the index parameter is out of range, or an LSET is performed on an empty list (key does not exist), an error is returned. Return value: The operation returns OK, otherwise it returns an error message.
LINSERT // LINSERT key BEFORE | AFTER pivot value Inserts the value value into the list key, before or after the value pivot. When pivot does not exist in the list key, no action is performed. When the key does not exist, the key is treated as an empty list and no action is performed. If key is not a list type, an error is returned.
return value:
If the command succeeds, returns the length of the list after the insert operation is complete. If no pivot is found, -1 is returned. If key does not exist or is an empty list, 0 is returned.

LTRIM // LTRIM key start stop Trim a list, that is, let the list keep only the elements in the specified range, and the elements not in the specified range will be deleted. Both the index and start parameters start and stop are zero-based, that is, 0 is the first element of the list, 1 is the second element of the list, and so on. You can also use negative subscripts, with -1 representing the last element of the list, -2 representing the penultimate element of the list, and so on. When key is not a list type, an error is returned.
Return value: When the command is executed successfully, ok is returned.

RPOPLPUSH // RPOPLPUSH source destination command RPOPLPUSH performs two actions in one atomic time: 1 Pops up the last element (tail element) in the list source and returns it to the client. 2. Insert the element popped by source into the list destination as the head element of the destination list. If source does not exist, the value nil is returned and no other action is performed. If source and destination are the same, the footer element in the list is moved to the head and the element is returned. This special case can be considered as a rotation operation of the list.
Returns: the element being popped.
sets
Sets: up to 4 billion elements.

SADD // Create a new collection. `SADD w1 xxx xxx xx`. You can also add new elements, and duplicate elements are ignored. Returns the number of new elements added to the collection, excluding ignored elements.
SINTER // Insert.
SUNION // SUNION key [key] Returns a list of all members of a collection.
SPOP // Randomly pop. If it does not pop up and just check it, use the SRANDMEMBER command. Return value: the removed random element. Or empty.
SISMEMBER // Check if an element is in the list.
SINTER // Find the intersection.
SDIFF // SDIFF key [key] Returns all members of a set, which is the difference product between all given sets. Return value: A list containing members of the difference product.
SMOVE sources destination member moves the member element from the source collection to the destination collection. No operations are performed on elements that are not in the source collection, otherwise they are performed. If the source and destination have this element, the source is removed. The element does not exist and returns an error. Returns 1 on success, or 0 if it is not an element of source.
sorted sets
Sorted Sets: // Ordered sets.

ZADD // Define or add index. ZADD key score member [[score member] [score member] ...]
ZRANGE // Indicate the start and end indexes.
ZCARD // definition
ZRANK // ZRANK key member Returns the rank of the member. The members of the ordered set are incremented according to the score to rank as 0. The ZREVRANK command can be used to get the ranking of members in descending (from large to small) score values. The ranking is 0, which means that the member with the lowest score is ranked 0. Use the ZREVRANK command to get members ranked in descending (highest to lowest) scores.
Return value: If member is a member of the ordered set key, the ranking of member is returned. If member is not a member of the ordered set key, returns nil
ZINCRBY // ZINCRBY key increment member is the score value of member member of the set key plus the increment. You can pass a negative value increment to subtract the corresponding value from the score, such as ZINCRBY key -5 member, which is to subtract 5 from the member's score. When key does not exist, or member is not a member of key, ZINCRBY key increment member is equivalent to ZADD key increment member.
When key is not an ordered set type, an error is returned. The score value can be an integer value or a double-precision floating-point number.
Return value: The new score value of the member, represented as a string.
ZREMRANGEBYRANK key start stop // Remove all members in the ordered rank key from the ordered set key. The interval is indicated by the subscript parameters start and stop, respectively, including start and stop. The subscript parameters start and stop both have 0 as the base, that is, 0 represents the first member of the ordered set, 1 represents the second member of the ordered set, and so on. You can also use a negative index, with -1 for the last member, -2 for the penultimate member, and so on. Returns: the number of members removed.

ZADD weekday1 1 mon 2 tue 3 web // Define an ordered collection. Or add. Or update the collection. Returns the number of new members successfully added, excluding updated, existing members.
ZRANGE weekday1 // returns integer 3
ZRANGE weekday1 tue // returns integer 1
ZSCORE weekday1 1 // Returns SCORE according to the element.
hash
Hash: Associative Array, introduced in 2.0. Redis hash is a mapping table of field and value of string type. Its addition and deletion operations are 0 (1) (average). Hash is particularly suitable for storing objects. Instead of storing each field of the object as a single string type. Storing an object in the hash type takes up less memory and makes it easier to access the entire object.

 HSET // HSET key field value Set the value of the field field in the hash table key to value. If key does not exist, a new hash table is created and HSET operation is performed. If the field field already exists in the hash table, the old value will be overwritten.
Return value: If field is a newly created field in the hash table, and the value is set successfully, 1 is returned. If the field field in the hash table already exists and the old value has been overwritten by the new value, 0 is returned.

  HMSET // HMSET key field value [field value ...] Set multiple field-value pairs into the hash table key at the same time. This command overwrites a domain that already exists in the hash table. If key does not exist, an empty hash table is created and an HMSET operation is performed.
Return value: If the command is executed successfully, return OK. When key is not a hash type, an error is returned.

  HINCRBY // HINCRBY key field increment adds the increment value to the value of the field field in the hash table key. The increment can also be negative, which is equivalent to subtracting the given field. If key does not exist, a new hash table is created and the HINCRBY command is executed. If the field field does not exist, the field value is initialized to 0 before the command is executed. Performing a HINCRBY command on a field that stores a string value will cause an error.
Return value: After executing the HINCRBY command, the value of the field field in the hash table key

HMGET // HMGET key field [field ...] Returns the value of one or more given fields in the hash table key. If the given domain does not exist in the hash table, then a nil value is returned.
Return value: A table containing the associated values of multiple given fields. The order of the table values is the same as the order of the given parameters.

 HSETNX //

HGET // value
HKEYS // Returns all fields in the hash table Key.
Return value: A table containing all fields in the hash table. When the key does not exist, an empty list is returned.
HVALS //
HDEL // Delete

hash h1 amon // Increase the key value
hash h1 b tue
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.