Memcached常用操作

简介:

memcached是一个高性能的、分布式内存对象缓存系统,应用广泛。 通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、 提高可扩展性。
它可以应对任意多个连接,使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。还使用内置的内存块分配和哈希表算法,确保虚拟内存不会过来捣乱。
Memcached 官方网站:http://www.danga.com/memcached 

二. memcached 的安装:
注:memcached 用到了libevent这个库用于Socket的处理,所以还需要安装libevent.官网:http://www.monkey.org/~provos/libevent/
1. 先安装libevent:
[root@localhost software]# tar zxvf libevent-1.4.11-stable.tar.gz 
[root@localhost libevent-1.4.11-stable]# ./configure –prefix=/usr 
[root@localhost libevent-1.4.11-stable]# make 
[root@localhost libevent-1.4.11-stable]# make install 

2. 测试libevent是否安装成功
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent 
lrwxrwxrwx   1 root root       22 07-10 13:10 libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
-rwxr-xr-x   1 root root    31596 2007-01-07 libevent-1.1a.so.1.0.2
lrwxrwxrwx   1 root root       21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x   1 root root   308088 07-21 03:33 libevent-1.4.so.2.1.3
-rw-r--r--   1 root root   394474 07-21 03:33 libevent.a
lrwxrwxrwx   1 root root       26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x   1 root root   109490 07-21 03:33 libevent_core-1.4.so.2.1.3
-rw-r--r--   1 root root   148742 07-21 03:33 libevent_core.a
-rwxr-xr-x   1 root root      866 07-21 03:33 libevent_core.la
lrwxrwxrwx   1 root root       26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx   1 root root       27 07-21 03:33 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x   1 root root   246870 07-21 03:33 libevent_extra-1.4.so.2.1.3
-rw-r--r--   1 root root   307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x   1 root root      873 07-21 03:33 libevent_extra.la
lrwxrwxrwx   1 root root       27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x   1 root root      831 07-21 03:33 libevent.la
lrwxrwxrwx   1 root root       21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3
 

安装OK。

3. 安装memcached,同时需要安装中指定libevent的安装位置
[root@localhost software]# tar zxvf memcached-1.4.0.tar.gz 
[root@localhost memcached-1.4.0]# ./configure –with-libevent=/usr 
[root@localhost memcached-1.4.0]# make 
[root@localhost memcached-1.4.0]# make intall 

4. 测试是否成功安装memcached
[root@localhost memcached-1.4.0]# ls -al /usr/local/bin | grep memcached 
-rwxr-xr-x  1 root root  188225 07-21 03:35 memcached 

安装OK。

三. 如何启动 memcached 服务:
只需要启动一个 memcached 监护进程,监护进程不需要配置文件,只要在命令行里面加三四个参数就可以了:
[root@localhost bin]# memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid 
-d: (run as a daemon) 选项是启动一个守护进程 
-m:(max memory to use for items in megabytes (default: 64 MB))是分配给Memcache使用的内存数量,单位是MB,我这里是100MB,
-u:(assume identity of <username> (only when run as root))是运行Memcache的用户,我这里是root,
-l:(interface to listen on)是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址127.0.0.1,
-p:是设置Memcache监听的端口,这里设置了11211,最好是1024以上的端口,
-c:选项是最大运行的并发连接数,默认是1024,这里设置了256,根据服务器的负载量来设定,
-P:(save PID in <file>, only used with -d option)是设置保存Memcache的pid文件,这里是保存在 /tmp/memcached.pid
 

注:也可以启动多个守护进程,不过端口不能重复。

四. 安装 Memcached 的PHP扩展:
在PHP中使用Memcached,有两种方式:
一种是安装PHP的memcached扩展。该扩展是用c写的,效率较高,需要在服务器上安装。
另外一种则是直接使用客户端的php-memcached-client类库。
下面是使用PECL中Memcache的专用扩展,因为毕竟是用C写的,效率高,而且安装部署起来也比较方便。
1. 在 http://pecl.php.net/package/memcache 选择相应想要下载的memcache版本。我下载的是:memcache-2.2.5.tgz 版本。
2. 安装 memcache
[root@localhost software]# tar zxvf memcache-2.2.5.tgz 
[root@localhost software]# cd memcache-2.2.5 
[root@localhost memcache-2.2.5]# /usr/bin/phpize 
[root@localhost memcache-2.2.5]# ./configure –enable-memcache –with-php-config=/usr/bin/php-config –with-zlib-dir 
[root@localhost memcache-2.2.5]# make 
[root@localhost memcache-2.2.5]# make install 
这步会有类似这样的提示:Installing shared extensions: /usr/local/php/modules
3. 把/etc/php.ini中的extension_dir = “./”修改为:extension_dir = "/usr/lib/php/modules"
4. 并添加: extension=memcache.so

也可执行以下shell命令,对php.ini文件的修改:
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/n#' /usr/local/webserver/php/etc/php.ini


五. 安装C/C++ Memcached客户端库:libmemcached
下载:http://download.tangent.org/libmemcached-0.32.tar.gz
1. 安装 libmemcached
[root@localhost src]# tar zxvf libmemcached-0.32.tar.gz 
[root@localhost src]# cd libmemcached-0.32 
[root@localhost libmemcached-0.32]# ./configure --prefix=/usr 
[root@localhost libmemcached-0.32]# make && make install 
2. 检查安装结果
[root@localhost src]# ls /usr/lib/libmemcache* //库文件
[root@localhost src]# ls /usr/include/libmemcached/* //头文件
[root@localhost src]# ls /usr/bin/mem* //命令行工具

六. 应用:
1. 启动 memcache 服务
[root@localhost bin]# memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid 
2. 重启 Web 服务器
[root@localhost bin]# service httpd restart 






一、Memcache面向对象的常用接口包括:
Memcache::connect — 打开一个到Memcache的连接
Memcache::pconnect — 打开一个到Memcache的长连接
Memcache::close — 关闭一个Memcache的连接
Memcache::set — 保存数据到Memcache服务器上
Memcache::get — 提取一个保存在Memcache服务器上的数据
Memcache::replace — 替换一个已经存在Memcache服务器上的项目
Memcache::delete — 从Memcache服务器上删除一个保存的项目
Memcache::flush — 刷新所有Memcache服务器上保存的项目(类似于删除所有的保存的项目)
Memcache::getStats — 获取当前Memcache服务器运行的状态

For More: http://cn.php.net/memcache

二、查看系统的运行状态:

stats
pid               Process id of this server process (memcache服务器的进程ID
uptime            Number of seconds this server has been running (服务器已经运行的秒数)
time              Current UNIX time according to the server (服务器当前的UNIX时间)
version           Version string of this server (memcache版本)
pointer_size      Current system pointer 当前操作系统的指针大小(32位系统一般是32bit)
rusage_user       Accumulated user time for this process (该进程累计的用户时间(秒:微妙))
rusage_system     Accumulated system time for this process (该进程累计的系统时间(秒:微妙))
curr_items        Current number of items stored by the server (服务器当前存储的内容数量
total_items       Total number of items stored by this server ever since it started (服务器启动以来存储过的内容总数
bytes             Current number of bytes used by this server to store items (服务器当前存储内容所占用的字节数)
curr_connections  Number of open connections (当前打开着的连接数量)
total_connections Total number of connections opened since the server started running (服务器运行以来接受的连接总数)
connection_structures Number of connection structures allocated by the server (服务器分配的连接结构的数量)
cmd_get             Cumulative number of retrieval requests (get命令(获取)总请求次数)
cmd_set             Cumulative number of storage requests (set命令(保存)总请求次数)
get_hits            Number of keys that have been requested and found present (请求成功的总次数)
get_misses          Number of items that have been requested and not found (请求失败的总次数)
threads             Current number of thread (当前线程数)
bytes_read          Total number of bytes read by this server from network (服务器从网络读取到的总字节数)
bytes_written       Total number of bytes sent by this server to network (服务器向网络发送的总字节数)
limit_maxbytes      Number of bytes this server is allowed to use for storage. (服务器在存储时被允许使用的字节总数)
evictions           Number of valid items removed from cache to free memory for new items (为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items))

其中,最关注最多的几个参数:
uptime:是memcached运行的秒数。
cmd_get:是查询缓存的次数。
cmd_get/uptime 结果是平均每秒请求缓存的次数——结果值越大,说明Memcached的利用率越高,站点的访问量大,如果太低,用文件系统缓存就可以了,根本不会体现出使用memcached的强大性能。
cmd_set:是设置key=>value的次数。整个memcached是个大hash,用cmd_get没有找到的内容,就会调用一下cmd_set写进缓存里。
get_hits:是缓存命中的次数。所谓的命中率 = get_hits/cmd_get * 100%。
get_misses:是缓存未命中的次数。get_misses加上get_hits就等于cmd_get。
stats:显示服务器信息、统计数据等
stats reset:清空统计数据

stats slabs:显示各个slab的信息,包括chunk的大小、数目、使用情况等
stats items:显示各个slab中item的数目和存储时长(最后一次访问距离现在的秒数)
quit:退出

三、利用shell命令操作Memcached
1、数据存储(key为wan,value为123)
set
2、数据取回
get
3、替换数据(将以wan为key存储的值替换为122)
replace
4、数值增加 1
incr
5、数值减少 2
decr
6、数据删除
delete
7、查看Memcached当时状态
printf “stats\r\n” | nc 127.0.0.1 11211
8、查看Memcached实时状态

watch “printf ‘stats\r\n’ | nc 127.0.0.1 11211″

watch

Memcached protocol 中英文档可以参考:

http://blog.s135.com/book/memcached/

四. 查看slabs的使用状况
使用memcached的创造着Brad写的名为 memcached-tool 的Perl脚本,可以方便地获得slab的使用情况(它将memcached的返回值整理成容易阅读的格式)。可以从下面的地址获得脚本:
http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool
[root@localhost html]# vim memcached-tool
[root@localhost html]# chmod +x memcached-tool
[root@localhost html]# ./memcached-tool 127.0.0.1:11211
#  Item_Size   Max_age  1MB_pages Count   Full?
1      80 B        0 s               1           0      no
2     104 B       12175 s         1           1      no
3     176 B    1339587 s       33       196567  yes

各列的含义:
#: slab class编号
Item_Size: Chunk大小
Max_age: LRU内最旧的记录的生存时间
1MB_pages: 分配给Slab的页数
Count: Slab内的记录数
Full?: Slab内是否含有空闲chunk

五. 也可以图形化监控 Memcached 的运行状态
http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/
是一个PHP源文件,只需要修改源码中的用户名、密码以及数组$MEMCACHE_SERVERS 就可以了。





一、存储命令

存储命令的格式:

1
2
<command name> <key> <flags> <exptime> <bytes>
<data block>

参数说明如下:

<command name> set/add/replace
<key> 查找关键字
<flags> 客户机使用它存储关于键值对的额外信息
<exptime> 该数据的存活时间,0表示永远
<bytes> 存储字节数
<data block> 存储的数据块(可直接理解为key-value结构中的value)

1、添加

(1)、无论如何都存储的set

set

这个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的懒惰检测和删除机制决定的)。

flush_all 效果是它导致所有更新时间早于 flush_all 所设定时间的项目,在被执行取回命令时命令被忽略。

4、其他命令

memcached还有很多命令,比如对于存储为数字型的可以通过incr/decr命令进行增减操作等等,这里只列出开发和运维中经常使用的命令,其他的不再一一举例说明。










本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1727469,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
存储 NoSQL 安全
Redis相关命令详解及其原理:Redis基本操作、数据结构以及应用
Redis相关命令详解及其原理:Redis基本操作、数据结构以及应用
90 0
|
2月前
|
缓存 NoSQL Redis
如何在Python中使用Redis或Memcached进行缓存?
如何在Python中使用Redis或Memcached进行缓存?
26 2
|
3月前
|
存储 NoSQL Redis
redis基础数据类型常用命令笔记
redis基础数据类型常用命令笔记
14 0
|
4月前
|
存储 缓存 NoSQL
Redis的安装方法与基本操作
Redis是一个开源的内存数据结构存储系统,也可以用作数据库、缓存和消息中间件。它支持多种数据结构,包括字符串(strings)、哈希(hashes)、列表(lists)、集合(sets)、有序集合(sorted sets)等。
81 0
Redis的安装方法与基本操作
|
4月前
|
NoSQL Unix Redis
Redis - 五种数据类型与常用操作详解-1
Redis - 五种数据类型与常用操作详解-1
26 0
|
9月前
|
存储 SQL NoSQL
Redis 的基础命令集合
Redis 的基础命令集合
103 0
|
12月前
|
缓存 NoSQL Redis
redis redis常用命令及内存分析总结(附RedisClient工具简介
redis redis常用命令及内存分析总结(附RedisClient工具简介
154 0
|
存储 SQL 缓存
Redis数据库的介绍、安装、数据结构、常用命令
Redis数据库的介绍、安装、数据结构、常用命令
161 0
Redis数据库的介绍、安装、数据结构、常用命令
|
消息中间件 缓存 NoSQL
|
存储 NoSQL Unix
Redis:Bitmaps使用场景和常用命令
Redis:Bitmaps使用场景和常用命令
299 0