Home Database Mysql Tutorial memcached几个容易被忽略但非常有用的命令

memcached几个容易被忽略但非常有用的命令

Jun 07, 2016 pm 03:58 PM
memcached several Order neglect use

最近通读了一遍memcached的源码,在这个过程中发现了许多memcached平时没有注意不常用到的命令,但是经常在使用或运维memcached的时候会碰到正好能用到这些命令的需求,只是当时受限于所用的memcached 版本或memcached client或其他原因,不知道memcached还

最近通读了一遍memcached的源码,在这个过程中发现了许多memcached平时没有注意不常用到的命令,但是经常在使用或运维memcached的时候会碰到正好能用到这些命令的需求,只是当时受限于所用的memcached 版本或memcached client或其他原因,不知道memcached还有这些用法,现在给大家分享一下。

一、CAS和GETS

Memcached从1.2.4版本新增CAS(Check and Set)协议,用于处理同一个ITEM(key-value)被多个session更新修改时的数据一致性问题。

假设有两个session(A、B),要同时修改某个key的值x,并且修改的数据是基于原来数据的一个计算的结果。session A和B同时得到了key的值x,session A经过计算后应该更新为y,session B经过计算后也更新为y,但是session B其实期望的是拿到y值,并将其计算为Z后更新。造成这个问题的原因就是缺少一个类似于MySQL的事务,用于数据并发修改的一致性问题。

CAS命令着眼于解决一定的并发修改问题,引入了乐观锁的概念。在试图修改某个KEY的值之前,先由GETS命令得到某个KEY的值及其版本号,完成数据操作更新数据时,使用CAS谨慎更新,比较版本号是否与本地的版本号一致,否则放弃此次的修改。

Memcached在默认开启CAS协议后,每个key关联有一个64-bit长度的long型惟一数值,表示该key对应value的版本号。这个数值由Memcached server产生,从1开始,且同一Memcached server不会重复。在两种情况下这个版本数值会加1:

1、新增一个key-value对;

2、对某已有key对应的value值更新成功。删除item版本值不会减小。

首先为了获得KEY值的版本号,引入了GETS命令,可以发现GETS命令比GET命令多返回了一个数字,这个数字就是上面我们提到的KEY值的版本号。

然后是CAS命令,与SET命令类似,只是在最后面多了一个参数,也就是key值得版本号,只有版本号与存储的数据版本号一致时,更新操作才会生效。

set a 0 0 1
x
STORED

gets a
VALUE a 0 1 1  //最后一位就是a的版本号
x
END

set a 0 0 1
y
STORED

gets a
VALUE a 0 1 2    //新增或修改之后,版本号都会增加
y

cas a 0 0 1 1    //cas更新的时候,不同于set操作,最后一位要指定版本号,当本地版本号和服务器版本号相同的时候,更新才会有效
x
EXISTS

cas a 0 0 1 2
y
STORED
Copy after login

二、stats items和stats cachedump

你曾经是否也有想知道memcached里面都存了哪些数据的需求,你是否也曾经在寻找一个方法能像redis一样可以遍历memcached所有的key

其实就是应用我们平时经常用到的stats方法。stats方法不仅能获得memcached的一个概况信息,如果加上子命令还可以获得更多的更加详细的信息。如slabs,items等。

stats items命令,可以获得memcached内item组的相关信息,如分组内item的数量,踢掉次数等。后面运行cachedump命令的时候会用到这个命令的返回信息(item组序号)。

stats cachedump命令,可以将某个slab中的items全部dump出来,第一个参数就是上面stats items返回的items组号,也就是slab的编号,第二个参数为一次显示多少个item信息,如果为0就显示这个item组的全部items,第二列就是key。

stats items
STAT items:1:number 3
STAT items:1:age 943
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
END

stats cachedump 1 0
ITEM c [5 b; 1405246917 s]
ITEM b [1 b; 1405246917 s]
ITEM a [1 b; 1405246917 s]
END
Copy after login
虽然应用这两个命令并不能一次显示全部的key,但是如果我们自己根据stats items的返回值自己做一次迭代,或者仅仅是为了手动做几个item的抽样,那么就能很好的帮助我们了解memcached中数据的情况。
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to run SUDO commands in Windows 11/10 How to run SUDO commands in Windows 11/10 Mar 09, 2024 am 09:50 AM

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

How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 Feb 29, 2024 pm 04:34 PM

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

Super practical! Sar commands that will make you a Linux master Super practical! Sar commands that will make you a Linux master Mar 01, 2024 am 08:01 AM

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

Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Feb 29, 2024 pm 05:52 PM

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

cmdtelnet command is not recognized as an internal or external command cmdtelnet command is not recognized as an internal or external command Jan 03, 2024 am 08:05 AM

The cmd window prompts that telnet is not an internal or external command. This problem must have deeply troubled you. This problem does not appear because there is anything wrong with the user's operation. Users do not need to worry too much. All it takes is a few small steps. Operation settings can solve the problem of cmd window prompting telnet is not an internal or external command. Let’s take a look at the solution to the cmd window prompting telnet is not an internal or external command brought by the editor today. The cmd window prompts that telnet is not an internal or external command. Solution: 1. Open the computer's control panel. 2. Find programs and functions. 3. Find Turn Windows features on or off on the left. 4. Find “telnet client

How to use LSOF to monitor ports in real time How to use LSOF to monitor ports in real time Mar 20, 2024 pm 02:07 PM

LSOF (ListOpenFiles) is a command line tool mainly used to monitor system resources similar to Linux/Unix operating systems. Through the LSOF command, users can get detailed information about the active files in the system and the processes that are accessing these files. LSOF can help users identify the processes currently occupying file resources, thereby better managing system resources and troubleshooting possible problems. LSOF is powerful and flexible, and can help system administrators quickly locate file-related problems, such as file leaks, unclosed file descriptors, etc. Via LSOF Command The LSOF command line tool allows system administrators and developers to: Determine which processes are currently using a specific file or port, in the event of a port conflict

How to delete win11 widgets? One command to uninstall Windows 11 widgets function tips How to delete win11 widgets? One command to uninstall Windows 11 widgets function tips Apr 11, 2024 pm 05:19 PM

Widgets are a new feature of the Win11 system. They are turned on by default. However, it is inevitable that some users do not use widgets very much and want to disable them because they take up space. So how should they do this? The editor below will teach you how to operate it, and you can try it out. What are widgets? Widgets are small cards that display dynamic content from your favorite apps and services on your Windows desktop. They appear on the widget board, where you can discover, pin, unpin, arrange, resize, and customize widgets to reflect your interests. The widget board is optimized to display relevant widgets and personalized content based on usage. Open the widget panel from the left corner of the taskbar, where you can see live weather

Artifact in Linux: Principles and Applications of eventfd Artifact in Linux: Principles and Applications of eventfd Feb 13, 2024 pm 08:30 PM

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features

See all articles