In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
I. three commonly used instructions for storing data
The three main instructions for storing data are set, add, and replace.
Set instruction: save data called key directly to the server (regardless of whether the data exists or not); add instruction: add a data to memcached, if the key already exists, do not add it, if it does not exist, perform the add Replace instruction: also adds a data to the memcached, but it is the opposite of the add instruction. If the key exists, modify its key value, if it does not exist, return an error and do nothing.
The syntax format of the above three instructions is the same, but the instructions are different. The syntax format of the instructions is as follows:
In the above syntax format, it is explained as follows:
: add, set, replace and other instructions;-key: is a unique identifier stored on the server, which cannot conflict with other key, otherwise the original data will be overwritten (if you use the set command), this key is to accurately access a data item;-flag: marks a 16-bit unsigned integer data, which is used to set some interactive operations between the server and the client. -expiration time: the validity period of the data on the server. If it is 0, the data is always valid (in seconds). The Memcached server will set the validity period of a data to the current Unix time + the set valid time;-bytes: the length of the data, the length of the block data data block. Generally, the next line after this length is followed by the block data data content. After sending the data, the client usually waits for the return from the server.
1. Example of using set instruction
[root@memcache1 ~] # telnet 192.168.171.132 11211 # Connect to memcached database set lisi 003 # insert a data aaa # insert data for aaaSTORED # return "STORED" means successful insertion of get lisi # query inserted data VALUE lisi 0 3 aaaENDset lisi 0 04 # modify "lisi" The value of this key is bbbbbbbbSTOREDget lisi # check again Confirm that the change was successful VALUE lisi 0 4bbbb
2. Example of using add instruction
In the following example, we set:
Key → new_key
Flag → 0
Exptime → 900 (in seconds)
Bytes → 10 (bytes of data storage)
Value → data_value
Add new_key 0 900 10data_valueSTOREDget new_keyVALUE new_key 0 10data_valueEND
Output
If the data is added successfully, the output:
STORED
Output information description:
STORED: output after successful save.
NOT_STORED: output after hold failure.
3. 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 replacement fails and the response NOT_STORED is obtained.
The basic syntax format of the replace command is as follows:
Replace key flags exptime bytes [noreply] value
The parameters are described as follows:
Key: key in the key value key-value structure, which is used to find cached values.
Flags: can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs.
Exptime: length of time to keep key-value pairs in the cache (in seconds, 0 means forever)
Bytes: the number of bytes stored in the cache
Noreply (optional): this parameter tells the server that there is no need to return data
Value: the stored value (always on the second line) (can be directly understood as value in the key-value structure)
Use an example
Will be set in the following example:
Key → mykey
Flag → 0
Exptime → 900 (in seconds)
Bytes → 10 (bytes of data storage)
Value → data_value
In the following example, we use the key 'mykey'' and store the corresponding value data_value. After execution, we replace the value of the same key with 'some_other_value'.
Add mykey 0 900 10data_valueSTOREDget mykeyVALUE mykey 0 10data_valueENDreplace mykey 0 900 16some_other_valueget mykeyVALUE mykey 0 16some_other_valueEND
If the data is added successfully, the output:
STORED
Output information description:
STORED: output after successful save.
NOT_STORED: output after failure to perform replacement. ~
4. Append command
The append command is used to append data to the value (data value) that already exists with key (key).
Syntax:
The basic syntax format of the append command is as follows:
Append key flags exptime bytes [noreply] value
The parameters are described as follows:
Key: key in the key value key-value structure, which is used to find cached values.
Flags: can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs.
Exptime: length of time to keep key-value pairs in the cache (in seconds, 0 means forever)
Bytes: the number of bytes stored in the cache
Noreply (optional): this parameter tells the server that there is no need to return data
Value: the stored value (always on the second line) (can be directly understood as value in the key-value structure)
.
Examples are as follows:
First, we store a key runoob in Memcached, whose value is memcached.
Then we use the get command to retrieve the value.
Then, we use the append command to append "redis" to the value whose key is runoob.
Finally, we use the get command to retrieve the value.
Set runoob 0 900 9memcachedSTOREDget runoobVALUE runoob 0 9memcachedENDappend runoob 0 900 5redisSTOREDget runoobVALUE runoob 0 14memcachedredisEND
Output
If the data is added successfully, the output:
STORED
Output information description:
STORED: output after successful save.
NOT_STORED: this key does not exist on Memcached.
CLIENT_ERROR: execution error.
5. Prepend command
The prepend command is used to append data to the front of a value (data value) that already exists with key (key), just the opposite of append.
Syntax:
The basic syntax format of the prepend command is as follows:
Prepend key flags exptime bytes [noreply] value
The parameters are described as follows:
Key: key in the key value key-value structure, which is used to find cached values.
Flags: can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs.
Exptime: length of time to keep key-value pairs in the cache (in seconds, 0 means forever)
Bytes: the number of bytes stored in the cache
Noreply (optional): this parameter tells the server that there is no need to return data
Value: the stored value (always on the second line) (can be directly understood as value in the key-value structure)
Examples are as follows:
First, we store a key runoob in Memcached, whose value is memcached.
Then we use the get command to retrieve the value.
Then, we use the prepend command to append "redis" to the value whose key is runoob.
Finally, we use the get command to retrieve the value.
Set runoob 0 900 9
> memcached > STORED > get runoob > VALUE runoob 0 14 > memcached > END > prepend runoob 0900 5 > redis > STORED > get runoob > VALUE runoob 0 14 > redismemcachedEND
If the data is added successfully, the output:
STORED
Output information description:
STORED: output after successful save.
NOT_STORED: this key does not exist on Memcached.
CLIENT_ERROR: execution error.
6. Cas command
The CAS (Check-And-Set or Compare-And-Swap) command is used to perform a "check and set" operation. It can be written only if the value corresponding to the key has not been modified by other clients after the current client last took the value. The check is done through the cas_token parameter, which is the only 64-bit value that Memcach assigns to an element that already exists.
The basic syntax format of the CAS command is as follows:
Cas key flags exptime bytes unique_cas_token [noreply] value
The parameters are described as follows:
Key: key in the key value key-value structure, which is used to find cached values.
Flags: can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs.
Exptime: length of time to keep key-value pairs in the cache (in seconds, 0 means forever)
Bytes: the number of bytes stored in the cache
A unique 64-bit value obtained by unique_cas_token through the gets command.
Noreply (optional): this parameter tells the server that there is no need to return data
Value: the stored value (always on the second line) (can be directly understood as value in the key-value structure)
To use the CAS command on Memcached, you need to obtain the token (token) through the gets command from the Memcached service provider.
The function of the gets command is similar to the basic get command. The difference between the two commands is that gets returns a little more information: the 64-bit integer value is very much like the "version" identifier of the name / value pair.
The steps of the example are as follows:
If no unique token is set, the CAS command executes incorrectly.
If the key key does not exist, execution fails.
Add key-value pairs.
Obtain a unique token through the gets command.
Update data using the cas command
Use the get command to see if the data is updated
Cas tp 0 900 9
ERROR value)) Yes.
This command provides an optional parameter, time, to perform a cache cleanup operation after the specified time.
The basic syntax format of the flush_all command is as follows:
Flush_all [time] [noreply]
Example
Clean up the cache:
Set runoob 0 900 9memcachedSTOREDget runoobVALUE runoob 0 9memcachedENDflush_allOKget runoobEND
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.