Redis key and Value

Source: Internet
Author: User
Tags set set

Exists key

Del key1 key2

Redis vaule
Redis provides five data types: String, hash, list, set, and sorted set.

String is the most basic type, and the string type is binary secure. It indicates that the redis string can be
Contains any data. For example, JPG images or serialized objects. From the internal implementation, the string can be regarded as a byte array, and the maximum value is 1 GB.

Set key value: set the value of the string type corresponding to the key. If the return value is 1, the value is successful, and the return value is 0.
Setnx key value if the key does not exist, set the value of the string type corresponding to the key. If the key already exists, return
Return to 0. Set if not exists 』

After set a 1, get a returns "1", and type A is a string.
Get key obtains the string value corresponding to the key. If the key does not exist, Nil is returned.
GetSet key value first obtains the key value and then sets the key value. If the key does not exist, Nil is returned.

Mget key1 key2... keyn obtains the values of multiple keys at a time. If the corresponding key does not exist, Nil is returned.
Mset key1 value1 ...... keyn valuen sets the value of multiple keys at a time. If the value is successfully returned, 1 indicates that all values are set.
0 indicates that no value is set.
Msetnx key1 value1 ...... keyn valuen sets the value of multiple keys at a time, but does not overwrite the existing key.
Incr key performs the ++ operation on the key value and returns a new value. Note that an incr value other than int will return an error.
If incr does not exist, set the key value to 1.
Decr key performs the -- operation on the key value. If decr does not have a key, set the key value to-1.
Incrby key integer adds a specified value to the key. If the key does not exist, the key is set and the original value is considered
It is 0.
Decrby key integer minus the specified value. Decrby is completely for readability. We can use incrby
A negative value to achieve the same effect, and vice versa.

 

2.2.2. Hash type
Hash is a ing table between fields and values of the string type. Add and delete operations are O (1) (average ).
Hash is particularly suitable for Object Storage. Each field of the object is saved as a single string type. Convert an object
The storage in the hash type consumes less memory and makes it easier to access the entire object. The reason for saving memory is:
Zipmap (also known as small hash) is used to store a new hash object. This zipmap actually does not
It is not a hash table, but zipmap can save a lot of the elements required by hash compared to normal hash implementation.
Data storage overhead. Although the addition, deletion, and query of zipmap are O (N ),
There are not many. Therefore, the use of zipmap is also very fast, that is to say, the average addition and deletion is still O (1 ). If Field
Or when the value size exceeds a certain limit, redis will automatically replace zipmap with a normal hash internally.
This restriction can be specified in the configuration file.
Hash-max-zipmap-entries 64 # up to 64 configuration fields
Hash-max-zipmap-value 512 # set the maximum value to 512 bytes.

 

Hset key field value: Set hash field to the specified value. If the key does not exist, create

Return Value:
If FieldIs a new field in the hash table, and the value is set successfully, return 1. If the field in the hash table is FieldThe existing value and the old value have been overwritten by the new value. 0.
Redis> hset website Google "www.g.cn" # set a new domain (integer) 1 redis> hset website Google "www.google.com" # overwrite an old domain (integer) 0

Hget key field to obtain the specified hash field.
Hmet key filed1. .. fieldn get all the specified hash filed.
Hmset key filed1 value1 ...... filedn valuen simultaneously sets multiple hash fields.
Hincrby key field integer adds the specified hash filed value. Return the modified hash filed
Value.
Hexists key field checks whether the specified field exists.
Hdel key field: deletes the specified hash field.
Hlen key returns the number of fields in the specified hash.

Hkeys key returns all hash fields.
Hvals key returns all values of hash.
Hgetall returns all the filed and value of the hash.

 

List type: Reference: http://www.cnblogs.com/youxin/p/3585867.html

 

2.2.4. Set Type
A set is an unordered set. It can contain up to 32 to 1 elements of 2. Set is implemented through hash table,
Therefore, the complexity of adding, deleting, and searching is O (1 ). The hash table is automatically adjusted with the addition or deletion.
Note that synchronization (get write lock) is required when the hash table size is adjusted to block other read/write operations. Not possible
After a long time, it will be implemented using the Skip table (Skip List. The jump table is already used in sorted sets. About the Set set type
In addition to basic addition and deletion operations, other useful operations include union, intersection, and ),
Difference ). Through these operations, you can easily implement the friend recommendation and blog tag FUNCTIONS In SNS.

Sadd key member [member...]

Set one or moreMemberAdd element to setKeyWhich already exists in the SetMemberThe element is ignored.

Assume thatKeyIf not, createMemberAn element is a set of members.

WhenKeyIf it is not a set type, an error is returned.

# Add a single element redis> sadd BBS "discuz.net" (integer) 1 # Add duplicate elements redis> sadd BBS "discuz.net" (integer) 0 # add multiple elements redis> sadd BBS "Tianya.cn" "groups.google.com" (integer) 2 redis> smembers bbs1) "discuz.net" 2) "groups.google.com" 3) "Tianya.cn"

Smembers key return setKeyAll members in.

Srem key member removes the specified element from the set corresponding to the key. If member is successful, 1 is returned.
If the key does not exist or the key does not exist, 0 is returned. If the key does not correspond to a set value, an error is returned.
The spop key is deleted and a random element in the set corresponding to the key is returned. If the set is null or the key does not exist
Nil.
Srandmember key is the same as spop. An element in the set is randomly selected, but no element is deleted.
Smove srckey dstkey member removes the Member from the srckey's corresponding set and adds it to the corresponding set of dstkey,
The entire operation is atomic. 1 is returned if the member does not exist in the srckey, and 0 is returned if the key is not set
Type return error.
Scard key returns the number of set elements. If set is null or the key does not exist, 0 is returned.
Sismember key member determines whether the Member is in the set. If yes, 1 or 0 is returned, indicating that the member does not exist or the key is not
Yes.
Sinter key1 key2 ...... Keyn returns the intersection of all given keys.
Sinterstore dstkey key1 ...... keyn returns the intersection of all given keys and stores them under dstkey.
Sunion key1 key2... keyn returns the union of all given keys.
Sunionstore dstkey key1 ...... keyn returns the union of all given keys and saves the Union to the dstkey.
Sdiff key1 key2... keyn returns the difference set of all given keys.

 

Sorted set type
Sorted set is an ordered set. It adds an ordered Attribute Based on the set.
You can specify the element to be modified. After each element is specified, the sequence is automatically adjusted based on the new value. It can be understood that there are two columns
MySQL table, stores values in one column, and stores values in one column. In the operation, the key is interpreted as the name of sorted set.

 

Zadd key score member [[score member] [score member]...]

Set one or moreMemberElements andScoreValues are added to the sorted set.Key.

IfMemberIf it is already a member of the sorted set, update thisMemberOfScoreValue, and re-insert thisMemberTo ensureMemberIn the correct position.

ScoreThe value can be an integer or a double-precision floating point number.

IfKeyOtherwise, an empty ordered set is created and the zadd operation is executed.

WhenKeyAn error is returned when an ordered set exists but is not of the type.

For more information about Ordered Sets, see sorted set.

# Add a single element redis> zadd page_rank 10 Google.com (integer) 1 # add multiple elements redis> zadd page_rank 9 Baidu.com 8 bing.com (integer) 2 redis> zrange page_rank 0-1 withscores1) "bing.com" 2) "8" 3) "Baidu.com" 4) "9" 5) "Google.com" 6) "10" # Add an existing element, and the score value remains unchanged redis> zadd page_rank 10 Google.com (integer) 0 redis> zrange page_rank 0-1 withscores # Not changed 1) "bing.com" 2) "8" 3) "Baidu.com" 4) "9" 5) "Google.com" 6) "10" # Add an existing element, but change the score value to redis> zadd page_rank 6 bing.com (integer) 0 redis> score of zrange page_rank 0-1 withscores # bing.com element is changed. 1) "bing.com" 2) "6" 3) "Baidu.com" 4) "9" 5) "Google.com" 6) "10"

Zrem key member [member...]

Remove an Ordered SetKeyThe member does not exist.

WhenKeyAn error is returned when an ordered set exists but is not of the type.

 

Reference: https://redis.readthedocs.org/en/latest/

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.