Redis String Type and Hash type learning notes and summarizing _redis

Source: Internet
Author: User
Tags memcached object serialization redis strlen


Linux Version Information:


code as follows:

Cat/etc/issue or Cat/etc/redhat-release (Linux view version of the current operating system release information)


CentOS Release 6.6 (Final)





(i) String type



The "definition" string is the simplest type, and you can understand that the Memcached is exactly the same type, a key corresponds to a value, and the operation supported on it is similar to the operation of the Memcached. But it's more functional.



The string type is binary safe. The Redis string can contain any data, such as a JPG image or a serialized object. From an internal implementation point of view, a string can be considered a byte array, with a maximum upper limit of 1G bytes.



Operation



①set method



The value of the SET key corresponds to the string type.



"Example" adds name = Dee's key value pair:


 code as follows:

127.0.0.1:6379> Set name Dee
Ok

Setup succeeded.





②get method



Cases


 code as follows:

127.0.0.1:6379> Get Name
"Dee."

Get success.








Set name = Deathmask again, and the value will be overwritten:


code as follows:

127.0.0.1:6379> Set name Deathmask
Ok


127.0.0.1:6379> Get Name
"Deathmask"





③setnx method



Setting the key corresponds to the value of string type, and if the key already exists, returning 0,NX represents not exist.



Cases


code as follows:

127.0.0.1:6379> setnx Name Dee
(integer) 0



127.0.0.1:6379> setnx App Weibo
(integer) 1


127.0.0.1:6379> Get Name
"Deathmask"


127.0.0.1:6379> Get App
"Weibo"





Indicates that name already exists and has not changed, and that the app does not exist succeeds in the set.



④setex method



Sets the value of the key to the string type, and specifies the validity period for this key value.



Cases


 code as follows:

127.0.0.1:6379> Setex Mobile iphone
Ok


127.0.0.1:6379> Get Mobile
"iphone"


127.0.0.1:6379> Get Mobile
"iphone"


127.0.0.1:6379> Get Mobile
(nil)





Set the mobile value to the IPhone and specify a validity period of 10 seconds.



⑤setrange method



Sets the substring of the value of the specified key



"Example" replaces Dee's 126 mailbox with a Gmail mailbox.


 code as follows:

127.0.0.1:6379> Set Email dee@126.com
Ok


127.0.0.1:6379> Get Email
"Dee@126.com"


127.0.0.1:6379> SetRange Email 4 gmail.com
(integer) 13


127.0.0.1:6379> Get Email
"Dee@gmail.com"


Where 4 represents subscript, (integer) 13 indicates the length of the string.





"Example 2"


 code as follows:

127.0.0.1:6379> Set Email dee@vip.ofim.com
Ok


127.0.0.1:6379> Get Email
"Dee@vip.ofim.com"


127.0.0.1:6379> SetRange Email 4 gmail.com
(integer) 16


127.0.0.1:6379> Get Email
"Dee@gmail.comcom"


Note: If the replacement string has no long source string, only characters of the same length in the source string are replaced, while the remaining characters in the source string are preserved.





⑥mset method



Set multiple key values at once and return OK successfully, indicating that all values are set and the failure returns 0, indicating that no values are set.



Cases


code as follows:

127.0.0.1:6379> mset key1 dee1 key2 dee2
Ok


127.0.0.1:6379> Get Key1
"Dee1"


127.0.0.1:6379> Get Key2
"Dee2"





⑦msetnx method



Sets the value of multiple keys at once, successfully returning 1, indicating that all values are set. Failure returns 0, indicating that no values are set, but no existing key is overwritten.


 code as follows:

127.0.0.1:6379> msetnx Key3 dee3 key4 dee4
(integer) 1



127.0.0.1:6379> msetnx Key3 dee333 key4 dee444 key5 dee5
(integer) 0


127.0.0.1:6379> Get Key3
"Dee3"


127.0.0.1:6379> Get Key4
"Dee4"


127.0.0.1:6379> Get Key5
(nil)


⑧getset Method





Sets the value of the key and returns the old value of the key.



Cases


 code as follows:

127.0.0.1:6379> Getset Key1 Emperor
"Dee1"





⑨getrange method



Gets a substring of the value of the key.


 code as follows:

127.0.0.1:6379> Get Email
"Dee@gmail.comcom"



127.0.0.1:6379> GetRange Email 0 12
"Dee@gmail.com"


Returns the No. 0 to 12th characters.
⑩mget Method





Gets the value of more than one key at a time, and returns nil if the corresponding key does not exist.



Cases


code as follows:

127.0.0.1:6379> mget key1 Key2 Key3 Key4
1) "Emperor"
2) "Dee2"
3) "Dee3"
4) "Dee4"
5) (nil)





⑪incr method
Does a Gaga operation on the value of the key and returns a new value.



"Example" increments by 1 each time


 code as follows:

127.0.0.1:6379> Set Age 28
Ok


127.0.0.1:6379> Get Age
"28"


127.0.0.1:6379> incr Age
(integer) 29


127.0.0.1:6379> incr Age
(integer) 30


127.0.0.1:6379> Get Age
"30"


"Example 2"
 code as follows:

127.0.0.1:6379> Get Real-age
(nil)


127.0.0.1:6379> incr Real-age
(integer) 1


127.0.0.1:6379> Get Real-age
"1"





⑫incrby method



Similar to the INCR method, add the specified value, the key does not exist will set the key, and that the original value of 0.



Cases





 code as follows:

127.0.0.1:6379> Get times
(nil)


127.0.0.1:6379> Incrby Times 5
(integer) 5


127.0.0.1:6379> Get times
"5"


"Example 2"
 code as follows:

127.0.0.1:6379> Incrby times-5
(integer) 0



127.0.0.1:6379> Get times
"0"





⑬decr method



The value of the key is reduced operations.



⑭decrby method



Similar to the Decr method, subtract the specified value.



Cases


 code as follows:

127.0.0.1:6379> Get Age
"30"



127.0.0.1:6379> DECR Age
(integer) 29


127.0.0.1:6379> Decrby age 10
(integer) 19


127.0.0.1:6379> Decrby age-10
(integer) 29





⑮append Method (Important)



Appends a value to the string for the specified key, returning the length of the new string.



Cases


 code as follows:

127.0.0.1:6379> Get Name
"Deathmask"



127.0.0.1:6379> Append name @163.com
(integer) 17


127.0.0.1:6379> Get Name
"Deathmask@163.com"





⑯strlen method



The length of the value value of the specified key.



Cases


 code as follows:

127.0.0.1:6379> Get Name
"Deathmask@163.com"



127.0.0.1:6379> strlen Name
(integer) 17





(b) Hash type



The Redis hash is a string-type field and value mapping table. Its addition, deletion operation is 0 (1) (average operation).



Hashing is particularly useful for storing objects. Rather than Gencun each word of an object to a single string type (string can store object serialization).



Storing an object in a hash type consumes less memory and makes it easier to access the entire object.



(The reason for saving memory is that when a new hash object is created, it is stored with a zipmap (also known as a small hash).



This zipmap is not actually a hash table, but zipmap than the normal hash implementation can save a lot of hash itself need some metadata storage overhead.



Although Zipmap additions, deletions, and lookups are all O (n), the use of Zipmap is quick because the number of objects in the general object is not too large, that is, adding the deletion average or O (1).



If the size of field or value exceeds a certain limit, Redis automatically replaces the zipmap with a normal hash implementation internally. This restriction can be specified in the configuration file.



Hash-max-zipmap-entries #配置字段最多 64, Hash-max-zipmap-value #配置 value maximum of 512 bytes)



Operation



①hset Method/Hget method



Sets the hash field to the specified value, and if the key does not exist, it is created first.



Cases

code as follows:


127.0.0.1:6379> hset myhash field1 Hello
(integer) 1





Description: Myhash is the name of the hash table, field1 is a field in the hash table, Hello is the corresponding value of this field. Storage users are more convenient.



"Example 2"


 code as follows:

127.0.0.1:6379> hset user:001 Name Dee
(integer) 1



127.0.0.1:6379> hget user:001 Name
"Dee."





Note: Set User table ID 001, user name is Dee.



You can think of user:001 as a table.



②hsetnx method



Sets the hash field to the specified value, and if the key does not exist, it is created first, and if it exists, returns 0.



Cases

 code as follows:


127.0.0.1:6379> hsetnx myhash Field "Hello"
(integer) 1



127.0.0.1:6379> hsetnx myhash Field "Hello!"
(integer) 0


127.0.0.1:6379> hget myhash Field
"Hello"





③hmset method



Set multiple field of hash at the same time



Cases

 code as follows:


127.0.0.1:6379> hmset user:002 name Dee specialty PHP
Ok


127.0.0.1:6379> hget user:002 Name
"Dee."


127.0.0.1:6379> Hget user:002 Specialty
"PHP"





④hget method



Gets the specified hash field.



Cases


 code as follows:

127.0.0.1:6379> hmget user:002 Name Specialty
1) "Dee"
2) "PHP"





⑤hincrby method



The specified hash field plus the given value.



Cases


 code as follows:

127.0.0.1:6379> hmset user:002 name Dee Specialty Redis Age 28
Ok


127.0.0.1:6379> Hincrby user:002 age-6
(integer) 22


127.0.0.1:6379> hmget user:002 Name Specialty Age
1) "Dee"
2) "Redis"
3) "22"


127.0.0.1:6379> Hincrby user:002 Age 5
(integer) 27


127.0.0.1:6379> hmget user:002 Name Specialty Age
1) "Dee"
2) "Redis"
3) "27"





⑥hexists method



Tests whether the specified field exists.



Cases


code as follows:

127.0.0.1:6379> hexists user:002 Name
(integer) 1



127.0.0.1:6379> hexists user:002 Sex
(integer) 0


⑦hlen Method





Returns the field number of the specified hash.



Cases


 code as follows:

127.0.0.1:6379> Hlen user:002
(integer) 3

⑧hdel Method





Deletes the field for the specified hash.



Cases


 code as follows:

127.0.0.1:6379> Hdel user:002 Age
(integer) 1



127.0.0.1:6379> Hget user:002 Age
(nil)


⑨hkeys method (similar to the Array_keys () method in PHP)





Returns all field for the hash.



Cases

 code as follows:


127.0.0.1:6379> Hkeys user:002
1) "Name"
2) "Specialty"

⑩hvals method (similar to the Array_values () method in PHP)





Returns all the value of the hash.



Cases


 code as follows:

127.0.0.1:6379> hvals user:002
1) "Dee"
2) "Redis"

⑪hgetall Method





Gets all the field and value in a hash.



Cases


 code as follows:

127.0.0.1:6379> Hgetall user:002
1) "Name"
2) "Dee"
3) "Specialty"
4) "Redis"





Summary Sets the hash type of data, first set the hash table, then set the hash field, and finally set the hash field value.


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.