Six features of Redis
L Strings
L HASHS
L Lists
L Sets
L Sorted Sets
L PUB/SUB Application Scenarios for Redis features Strings
The Strings data structure is a simple key-value type, and value is not only a string, it can also be a number. Common Methods
Method |
Description |
Characteristics |
Set |
Sets the value of the key corresponding to string type |
|
Get |
Gets the value of the string corresponding to the key, if no return nil exists |
|
Setnx |
Set can be a value of type string, if key exists return 0 does not overwrite, there is no return 1 |
NX means not exist Set the value of a key, only if the key does not exist |
Setex |
The value of the set key is a string of type value and specifies the validity period for this key value Setex Key seconds value |
Example: Setex mykey 10 Hello |
SetRange |
Sets the substring of the value of key |
SetRange Key Location Replacement content If the replacement content does not have the original value length, the remaining contents of the original value are preserved |
Mset |
Set the value of multiple keys at once, successfully return OK, fail to return 0, success is successful, or all failed successfully. |
Mset key1 Content One key2 content two |
Msetnx |
Setting the value of multiple keys at once, successfully returning OK, failing to return 0, not overwriting values that already exist, succeeding successfully, failing all. |
|
Getset |
Sets the value of key and returns the old value of key |
Getset Key Newvaluse |
GetRange |
Gets the value substring corresponding to key |
GetRange key 0 5//Get first 6 characters |
Mget |
Bulk acquisition |
Mget key1 Key2 Key3//No setting returns empty |
incr |
Increase the value of the key and return the new value |
+1 |
Incrby |
The value specified for the possible value plus, |
Key if not present will set key and value is 0 Incrby Key1 5//value of Key1 plus 5 |
Decr |
Reduce the value of a key by reducing the operation |
-1 |
Decrby |
Subtracts the specified value from the value of key |
|
Append |
Appends value to the string of the specified key, returning the new string length |
|
Strlen |
Takes the length of the value of the specified key |
|
Usage Scenarios
General Key-value Cache app. Regular count: Number of Weibo, number of fans HASHS
In memcached, we often package structured information into HashMap, which is stored as a string value after the client is serialized, such as the user's nickname, age, gender, integral, and so on, when it is necessary to modify one of these items, it is usually necessary to remove all values after deserialization, Modify the value of an item, and then serialize the store back. This not only increases the overhead, but also does not apply to some scenarios where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The hash structure of Redis allows you to modify only one item property value just as you would update a property in a database .
It is a string type of field and value mapping table, its addition and deletion are average, hash is particularly suitable for storing objects, for storing objects into a string, hash will occupy less memory, and can more convenient access to the entire object. It is completely similar to the Java HashMap method
Method |
Description |
Characteristics |
Hset |
Set a hash field to the specified value, and if key does not exist, create it first. |
Hset tab KE1 VAL1 |
Hget |
Get a field value for a hash |
Hget Tab KE1 |
Hsetnx |
Like a string is just a hash of the operation |
|
Hmset |
Bulk Set Hash content |
|
Hmget |
Get all key values of the hash table |
Hmget Key Field1 Field2 |
Hincrby |
Add value to a field in a hash table |
|
Hexists |
Determine if a key exists in the hash table |
|
Hlen |
Returns the number of keys in the hash table |
|
Hdel |
Deletes a key value pair for the specified hash table |
|
Hkeys |
Returns all keys in the hash table |
|
Hvals |