search
HomeDatabaseMysql Tutorialmemcached几个容易被忽略但非常有用的命令

最近通读了一遍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

二、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
虽然应用这两个命令并不能一次显示全部的key,但是如果我们自己根据stats items的返回值自己做一次迭代,或者仅仅是为了手动做几个item的抽样,那么就能很好的帮助我们了解memcached中数据的情况。
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
When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment