127.0.0.1: 62.16setnameinterok127. 0.0.1: 62.16getnameinter127. 0.0.1: 62.16setnamemillan127. 0.0.1: 6379 getnamemillan # In the first case, setnx determines that the name has a value, so the value after the parameter is set is returned (integer) 0. If the value is set to 127.0.0.1: 6379setnameinterOK127. 0.
127.0.0.1: 6379 set name inter OK 127.0.0.1: 6379 get name inter 127.0.0.1: 6379 set name millan 127.0.0.1: 6379 get name millan # In the first case, setnx determines the existence value of name, therefore, the following set Value Returns (integer) 0, and the set value fails 127.0.0.1: 6379 set name inter OK 127.0.
- 127.0.0.1: 6379> set name inter
- OK
|
- 127.0.0.1: 6379> get name
- "Inter"
|
- 127.0.0.1: 6379> set name millan
- 127.0.0.1: 6379> get name
- "Millan"
|
- # In the first case, setnx determines that the name has a value. Therefore, the value after the value is set to return (integer) 0. The value is not set successfully.
- 127.0.0.1: 6379> set name inter
- OK
- 127.0.0.1: 6379> setnx name millan
- (Integer) 0
- # Set a value for a variable that has not been set, return (integer) 1, verify by using the get method, the value is successfully set
- 127.0.0.1: 6379> setnx new_name inter
- (Integer) 1
- 127.0.0.1: 6379> get new_name
- "Inter"
|
- # Set the value of wind to hot, and set the time to 10 seconds. The value is valid within 10 seconds. After 10 seconds, the key-value pair is invalid.
- 127.0.0.1: 6379> setex wind 10 hot
- OK
- 127.0.0.1: 6379> get wind
- "Hot"
- 127.0.0.1: 6379> get wind
- (Nil)
|
- 127.0.0.1: 6379> set name djy@163.com
- OK
- 127.0.0.1: 6379> setrange name 4 gmail.com
- (Integer) 13
- 127.0.0.1: 6379> get name
- Djy@gmail.com"
|
- 127.0.0.1: 6379> mset key1 djy1 key2 djy2 key3 djy3
- OK
- 127.0.0.1: 6379> get key2
- "Djy2"
- 127.0.0.1: 6379> get key3
- "Djy3"
- 127.0.0.1: 6379> get key1
- "Djy1"
|
- 127.0.0.1: 6379> msetnx key1 djyisasmartguy key5 yes
- (Integer) 0
- 127.0.0.1: 6379> get key5
- (Nil)
- 127.0.0.1: 6379> get key1
- "Djy1"
|
- 127.0.0.1: 6379> getset key1 djyisasmartguy
- "Djy1"
- 127.0.0.1: 6379> get key1
- "Djyisasmartguy"
|
- 127.0.0.1: 6379> getrange key1 8 10
- "Art"
- Wagner. 0.0.1: 6379>
|
- 127.0.0.1: 6379> mget key1 key2
- 1) "djyisasmartguy"
- 2) "djy2"
|
- 127.0.0.1: 6379> set key5 10
- OK
- 127.0.0.1: 6379> incr key5
- (Integer) 11
|
- # Incrby key5 10 refers to an increase of 10 times. If it is-10, it is reduced by 10 times.
- 127.0.0.1: 6379> incrby key5 10
- (Integer) 21
- 127.0.0.1: 6379> get key5
- "21"
|
- 127.0.0.1: 6379> decr key5
- (Integer) 20
- 127.0.0.1: 6379> decrby key5 6
- (Integer) 14
|
- 127.0.0.1: 6379> get key1
- "Djyisasmartguy"
- Wagner. 0.0.1: 6379> append key1 true
- (Integer) 18
- 127.0.0.1: 6379> get key1
- "Djyisasmartguytrue"
|
- 127.0.0.1: 6379> strlen key1
- (Integer) 18
|
Redis database learning and practice-redis data type Redis data type: 1: Strings type and operation: String type is the simplest type, a Key corresponds to a Value, the String type is binary secure. Redis strings can contain any data, such as JPG images or serialized objects.
(1) Set the value of the key to a value of the String type.
For example, set a key-value pair with name = inter:
Valid value:
Overwrite value:
The value set later will overwrite the previously set value.
If you do not want to overwrite the previous value when setting the value, you can use setnx.
(2) During the Setnx operation, the system first checks whether there is a value in the name. The setting is successful only when the name does not contain the value. (Nx stands for not exist)
For example:
(3) setex
Set the value of the key to a value of the string type and specify the validity period of the key-value pair.
In other setting methods, if the validity period is not set, it indicates that it is permanently valid.
For example:
(4) setrange
Specifies the sub-string of the value of the specified key.
(5) mset
If multiple key values are set at a time, "OK" is returned, indicating that all values are set, and "0" is returned for failure, indicating that no value is set.
(6) msetnx
If multiple key values are set at a time, a successful return of OK indicates that all values are set. If a failure is returned, 0 indicates that no value is set, but the existing key is not overwritten.
As long as one key is not set successfully, none of the other keys set together will be set successfully.
(7) get
Obtain the string value corresponding to the key. If the key does not exist, nil is returned.
Getset
Set the new value of the key and return the old value of the key.
(8) getrange
The substring used to obtain the value of the key.
(9) mget
Obtain the values of multiple keys at a time. If the corresponding key does not exist, nil is returned.
(10) incr
Auto-increment overflow: adds the key value and returns a new value.
(11) incrby
Similar to incr, if a specified value is added, the key is set when the key does not exist and the original value is regarded as 0.
(12) decr
Subtract the key value.
(13) decrby
Similar to decr, reduce the specified value.
(14) append
Append value to the string of the specified key to return the length of the new string value.
(15) strlen
The length of the value of the specified key. 2: Hashes type and operation: Redis hash is a string-type field and value ing table. Its Addition and deletion operations are all 0 (1) (average ). Hash is particularly suitable for Object Storage. Compared to saving each field of an object to a single string type. Storing an object in the hash type consumes less memory and makes it easier to access the entire object.
Hset
Set hash field to the specified value. If the key does not exist, create it first.
127.0.0.1: 6379> hset myhash field1 hello (Integer) 1 127.0.0.1: 6379> hget myhash field1 "Hello" |
Hsetnx
Set hash field to the specified value. If the key does not exist, create it first. If yes, 0 is returned.
127.0.0.1: 6379> hsetnx myhash field1 helloyou (Integer) 0 |
Hmset
Multiple hash fields are set at the same time.
127.0.0.1: 6379> hmset myhash field2 helloyou field3 hellome OK 127.0.0.1: 6379> hmet myhash field2 field3 1) "helloyou" 2) "hellome" |
Hincrby
Specify a value for the specified hashfield.
127.0.0.1: 6379> hset myhash field4 30 (Integer) 1 127.0.0.1: 6379> hincrby myhash field4 10 (Integer) 40 127.0.0.1: 6379> hget myhash field4 "40" Wagner. 0.0.1: 6379> |
Hexists
Test whether the specified field exists.
127.0.0.1: 6379> hexists myhash field1 (Integer) 1 |
Hlen
Returns the number of fields in the specified hash.
127.0.0.1: 6379> hlen myhash (Integer) 4 |
Hdel
Deletes the specified hash field.
127.0.0.1: 6379> hdel myhash field4 (Integer) 1 Wagner. 0.0.1: 6379> |
Hkeys
Returns all hash fields.
127.0.0.1: 6379> hkeys myhash 1) "field1" 2) "field2" 3) "field3" |
Hvals
Returns all values of hash.
127.0.0.1: 6379> hvals myhash 1) "hello" 2) "helloyou" 3) "hellome" |
(10) hgetall
Obtains all fields and values in a hash.
127.0.0.1: 6379> hgetall myhash 1) "field1" 2) "hello" 3) "field2" 4) "helloyou" 5) "field3" 6) "hellome" |
3: List type and operation:
List is a linked List structure. The main function is to push, pop, get all values in a range, etc. In the operation, the Key is interpreted as the name of the linked List.
The list type of Redis is actually a two-way linked list of the string type for each sub-element. We can use the puth and pop operations to add and delete elements from the head or tail of the linked list, so that the list can be both a stack and a queue.
Here we will introduce the Data Types of apsaradb for Redis. There are two other data types. The Set and Zset types are not described in detail.