String is the simplest type. A key corresponds to a value. The string type is binary secure. Redis string can contain any data, such as JPG images or serialized objects.
Operation
Set
Set the value corresponding to the key to a value of the string type
For example:
Set Name haoxing // set key to haoxiang
Get name // get "haoxiang"
Note:
Adding the same key name to the set will overwrite the original value.
Setnx
Set the value corresponding to the key to a value of the string type. If the key does not exist, 0 is returned.
For example:
Setnx name lamp // 0 is returned. If this key does not exist, 1 is returned.
Setex
Set the value of the key to a value of the string type and specify the validity period of the key-value pair.
For example:
Setex color 10 red
Get color // returns "red" within 10 seconds"
Get color // If no result is returned after 10 seconds (NiL) indicates null
Setrange
Replaces the part of the string corresponding to a key.
For example:
Set email [email protected]
Get email // "[email protected]"
Setrange email 9 163.com
Get email // "[email protected]"
Note:
Replace is to replace the length of the new string you write from 9th. If it is smaller than the length of the original string, replace only the corresponding number of bytes and keep the subsequent content.
Mset
If multiple key values are set at a time, "OK" is returned, indicating that all values are set. Otherwise, "0" is returned, indicating that no value is set.
For example:
Mset key1 haoxiang1 key2 haoxiang2
Note:
The same key as set will be overwritten.
Msetnx
If multiple key values are set at a time, "OK" is returned successfully, indicating that all values are set. If a failure is returned, "0" indicates that no value is set, but the existing key is not overwritten, all values are reloaded before they are successfully processed.
Get
Returns the value of the value corresponding to the key. If the key does not exist, Nil is returned.
For example:
Get name
GetSet
Set the key value and return the old value of the key value.
For example:
Set num 20
Get num // "20"
GetSet num 30 // "20"
Get num // "30"
Getrange
Obtain the part of the value string corresponding to the key
For example:
Set eamil [email protected]
Get email 0 7 // "haoxiang"
Mget
Batch query. If the key does not exist, Nil is returned.
Incr
Add the value corresponding to the key and return a new value.
For example:
Set num 30
Incr num // (integer) 31
Incrby
Similar to incr, but the specified value is added. If the key does not exist, the key is set and the original value is regarded as 0. If the value is positive, the negative value is reduced.
For example:
Incrby num 5
Incrby num-5
Decr
Perform the subtraction operation on the value.
Decrby
Subtract the specified value. The value is subtraction, and the negative value is addition.
Append
Append a string to the value and return the New String Length.
Strlen
Obtains the string length of the value corresponding to the key.
Hash 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. Each field of the object is saved as 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 filed to the specified value. If the key does not exist, create
For example
Hset myhash filed1 Hello // Where myhash is the name of the hash table, filed1 is the field, and hello is the value corresponding to the field
Hsetnx
Set hash field to the specified value. If the key does not exist, create it first. If yes, 0 is returned, which is similar to setnx.
Hmset
Batch set the values of multiple hash Fields
For example:
Hmset myhash field1 Hello field2 world
Hget
Obtains the value of a field in hash.
For example:
Hget myhash field1
Hmet
Obtain the values of multiple fields at the same time
For example:
Hmet myhash field1 field2 field3
Hincrby
Add the specified value to the specified hash Field
For example:
Hincrby myhash age 8
Hexists
Test whether a field in the hash table exists. If yes, 1 is returned. If no field exists, 0 is returned.
For example:
Hexists myhash age
Hlen
Returns the number of fields in the hash table.
For example:
Hlen myhash
Hkeys
Returns all fields in the hash.
For example:
Hkeys myhash
Hvals
Returns all values in the hash.
For example:
Hvals myhash
Hgetall
Obtain all the fields and corresponding values in the hash.
For example:
Hgetall myhash
Hdel
Deletes a specified field in the hash.
For example:
Hdel myhash age