Memcached Set store command
The Memcached set command is used to store value (data value) in the specified key (key) .
If the set key already exists, the command can update the original data corresponding to the key, that is, the role of the update.
Grammar:
The basic syntax format for the SET command is as follows:
Set[noreply]
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- flags: You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs.
- exptime: The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache
- bytes: The number of bytes stored in the cache
- noreply (optional): This parameter tells the server that it does not need to return data
- value: Stored values (always in the second row) (can be directly understood as value in the key-value structure)
Instance
In the following example we set:
- Key→runoob
- Flag→0
- exptime→900 (in seconds)
- Bytes→9 (number of bytes of data storage)
- Value→memcached
Set wenhy09memcachedstoredget09memcachedEND
Output
If the data is set successfully, the output:
STORED
Output Information Description:
- STORED: Output after saving successfully.
- ERROR: Output after a failed hold.
Memcached Add Command
The Memcached add command is used to store value (data value) in the specified key (key) .
If the add key already exists, the data will not be updated, and the previous value remains the same, and you will get the response not_stored.
Grammar:
The basic syntax format for the add command is as follows:
[noreply]value
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- flags: You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs.
- exptime: The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache
- bytes: The number of bytes stored in the cache
- noreply (optional): This parameter tells the server that it does not need to return data
- value: Stored values (always in the second row) (can be directly understood as value in the key-value structure)
Instance
In the following example we set:
- Key→new_key
- Flag→0
- exptime→900 (in seconds)
- bytes→10 (number of bytes of data storage)
- Value→data_value
0data_valuestoredget0data_valueEND
Output
If the data is added successfully, the output:
STORED
Output Information Description:
- STORED: Output after saving successfully.
- not_stored : Output after a failed hold.
Memcached Replace command
The Memcached Replace command is used to replace the value (data value)of the existing key (key) .
If the key does not exist, the substitution fails, and you get the response not_stored.
Grammar:
The basic syntax format for the Replace command is as follows:
[noreply]value
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- flags: You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs.
- exptime: The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache
- bytes: The number of bytes stored in the cache
- noreply (optional): This parameter tells the server that it does not need to return data
- value: Stored values (always in the second row) (can be directly understood as value in the key-value structure)
Instance
In the following example we set:
- Key→mykey
- Flag→0
- exptime→900 (in seconds)
- bytes→10 (number of bytes of data storage)
- Value→data_value
We use the keyed ' MyKey ' in the following example and store the corresponding value data_value. After execution we replace the value of the same key as ' Some_other_value '.
0data_valuestoredget0data_valueEND0some _other_valueget0some_other_valueEND
Output
If the data is added successfully, the output:
STORED
Output Information Description:
- STORED: Output after saving successfully.
- not_stored: The output after performing the replace failure.
Memcached Append command
The Memcached append command is used to append data to the value (data value) that already exists key (key) .
Grammar:
The basic syntax format for the APPEND command is as follows:
[noreply]value
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- flags: You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs.
- exptime: The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache
- bytes: The number of bytes stored in the cache
- noreply (optional): This parameter tells the server that it does not need to return data
- value: Stored values (always in the second row) (can be directly understood as value in the key-value structure)
Instance
Examples are as follows:
- First we store a key runoob in Memcached with a value of Memcached.
- We then use the GET command to retrieve the value.
- We then use the append command to append "Redis" after the value of the key Runoob.
- Finally, we use the GET command to retrieve the value.
set Wenhy 0 900 9memcachedstoredget Wenhyvalue wenhy 0 9memcachedendappend wenhy 0 Span class= "lit" >900 5redisstoredget< Span class= "PLN" > Wenhyvalue wenhy 0 13
Memcachedredis
END
Output
If the data is added successfully, the output:
STORED
Output Information Description:
- STORED: Output after saving successfully.
- not_stored: The key does not exist on the Memcached.
- client_error: Execution error.
Memcached prepend Command
The Memcached prepend command is used to append data to the value (data value) that already exists key (key) .
Grammar:
The basic syntax format for the prepend command is as follows:
[noreply]value
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- flags: You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs.
- exptime: The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache
- bytes: The number of bytes stored in the cache
- noreply (optional): This parameter tells the server that it does not need to return data
- value: Stored values (always in the second row) (can be directly understood as value in the key-value structure)
Instance
Examples are as follows:
- First we store a key runoob in Memcached with a value of Memcached.
- We then use the GET command to retrieve the value.
- We then use the prepend command to append "Redis" after the value of the key Runoob.
- Finally, we use the GET command to retrieve the value.
Set09memcachedstoredget0memcachedEND05 redisstoredget0redismemcachedEND
Output
If the data is added successfully, the output:
STORED
Output Information Description:
- STORED: Output after saving successfully.
- not_stored: The key does not exist on the Memcached.
- client_error: Execution error.
Memcached Get Lookup command
The Memcached get command gets the value stored in the key ( the data value) and returns null if the key does not exist.
Grammar:
The basic syntax format for the GET command is as follows:
Get Key
Multiple keys are separated by spaces, as follows:
Get key1 key2 Key3
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
Instance
In the following example, we use Runoob as the key, and the expiration time is set to 900 seconds.
Set09memcachedstoredget09memcachedEND
Memcached Delete command
The Memcached Delete command is used to delete an existing key (key).
Grammar:
The basic syntax format for the delete command is as follows:
Delete[noreply]
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- noreply (optional): This parameter tells the server that it does not need to return data
Instance
In the following example, we use Runoob as the key, and the expiration time is set to 900 seconds. We then use the Delete command to delete the key.
Set09memcachedstoredget09memcachedENDDelete wenhydeletedget wenhyENDDelete wenhynot_found
Output
Output Information Description:
- DELETED: Delete succeeded.
- Error: syntax error or deletion failed.
- not_found: Key does not exist.
Memcached incr and DECR command
The Memcached incr and DECR commands are used to self-increment or decrement the numeric value of an existing key (key).
The data that the INCR and the DECR command operates on must be a decimal 32-bit unsigned integer.
If key does not exist return Not_found, if the value of the key is not a number, then client_erroris returned, and other errors return error.
INCR command syntax:
The basic syntax format for the INCR command is as follows:
INCR Key Increment_value
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- Increment_value: The added value.
Instance
In the following example, we use visitors as the key with an initial value of 10, followed by an additional 5 operation.
set visitors 0 900 210< Span class= "PLN" >storedget Visitorsvalue visitors 0 210end< Span class= "PLN" >incr visitors 515get Visitorsvalue visitors 0 2 15end
Output
Output Information Description:
- not_found: Key does not exist.
- client_error: Self-increment is not an object.
- Error Other errors, such as syntax errors.
DECR command
The basic syntax format for the DECR command is as follows:
DECR Key Decrement_value
The parameters are described as follows:
- Key : keys in the key-value structure to find the cached value.
- Decrement_value: The reduced value.
Instance
set visitors 0 900 210< Span class= "PLN" >storedget Visitorsvalue visitors 0 210end< Span class= "PLN" >DECR visitors 55get Visitorsvalue visitors 0 1 5end
In the following example, we use visitors as the key with an initial value of 10, followed by a minus 5 operation.
Output
Output Information Description:
- not_found: Key does not exist.
- client_error: Self-increment is not an object.
- Error Other errors, such as syntax errors.
Memcached flush_all Cleanup command
The Memcached flush_all command is used to clean up all the key=>value (key = = value) pairs in the cache.
This command provides an optional parameter time, which is used to perform a clean-up cache operation after a set period.
Grammar:
The basic syntax format for the Flush_all command is as follows:
[time][noreply]
Instance
To clean up the cache:
Set09memcachedstoredget09memcachedENDFlush_allok Get runoobEND
memcached Command-store command-find command-cleanup command