Memcached commonly used commands and instructions

Source: Internet
Author: User
Tags cas config memcached memory usage

One, storage command

Store the format of the command:

?
1 2 <command name> <key> <flags> <exptime> <bytes> <data block>

The parameter description is as follows:

<command name> Set/add/replace
<key> Find keywords
<flags> The client uses it to store additional information about key-value pairs
<exptime> The data's survival time, 0 means forever
<bytes> Number of bytes stored
<data block> Stored blocks of data (can be directly understood as value in the key-value structure)

1, add

(1), the set that is stored anyway

This set command is used very frequently in memcached. The set command can be simply added, and if the set key already exists, the command can update the original data corresponding to the key, which is the effect of the update.

You can view the records added by using the "Get Key Name" method:

As you know, we can also remove the Delete command and add it again.

(2), add if data is not present

(3), replace only if the data is present

2, delete

You can see that deleting a key value that already exists and a record that does not exist can return different results.

Second, read the command

1, get

The key of the GET command can represent one or more keys, separated by a space between the keys

2, gets

As you can see, the gets command returns a number more than the normal get command (13 in the figure above). This number checks to see if the data has changed. When the key corresponds to the data change, the more returned number will also change.

3. CAs

CAs, meaning checked and set, can be stored only if the last parameter matches the parameters obtained by gets, or "EXISTS" is returned.

Third, Status command

1, stats

2, stats items


Execute stats items, you can see stat items line, if memcached store a lot of content, then here will also list a lot of stat items line.

3, stats cachedump slab_id limit_num

We perform the stats cachedump 1 0 command effect as follows:

Here the slab_id is 1, which is determined by the result returned by stats items in 2 (STAT the number behind the items); Limit_num looks like a number of records to return, a good guess, but 0 means all records are displayed, and N (n>0) Indicates that N records are displayed, and if n exceeds all records under the slab, the result is consistent with the result returned by 0.


You can traverse Limit_num records by stats items, stats cachedump slab_id memcached with Get commands.

4. Other Stats orders

such as stats slabs,stats sizes,stats reset and so on use is also more common.

Iv. Other common orders

1, append

Add cached data after existing cached data, such as the existing cached key does not exist The server response is not_stored.

2, Prepend

is very similar to append, but its role is to add cached data before existing cached data.

3, Flush_all

The command has an optional numeric parameter. It always executes successfully and the server sends a "OKRN" response. The effect is to invalidate an existing project immediately (the default), or after a specified time. After that, the fetch command is executed, and nothing is returned (unless the same key name is stored again). Flush_all actually does not release the memory occupied by the project immediately, but is executed after a new project is stored (this is determined by the memcached lazy detection and deletion mechanism).

The Flush_all effect is that it causes all items that have been updated earlier than the time set by Flush_all to be ignored when the command is executed.

4. Other Orders

Memcached also has a lot of commands, such as for the storage of digital type can be through the INCR/DECR command to add or subtract operations, and so on, here only the development and operation of the regular use of the command, the other no longer one by one examples.

Add a simple understanding of several caches in the. NET Framework

The importance of caching in Web sites is unquestionable. I think a lot of asp.net developers in the development of Web applications when the priority is to use the cache is not a third-party caching solution (such as distributed cache memcached, Redis, etc.), but should be the. NET Framework has provided a variety of caching solutions. The following is a discussion of the understanding of caching in the. NET Framework, based on your own development experience.

1, System.Web.Caching.Cache

It is estimated that most people who have done asp.net development have used the cache under this namespace, and we can use the HttpContext.Current.Cache instance directly instead of instantiating it. Of course, the cache class under this namespace allows you to instantiate, and you need to customize your own caching system to completely control how you initialize the class. I've seen it in the garden. There are many articles about Cache the Crud helper class libraries are mostly for System.Web.Caching.Cache.

It should be explained that we can also implement the caching under different representations of Web, console, WinForm and so on through the Httpruntime.cache in this namespace, and do not need to instantiate it ourselves at all. Httpruntime.cache is one of the more classes used in personal development, and now prefers the enhanced cache class MemoryCache in. NET framework4.0.

2. Output Cache

As we all know, output caching is mainly divided into page output caching and page partial caching. Plainly, that is, caching the entire page of HTML or part of the HTML, there is nothing worth discussing, but recently saw Dudu this blog just suddenly found that the use of it is really great attention, I have not found this problem before? It seems that the ability to find and solve problems is just as important, sometimes even more important.

3, System.Runtime.Caching

The most commonly used class memorycache in personal development is now derived from this namespace, and a using System.Runtime.Caching is required before use. MemoryCache inherits from ObjectCache, IEnumerable, IDisposable, where ObjectCache is an abstract class. People who have used MemoryCache know that this memorycache has a property called Default, which can often be used as follows:

Private Static ObjectCache MemCache = Memorycache.default;

Of course, we can also initialize the cached object through the public memorycache (string name, NameValueCollection config = null) constructor.

We can then configure the Memory usage quota scheme and the quota check cycle for each MemoryCache instance to run in the Web.config file, and the following examples refer to MSDN:

  < system.runtime.caching >
    < MemoryCache >
      < namedcaches >
        < Add name="Default"cachememorylimitmegabytes="Ten"   PollingInterval="00:02:00"/>
      </namedcaches  >
    </memorycache>
   </  System.runtime.caching>

The implication of these configurations is that you can explicitly specify the memory usage quota scheme and the quota check cycle that each MemoryCache instance runs. For example, we can configure to change the memory quota of the Memorycache.default instance on demand (not knowing the maximum memory available for the cache, or perhaps the legendary 800M or so). The cache expiration policy is much the same as other caching frameworks, unlike System.Web.Caching.Cache, which is called ChangeMonitor and provides a caching-dependent policy based on files and directories, rather than the name CacheDependency. There is also a need to explore the cache expiration strategy, although personal development is more biased towards data caching and substitution, and there is currently no contact and use of a more perfect expiration strategy solution.

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.