Redis string type and hash type learning notes and summary

Time:2020-2-7

Linux version information:

Copy codeThe code is as follows:
Cat / etc / issue or cat / etc / RedHat release

CentOS release 6.6 (Final)

(1) String type

[definition] string is the simplest type, you can understand that it is exactly the same type as Memcached, a key corresponds to a value, and the operations on it are similar to those of Memcached. But it’s more functional.

String types are binary safe. This means that redis string can contain any data, such as JPG images or serialized objects. In terms of internal implementation, string can be regarded as byte array, and the maximum limit is 1g bytes.

[operation]

① Set method

Set the value corresponding to key to the value of string type.

[example] add the key value pair with name = Dee:

Copy codeThe code is as follows:
127.0.0.1:6379> set name dee
OK

Setup succeeded.

② Get method

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> get name
“dee”

Get success.

 

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

Copy codeThe code is as follows:
127.0.0.1:6379> set name deathmask
OK

127.0.0.1:6379> get name
“deathmask”

③ Setnx method

Set the value corresponding to the key to the value of string type. If the key already exists, return 0. NX means not exist.

[example]

Copy codeThe code is 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 the name already exists and has not been changed. If the app does not exist, it will be set successfully.

④ Setex method

Set the value corresponding to the key to the value of string type, and specify the validity period corresponding to the key value.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> setex mobile 10 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 iPhone and specify a 10 second expiration date.

⑤ Setrange method

Set the substring of the value value value of the specified key

[example] replace the 126 mailbox of Dee with Gmail mailbox

Copy codeThe code is as follows:
127.0.0.1:6379> set email [email protected]
OK

127.0.0.1:6379> get email
[email protected]

127.0.0.1:6379> setrange email 4 gmail.com
(integer) 13

127.0.0.1:6379> get email
[email protected]

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

[example 2]

Copy codeThe code is as follows:
127.0.0.1:6379> set email [email protected]
OK

127.0.0.1:6379> get email
[email protected]

127.0.0.1:6379> setrange email 4 gmail.com
(integer) 16

127.0.0.1:6379> get email
[email protected]

Note: if the replacement string is not as long as the source string, only the characters of the same length in the source string will be replaced, and the remaining characters in the source string will be retained.

⑥ Mset method

If multiple key values are set at one time and OK is returned successfully, all values are set, 0 is returned in case of failure, and no value is set.

[example]

Copy codeThe code is 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

Set the values of multiple keys at one time and return 1 successfully, indicating that all values have been set. Failure returns 0, indicating that no value is set, but the existing key will not be overwritten.

Copy codeThe code is 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

Set the value of the key and return the old value of the key.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> getset key1 emperor
“dee1”

⑨ Getrange method

Gets the substring of the value value value of the key.

Copy codeThe code is as follows:
127.0.0.1:6379> get email
[email protected]

127.0.0.1:6379> getrange email 0 12
[email protected]

Returns characters 0 through 12.
⑩ Mget method

Get the value of multiple keys at one time, and return nil if the corresponding key does not exist.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> mget key1 key2 key3 key4 key5
1) “emperor”
2) “dee2”
3) “dee3”
4) “dee4”
5) (nil)

⑪ Incr method
Add the value of key and return the new value.

Increase by 1 every time

Copy codeThe code is 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]

Copy codeThe code is 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, adding the specified value will set the key when the key does not exist, and the original value will be 0.

[example]

Copy codeThe code is 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]

Copy codeThe code is as follows:
127.0.0.1:6379> incrby times -5
(integer) 0

127.0.0.1:6379> get times
“0”

⑬ Decr method

Subtract the value of key.

⑭ Decrby method

Similar to decr method, subtract the specified value.

[example]

Copy codeThe code is 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)

Append value to the string of the specified key to return the length of the new string.

[example]

Copy codeThe code is 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
[email protected]

⑯ Strlen method

Takes the length of the value value value of the specified key.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> get name
[email protected]

127.0.0.1:6379> strlen name
(integer) 17

(2) Hash type

Redis hash is a mapping table of field and value of string type. Its add and delete operations are 0 (1) (average operation).

Hash is particularly suitable for storing objects. Rather than storing each field of an object as a single string type (string type can store object serialization).

Storing an object in a hash type takes up 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 zipmap (also known as small hash).

In fact, this zipmap is not a hash table, but compared with the normal hash implementation, zipmap can save a lot of metadata storage overhead required by hash itself.

Although the addition, deletion and lookup of zipmap are all o (n), but the number of fields of general objects is not too many, so using zipmap is also very fast, that is to say, the average addition and deletion is O (1).

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

Hash Max zipmap entries 64 ා configuration fields up to 64, hash Max zipmap value 512 ා configuration value up to 512 bytes)

[operation]

① Hset method / hget method

Set the hash field to the specified value. If the key does not exist, create it first.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> hset myhash field1 hello
(integer) 1

Note: myhash is the name of hash table, field1 is a field of hash table, and hello is the corresponding value of this field. It is convenient to store users.

[example 2]

Copy codeThe code is 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 the user whose user table ID is 001 and user name is dee.

You can think of user: 001 as a table.

② Hsetnx method

Set the hash field to the specified value. If the key does not exist, create it first. If it exists, return 0.

[example]

Copy codeThe code is 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

Setting multiple fields of hash at the same time

[example]

Copy codeThe code is 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 all the specified hash fields.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> hmget user:002 name specialty
1) “dee”
2) “php”

⑤ Hincrby method

Specifies the hash field plus the given value.

[example]

Copy codeThe code is 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.

[example]

Copy codeThe code is 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 number of fields for the specified hash.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> hlen user:002
(integer) 3

⑧ HDEL method

Delete the field of the specified hash.

[example]

Copy codeThe code is 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 fields of the hash.

[example]

Copy codeThe code is 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 values of the hash.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> hvals user:002
1) “dee”
2) “redis”

⑪ Hgetall method

Get all fields and values in a hash.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> hgetall user:002
1) “name”
2) “dee”
3) “specialty”
4) “redis”

[summary] to set the hash type data, first set the hash table, then set the hash field, and finally set the value of the hash field.