Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common commands and instructions for Memcached

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "Memcached commands and instructions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Format for storing commands:

The parameters are described as follows:

The set/add/replace lookup keyword client uses it to store additional information about key-value pairs the survival time of the data, and 0 represents a block of data stored in bytes forever (which can be directly understood as value in the key-value structure)

1. Add

(1) set stored anyway

This set command is used very frequently in memcached. Not only can the set command be simply added, but if the key of the set already exists, the command can update the original data corresponding to the key, that is, to achieve the purpose of updating.

You can view the added records by "get key name":

As you know, we can also delete it and re-add it with the delete command.

(2) add that is added only when the data does not exist

(3) replace that can be replaced when only data exists

2. Delete

As you can see, deleting existing keys and non-existent records can return different results.

2. Read command 1, get

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

2 、 gets

As you can see, the gets command returns one more number than the normal get command (13 in the figure above). This number can be checked to see if the data has changed. When the data corresponding to key changes, the multi-returned number also changes.

3 、 cas

Cas means checked and set and can be stored only if the last parameter matches the parameter obtained by gets, otherwise "EXISTS" is returned.

Status command 1, stats

2 、 stats items

When you execute stats items, you can see the STAT items lines, and if the memcached stores a lot of content, then there are many STAT items lines listed here.

3 、 stats cachedump slab_id limit_num

The effect of executing the stats cachedump 10 command is as follows: here slab_id is 1, which is determined by the result returned by stats items in 2 (the number after STAT items); limit_num looks like how many records are returned, which 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 consistent with the result returned by 0.

The records of memcached can be traversed through stats items, stats cachedump slab_id limit_num and get command.

4. Other stats commands

It is also common to use stats slabs,stats sizes,stats reset and so on.

4. Other common commands

1. Append adds cached data after the existing cached data. For example, the server response of the existing cached key is NOT_STORED.

2 、 prepend

Very similar to append, but its purpose is to add cached data before existing cached data.

3. Flush_all this command has an optional numeric parameter. It always executes successfully, and the server sends a "OK\ r\ n" response. Its effect is to invalidate existing projects immediately (the default) or after a specified time. After that, the retrieve command is executed and nothing will be returned (unless the same key name is re-stored). Flush_all does not actually release the memory occupied by the project immediately, but executes when new items are stored later (this is determined by memcached's laziness detection and deletion mechanism).

The flush_all effect is that it causes all items whose update time is earlier than the time set by flush_all to be ignored when the retrieve command is executed.

4. Other commands

Memcached also has many commands, such as adding and subtracting operations through incr/decr commands that are stored as digital. Here are only commands that are often used in development and operation and maintenance, and others are not illustrated one by one.

Add a note: a simple understanding of several caches in. Net framework

There is no doubt about the importance of caching in web sites. I think the cache that many asp.net developers give priority to using when developing web applications is not a third-party cache solution (such as distributed cache memcached, redis, etc.), but a variety of caching solutions already provided by. Net framework. The following combined with their own development experience to talk about the understanding of cache in. Net framework.

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 HttpContext.Current.Cache instances directly without instantiation. Of course, the Cache class under this namespace allows you to instantiate, and of course you can completely control how to initialize this class if you need to customize your own cache system. I've seen a lot of articles in the garden about Cache's CRUD helper libraries, most of which are aimed at System.Web.Caching.Cache.

It is important to note that we can also use the HttpRuntime.Cache under this namespace to implement caching in different forms, such as web, console, winform, and so on, without instantiating it ourselves. HttpRuntime.Cache is a class that was widely used in personal development, but now prefers the enhanced cache class MemoryCache in. Net framework4.0.

2 、 Output Cache

As we all know, the output cache is mainly divided into page output cache and page partial cache. To put it bluntly, there is nothing to discuss about caching the html or part of the html of the entire page, but when I read this blog post on dudu recently, I suddenly found that I didn't expect to be very particular about using it. Why didn't I find this problem before? It seems that the ability to find problems is as important as the ability to solve problems, and sometimes the former is even more important.

3 、 System.Runtime.Caching

Now the most used class MemoryCache in personal development comes from this namespace, and you need to reference using System.Runtime.Caching before using it. MemoryCache inherits from ObjectCache, IEnumerable, IDisposable, where ObjectCache is an abstract class. Anyone who has used MemoryCache knows that this MemoryCache has an attribute called Default, which can usually be used like this:

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

Then we can configure the memory usage quota scheme and quota checking period for each MemoryCache instance in the web.config file. For the following example, refer to MSDN:

The copy code is as follows:

The significance of these configurations is that you can explicitly specify the memory usage quota scheme and quota check period that each MemoryCache instance runs. For example, we can change the memory quota of MemoryCache.Default instances as needed through configuration (we don't know what the maximum memory available for the cache is, maybe it's about 800m). The cache expiration policy is more or less the same as other caching frameworks, except that the name is not CacheDependency, but ChangeMonitor, and provides a cache dependency policy based on files and directories. It is also necessary to discuss the cache expiration strategy, but personal development pays more attention to data caching and replacement, and has not yet contacted and used a perfect expiration policy solution.

This is the end of the content of "Memcached common commands and instructions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report