Home  >  Article  >  Backend Development  >  Detailed explanation of common Memcached commands and usage instructions_PHP tutorial

Detailed explanation of common Memcached commands and usage instructions_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:56827browse

存储命令的格式:


参数说明如下:

set/add/replace
查找关键字
客户机使用它存储关于键值对的额外信息
该数据的存活时间,0表示永远
存储字节数
存储的数据块(可直接理解为key-value结构中的value)
1、添加
(1)、无论如何都存储的setset

这个set的命令在memcached中的使用频率极高。set命令不但可以简单添加,如果set的key已经存在,该命令可以更新该key所对应的原来的数据,也就是实现更新的作用。

可以通过“get 键名”的方式查看添加进去的记录:

set_get

如你所知,我们也可以通过delete命令删除掉,然后重新添加。

delete

(2)、只有数据不存在时进行添加的add

add

(3)、只有数据存在时进行替换的replace

replace

 2、删除

delete

可以看到,删除已存在的键值和不存在的记录可以返回不同的结果。

二、读取命令1、get

get命令的key可以表示一个或者多个键,键之间以空格隔开

get
2、gets gets

可以看到,gets命令比普通的get命令多返回了一个数字(上图中为13)。这个数字可以检查数据是否发生改变。当key对应的数据改变时,这个多返回的数字也会改变。

3、cas
cas即checked and set的意思,只有当最后一个参数和gets所获取的参数匹配时才能存储,否则返回“EXISTS”。cas

三、状态命令1、stats
stats
2、stats items statsitems
执行stats items,可以看到STAT items行,如果memcached存储内容很多,那么这里也会列出很多的STAT items行。

3、stats cachedump slab_id limit_num

我们执行stats cachedump 1 0 命令效果如下:statscachedump这里slab_id为1,是由2中的stats items返回的结果(STAT items后面的数字)决定的;limit_num看起来好像是返回多少条记录,猜的一点不错, 不过0表示显示出所有记录,而n(n>0)就表示显示n条记录,如果n超过该slab下的所有记录,则结果和0返回的结果一致。statscachedump1
通过stats items、stats cachedump slab_id limit_num配合get命令可以遍历memcached的记录。

4、其他stats命令
如stats slabs,stats sizes,stats reset等等使用也比较常见。statsother

四、其他常见命令
1、append
append在现有的缓存数据添加缓存数据,如现有缓存的key不存在服务器响应为NOT_STORED。

2、prepend
和append非常类似,但它的作用是在现有的缓存数据添加缓存数据。prepend
3、flush_all flush_all该命令有一个可选的数字参数。它总是执行成功,服务器会发送 “OK\r\n” 回应。它的效果是使已经存在的项目立即失效(缺省),或在指定的时间后。此后执行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。 flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行(这是由memcached的懒惰检测和删除机制决定的)。

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

4. Other commands
memcached also has many commands. For example, for numbers stored in numbers, you can use the incr/decr command to increase or decrease operations, etc., only development is listed here. and commands frequently used in operation and maintenance. Others will not be explained one by one with examples.

An additional note: Briefly understand several types of caches in the .net framework

The importance of caching in web sites is undoubted. I think the cache that many ASP.NET developers prioritize when developing web application systems is not third-party caching solutions (such as distributed cache memcached, redis, etc.), but the various caches already provided by the .NET framework. Caching solution. Let's talk about the understanding of caching in .net framework based on my 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. We can use HttpContext.Current directly. Cache instance without instantiation. Of course, the Cache class under this namespace allows you to instantiate it. If you need to customize your own cache system, you can of course fully control how to initialize this class. I have seen many articles in the garden introducing Cache's CRUD auxiliary class libraries. Most of them are for System.Web.Caching.Cache.

It should be noted that we can also use HttpRuntime.Cache under this namespace to implement caching in different manifestations such as web, console, winform, etc., and there is no need to instantiate it by ourselves. HttpRuntime.Cache is a class that was used frequently in personal development before. Now I prefer the enhanced cache class MemoryCache in .net framework 4.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, it means caching the HTML of the entire page or part of the HTML. It was originally nothing worth discussing, but recently I saw this blog by dudu and suddenly realized that using it is really important. Why didn’t I find this problem before? ? It seems that the ability to find problems is equally important as the ability to solve problems, and sometimes the former is even more important.

3. System.Runtime.Caching
MemoryCache, the most commonly used class in personal development now, comes from this namespace and needs to be referenced using System.Runtime.Caching before use. 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 as follows:

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 check cycle of each MemoryCache instance in the web.config file. For the following example, refer to MSDN:

Copy code The code is as follows:





🎜>

The significance of these configurations is that you can clearly specify the memory usage quota scheme and quota check period for each MemoryCache instance to run. For example, we can change the memory quota of the MemoryCache.Default instance on demand through configuration (I don’t know what the maximum memory available for the cache is, it may still be around the legendary 800M). The cache expiration strategy is similar to other caching frameworks. The only difference from System.Web.Caching.Cache is that the name is not CacheDependency, but
ChangeMonitor
, and it provides file- and directory-based cache dependency strategies. There is also a need to discuss the cache expiration strategy. However, my personal development focuses on data caching and replacement. I have not yet come across and used a relatively perfect expiration strategy solution.

http://www.bkjia.com/PHPjc/327884.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327884.htmlTechArticleFormat of storage command: command name key flags exptime bytes data block Parameter description is as follows: command name set/add/replace key looks up keyword flags, which the client uses to store information about the key...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn