Several memcached commands that are easy to ignore but very useful

Source: Internet
Author: User
I recently read the source code of memcached. In this process, I found many memcached commands that are not frequently used at ordinary times, however, memcached often needs to be used to operate and maintain memcached. However, the memcached version, memcachedclient, or other reasons are limited.

I recently read the source code of memcached. In this process, I found many memcached commands that are not frequently used at ordinary times, however, memcached often needs to be used to operate and maintain memcached. However, the memcached version, memcached client, or other reasons are limited.

I recently read the source code of memcached. In this process, I found many memcached commands that are not frequently used at ordinary times, however, memcached often needs to be used to operate and maintain memcached, but it was limited by the memcached version, memcached client, or other reasons, I don't know how to use memcached. I 'd like to share it with you.

1. CAS and GETS

Memcached added the CAS (Check and Set) protocol from version 1.2.4 to handle data consistency issues when the same ITEM (key-value) is updated and modified by multiple sessions.

Assume that there are two sessions (A and B). You need to modify the value of x for A key at the same time, and the modified data is calculated based on the original data. Session A and B get the key value x at the same time. session A should be updated to y after calculation, and session B is also updated to y after calculation, however, session B expects to get the y value, calculate it as Z, and update it. This problem is caused by the lack of a MySQL-like transaction for the consistency of concurrent data modification.

CAS commands focus on solving certain concurrent modification problems and introduce the concept of optimistic locks. Before trying to modify the value of a KEY, run the GETS command to obtain the value of a KEY and its version number. When data is updated, use CAS to update data with caution, compare whether the version number is the same as the local version number. Otherwise, discard the modification.

After the CAS protocol is enabled by default, each key is associated with a 64-bit long unique value, indicating the version number of the value corresponding to the key. This value is generated by the Memcached server and starts from 1, and the same Memcached server does not repeat. In two cases, the value of this version is 1:

1. Add a key-value pair;

2. Update the value corresponding to an existing key. The version value of the deleted item does not decrease.

First, we introduced the GETS command to GET the version number of the KEY value. We can find that the GETS command returns a number more than the GET command. This number is the version number of the KEY value we mentioned above.

Then the CAS command is similar to the SET command, but a parameter is added at the end, that is, the key version number. Only when the version number is consistent with the version number of the stored data, the update operation takes effect.

Set a 0 0 1 xSTOREDgets aVALUE a 0 1 1 // The last bit is version a xENDset a 0 0 1 ySTOREDgets aVALUE a 0 1 2 // after adding or modifying, ycas a 0 0 1 1 // when cas is updated, unlike the set operation, the last digit must specify the version number. When the local version number is the same as the server version number, update will be valid xEXISTScas a 0 0 1 2 ySTORED

Ii. stats items and stats cachedump

Have you ever wondered what data is stored in memcached? Have you ever been looking for a way to traverse all memcached keys like redis?

It is actually the stats method that we usually use. The stats method not only provides an overview of memcached, but also provides more detailed information if you add sub-commands. Such as slabs and items.

The stats items command can obtain information about the item group in memcached, such as the number of items in the group and the number of times they are kicked off. The returned information (item group number) of this command will be used when you run the cachedump command later ).

The stats cachedump command can dump all items in a slab. The first parameter is the items group number returned by stats items, that is, the slab number, the second parameter is how many items are displayed at a time. If the value is 0, all items of the item group are displayed, and the second column is the key.

stats itemsSTAT items:1:number 3STAT items:1:age 943STAT items:1:evicted 0STAT items:1:evicted_nonzero 0STAT items:1:evicted_time 0STAT items:1:outofmemory 0STAT items:1:tailrepairs 0STAT items:1:reclaimed 0STAT items:1:expired_unfetched 0STAT items:1:evicted_unfetched 0ENDstats cachedump 1 0ITEM c [5 b; 1405246917 s]ITEM b [1 b; 1405246917 s]ITEM a [1 b; 1405246917 s]END
Although these two commands cannot display all keys at a time, if we perform an iteration Based on the return value of stats items, or just to manually sample several items, this will help us understand the data in memcached.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.