Memcached常用命令以及使用说明详解
存储命令的格式:
参数说明如下:
set/add/replace | |
查找关键字 | |
客户机使用它存储关于键值对的额外信息 | |
该数据的存活时间,0表示永远 | |
存储字节数 | |
存储的数据块(可直接理解为key-value结构中的value) |
(1)、无论如何都存储的set
这个set的命令在memcached中的使用频率极高。set命令不但可以简单添加,如果set的key已经存在,该命令可以更新该key所对应的原来的数据,也就是实现更新的作用。
可以通过“get 键名”的方式查看添加进去的记录:
如你所知,我们也可以通过delete命令删除掉,然后重新添加。
(2)、只有数据不存在时进行添加的add
(3)、只有数据存在时进行替换的replace
2、删除可以看到,删除已存在的键值和不存在的记录可以返回不同的结果。
二、读取命令1、get
get命令的key可以表示一个或者多个键,键之间以空格隔开
2、gets
可以看到,gets命令比普通的get命令多返回了一个数字(上图中为13)。这个数字可以检查数据是否发生改变。当key对应的数据改变时,这个多返回的数字也会改变。
3、cas
cas即checked and set的意思,只有当最后一个参数和gets所获取的参数匹配时才能存储,否则返回“EXISTS”。
三、状态命令1、stats
2、stats items
执行stats items,可以看到STAT items行,如果memcached存储内容很多,那么这里也会列出很多的STAT items行。
3、stats cachedump slab_id limit_num
我们执行stats cachedump 1 0 命令效果如下:这里slab_id为1,是由2中的stats items返回的结果(STAT items后面的数字)决定的;limit_num看起来好像是返回多少条记录,猜的一点不错, 不过0表示显示出所有记录,而n(n>0)就表示显示n条记录,如果n超过该slab下的所有记录,则结果和0返回的结果一致。
通过stats items、stats cachedump slab_id limit_num配合get命令可以遍历memcached的记录。
4、其他stats命令
如stats slabs,stats sizes,stats reset等等使用也比较常见。
四、其他常见命令
1、append 在现有的缓存数据后添加缓存数据,如现有缓存的key不存在服务器响应为NOT_STORED。
2、prepend
和append非常类似,但它的作用是在现有的缓存数据前添加缓存数据。
3、flush_all 该命令有一个可选的数字参数。它总是执行成功,服务器会发送 “OK\r\n” 回应。它的效果是使已经存在的项目立即失效(缺省),或在指定的时间后。此后执行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。 flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行(这是由memcached的懒惰检测和删除机制决定的)。
flush_all 效果是它导致所有更新时间早于 flush_all 所设定时间的项目,在被执行取回命令时命令被忽略。
4、其他命令
memcached还有很多命令,比如对于存储为数字型的可以通过incr/decr命令进行增减操作等等,这里只列出开发和运维中经常使用的命令,其他的不再一一举例说明。
补充一则:简单认识.net framework中的几种缓存
web站点中缓存的重要性毋庸置疑。我想很多asp.net开发人员在开发web应用系统的时候优先考虑使用的缓存并不是第三方缓存解决方案(比如分布式缓存memcached、redis等等),而应该是.net framework已经提供的多种缓存解决方案。下面结合自己的开发经验谈谈对.net framework中缓存的认识。
1、System.Web.Caching.Cache
估计大部分做过asp.net开发的人都用过这个命名空间下的缓存,我们可以直接使用HttpContext.Current.Cache实例而不用实例化。当然这个命名空间下的Cache类是允许您实例化的,需要定制自己的缓存系统的当然可以完全自己控制如何初始化这个类。我在园子里看到过有很多文章介绍Cache的CRUD辅助类库大多数都是针对System.Web.Caching.Cache。
需要说明的是,我们还可以通过该命名空间下的HttpRuntime.Cache实现web、控制台、winform等不同表现形式下的缓存,而且完全无需自己实例化。HttpRuntime.Cache是之前个人开发中使用比较多的一个类,现在比较偏爱.net framework4.0中的增强型的缓存类MemoryCache。
2、Output Cache
众所周知,输出缓存主要分页面输出缓存和页面部分缓存。说白了,就是缓存整个页面的html或者部分html,本来没什么值得讨论的,但是最近看到dudu的这篇博客才恍然发现,想不到使用它还真是大有讲究,我以前怎么就没有发现这个问题呢?看来发现问题和解决问题的能力同样重要,有时候前者甚至更重要啊。
3、System.Runtime.Caching
现在个人开发中使用最多的类MemoryCache出自这个命名空间,使用前需要引用using System.Runtime.Caching。MemoryCache继承自ObjectCache, IEnumerable, IDisposable,其中ObjectCache是个抽象类。用过MemoryCache的人都知道,这个MemoryCache有一个属性叫Default,通常可以像下面这样使用:
private static ObjectCache memCache = MemoryCache.Default;当然我们也完全可以通过public MemoryCache(string name, NameValueCollection config = null)构造函数初始化缓存对象。
接着我们可以在web.config文件中配置每个MemoryCache实例运行的内存使用配额方案和配额检查周期,下面示例参考MSDN:
复制代码 代码如下:
这些配置意义在于可以明确指定每个MemoryCache实例运行的内存使用配额方案和配额检查周期。比如我们可以通过配置来按需更改MemoryCache.Default实例的内存配额(不知道缓存可用最大内存是多少,可能还是传说中的800M左右)。缓存过期策略与其它的缓存框架大同小异,与System.Web.Caching.Cache的不同只是名称不叫CacheDependency,而叫ChangeMonitor,并且提供了基于文件和目录的缓存依赖策略。关于缓存过期策略也比较有探讨的必要,不过个人开发中比较偏重于数据缓存和替换,目前还没有接触和使用过比较完美的过期策略解决方案。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager
