Memcached common commands and instructions for use _ PHP Tutorial

Source: Internet
Author: User
Memcached common commands and instructions for use. Storage Command format: commandnamekeyflagsexptimebytesdatablock parameter description: commandnamesetaddreplacekey search keyword flags the client uses it to store the format of the key storage command:


Parameters are described as follows:
Set/add/replace
Search for keywords
The client uses it to store additional information about key-value pairs.
Survival time of the data. 0 indicates permanent
Storage bytes
Stored data blocks (which can be directly understood as values in the key-value structure)
1. add
(1) set

The set command is frequently used in memcached. The set command can be simply added. if the set key already exists, the command can update the original data corresponding to the key, that is, to update the data.

You can use the "get key name" method to view the added records:

As you know, we can also delete it by using the delete command and then add it again.

(2) add only when the data does not exist

(3) replace

2. delete

As you can see, deleting an existing key value and a record that does not exist can return different results.

II. read command 1. get

The key of the get command can be one or more keys separated by spaces.


2. gets

As you can see, The gets Command returns a number (13) more than the common get command ). This number can be used to check whether the data has changed. When the data corresponding to the key changes, the multiple returned numbers will also change.

3. cas
Cas indicates checked and set. it can be stored only when the last parameter matches the parameter obtained by gets. otherwise, "EXISTS" is returned ".

III. status Command 1. stats

2. stats items
Execute stats items to view the STAT items line. if memcached stores a lot of content, many STAT items lines will be listed here.

3. stats cachedump slab_id limit_num

Execute the stats cachedump 1 0 command. The effect is as follows: Here, slab_id is 1, which is determined by the result returned by stats items in 2 (number after STAT items; limit_num seems to be the number of records returned. it is a good guess, but 0 indicates that all records are displayed, and n (n> 0) indicates that n records are displayed, if n exceeds all records under the slab, the result is the same as that returned by 0.
You can use the stats items, stats cachedump slab_id limit_num, and get commands to traverse memcached records.

4. other stats commands
Such as stats slabs, stats sizes, and stats reset.

4. other common commands
1. append
In the existing cache dataAfterAdd cache data. if the existing cache key does not exist, the server returns NOT_STORED.

2. prepend
It is very similar to append, but its function is to cache data in the existingBeforeAdd cache data.
3. flush_allThis command has an optional numeric parameter. It always runs successfully, and the server sends an "OK \ r \ n" response. The effect is to immediately invalidate an existing project (default) or after the specified time. After the retrieval command is executed, no content will be returned (unless the same key name is re-stored ). Flush_all does not immediately release the memory occupied by the project, but is executed when new projects are stored in succession (this is determined by the memcached's laziness detection and deletion mechanism ).

The result of flush_all is that all items whose update time is earlier than the time set by flush_all are ignored when the retrieval command is executed.

4. other commands
Memcached also has many commands. for example, you can use the incr/decr command to increase or decrease the storage type. here, only the frequently used commands in development and O & M are listed.

Supplement 1: Several caches in. net framework
There is no doubt about the importance of caching in websites. I think the cache that many asp.net developers prioritize when developing web application systems is not a third-party cache solution (such as distributed cache memcached and redis.. net framework provides multiple cache solutions. The following describes the knowledge of caching in. net framework based on your development experience.

1. System. Web. Caching. Cache
It is estimated that most people who have developed asp.net have used the Cache in this namespace. we can directly use the HttpContext. Current. Cache instance without instantiating it. Of course, the Cache class in this namespace allows you to instantiate it. if you need to customize your own Cache system, you can fully control how to initialize this class. I have seen many articles in the garden about the CRUD auxiliary class library of Cache. most of them are for System. Web. Caching. Cache.

You can also use HttpRuntime. Cache in the namespace to implement Cache in different forms, such as web, console, and winform, without the need to instantiate the Cache by yourself. HttpRuntime. Cache is a class that I used in my personal development. now I prefer the enhanced Cache class MemoryCache in. net framework4.0.

2. Output Cache
As we all know, the output cache mainly refers to the paging output cache and partial page cache. To put it bluntly, it is to cache the html or part of the html of the entire page. there is nothing worth discussing. However, just recently I saw dudu's blog, and I suddenly found that it was really exquisite to use it, how did I not find this problem before? It seems that the ability to discover and solve problems is equally important. sometimes, the former is even more important.

3. System. Runtime. Caching
The most frequently used class MemoryCache in personal development comes from this namespace. before using it, you must reference using System. Runtime. Caching. MemoryCache is inherited from ObjectCache, IEnumerable, and IDisposable. ObjectCache is an abstract class. Anyone who has used MemoryCache knows that this MemoryCache has a property called Default, which can be used as follows:

Private static ObjectCache memCache = MemoryCache. Default; of course, we can also use the public MemoryCache (string name, NameValueCollection config = null) constructor to initialize the cache object.

Next, we can configure the memory usage quota scheme and quota check cycle for each MemoryCache instance in the web. config file. the following example referencesMSDN:

The code is as follows:










These configurations clearly specify the memory usage quota scheme and quota check cycle for each MemoryCache instance. For example, we can change the memory quota of the MemoryCache. Default instance on demand through configuration (I don't know the maximum memory available for the cache, maybe it's about MB ). The Cache expiration policy is similar to other Cache frameworks. Unlike System. Web. Caching. Cache, the Cache name is not CacheDependency, ChangeMonitorAnd provides cache dependency policies based on files and directories. It is also necessary to discuss cache expiration policies. However, personal development focuses more on data caching and replacement, and there is no perfect solution to cache expiration policies.

Command name key flags exptime bytes data block parameters are described as follows: command name set/add/replace key search keyword flags the client uses it to store the key...

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.