memcached基本介绍
author:madding.lip
date: 2010.07.21
特点
-
协议简单:基于文本
-
基于libevent的事件处理
-
内置内存存储方式
-
memcached 不互相通信的分布式
协议
-
http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
结构
-
c/s结构
常用命令
连接
-
命令
-
telnet ip port
-
-
举例
存储命令
-
命令1:
<commandname> <key> <flags> <exptime> <bytes>[noreply]\r\n-
command
-
set
-
存储数据,storethis data
-
-
-
-
add
-
当且仅当这个key不存在时才添加数据,storethis data, but only if the server *doesn't* already hold data forthis key
-
-
-
replace
-
当且仅当这个key存在是才添加数据,storethis data, but only if the server *does* already hold data forthis key
-
-
-
append
-
添加数据到存在key的数据后,addthis data to an existing key after existing data
-
-
-
prepend
-
添加数据到存在key的数据后,addthis data to an existing key before existing data
-
-
-
key
-
the key under which the clientasks to store the data
-
-
-
flags
-
?个人理解是有点像标志,客户端聚合一些相似的数据,服务器端存储则是透明的
-
-
-
exptime
-
过期时间。0:永不过期;单位:秒
-
-
-
bytes
-
数据bytes大小
-
-
-
noreply
-
-
命令2:
cas <key> <flags><exptime> <bytes> <cas unqiue> [noreply]\r\n
-
cas
-
存储一个数据,改数据在上次获取之后没有更新过,storethis data but only if no one else has updated since I last fetchedit
-
-
-
cas unique
-
?
-
-
-
返回格式
-
成功:STORED\r\n
-
-
失败:NOT_STORED\r\n
-
-
存在:EXISTS\r\n
-
-
不存在:NOT_FOUND\r\n
-
取回命令
命令格式1:get <key>*\r\n命令2:gets<key>*\r\n
-
如果是请求一个列表,列表用空格分开
返回格式
-
VALUE <key> <flags><bytes> [<cas unique>]\r\n<data block>\r\nEND\r\n
删除命令
-
命令格式1:delete<key> [<time>] [noreply]\r\n
-
命令格式2:flush_all
-
返回格式
-
删除:DELETED\r\n
-
-
没找到:NOT_FOUND\r\n
-
增加/减少
增加:incr<key> <value> [noreply]\r\n
删除:decr<key> <value> [noreply]\r\n
返回格式
-
没发现:NOT_FOUND\r\n
-
值:<value>\r\n
统计命令
命令1:stats\r\n
命令2:stats<args>\r\n
返回格式
参数说明
-
返回结果参数说明:
NameType Meaning
----------------------------------
pid 32uProcess id of this server process
uptime 32u Number of secondsthis server has been running
time 32u current UNIX time accordingto the server
version string Version string of thisserver
pointer_size 32 Default size of pointers on the host OS
(generally 32 or 64)
rusage_user 32u:32u Accumulated user timefor this process
(seconds:microseconds)
rusage_system32u:32u Accumulated system time for this process
(seconds:microseconds)
curr_items 32u Current number of itemsstored by the server
total_items 32u Total number of items storedby this server
ever since it started
bytes 64u Currentnumber of bytes used by this server
to storeitems
curr_connections 32u Number of openconnections
total_connections 32u Total number of connectionsopened since
the server started running
connection_structures32u Number of connection structures allocated
by theserver
cmd_get 64u Cumulative number of retrievalrequests
cmd_set 64u Cumulative number of storagerequests
get_hits 64u Number of keys that have been requested and
found present
get_misses 64u Number of items that have beenrequested
and not found
evictions 64u Number of valid itemsremoved from cache
to free memory for new items
bytes_read64u Total number of bytes read by this server
fromnetwork
bytes_written 64u Total number of bytes sent by thisserver to
network
limit_maxbytes 32u Number of bytes thisserver is allowed to
use for storage.
threads 32u Number ofworker threads requested.
(see doc/threads.txt)
其他
version
verbosity
quit
内存存储(slaballocation)
-
原理:按照预先规定的大小,将分配的内存分割成特定长度的块,以完全解决内存碎片问题
-
命令
-
启动memcached
-
memcached -p 11211 -m 1m -vvv
-
-
-
memcached -p 11212 -m 1m-d
-
-
设置增长系数
-
memcached -p 11211 -m 1m -f 1.25-vvv
-
-
-
madding@wmmad:~$memcached -p 11212 -vvv -f 1.25 -m 1B
slab class 1: chunksize 80perslab 13107
slab class 2: chunksize 104 perslab 10082
...................................................
slab class 40: chunksize 573744 perslab 1
slab class 41: chunksize 717184 perslab 1
slab class 42: chunksize 1048576 perslab 1
-
-
状态查看
-
stats
-
-
-
查看slbs使用状况
-
-
具体参考memcached的安装man手册
-
-
概念
-
page
-
分配给Slab的内存空间,默认是1MB。分配给Slab之后根据slab的大小切分成 chunk
-
-
-
chunk
-
用于缓存记录的内存空间
-
-
-
slab class
-
特定大小的chunk的组
-
-
-
关系
-
多个chunk组成slabclass,多个slab class构成page
-
-
-
记录(存储)
-
根据收到记录的大小选择合适的slab,再根据slab中保存的空闲chunk表,找到chunk,存储记录
-
-
优缺点
-
优点:减少内存分配
-
-
缺点:空间浪费
-
-
其实都是权衡后的结果,只要适当控制chunk之间的大小,就能减少空间的浪费
-
删除机制
-
获取时检查过期
-
禁止LRU:memcached-M -m 1024