Memcache 安装与使用

来源:互联网 发布:linux保存文件并退出 编辑:程序博客网 时间:2024/05/17 04:04
memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。为了提高性能,memcached中保存的数据都存储在memcached内置的内存存储空间中。由于数据仅存在于内存中,因此重启memcached、重启操作系统会导致全部数据消失。另外,内容容量达到指定值之后,就基于LRU(Least Recently Used)算法自动删除不使用的缓存。
安装请先确认gcc、make、patch等编译工具是否已安装,并可正常使用。
libevent是一套跨平台的事件处理接口的封装,能够兼容包括这些操作系统:Windows/Linux/BSD/Solaris 等操作系统的的事件处理。Memcached 使用libevent来进行网络并发连接的处理,能够保持在很大并发情况下,仍旧能够保持快速的响应能力。所以安装Memcached需先安装libevent。
1.安装libevent
1)http://libevent.org/ 下载
wget http://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
2)解压安装
创建安装目录
mkdir libevent
tar -zxvf libevent-2.0.22-stable.tar.gz 
cd libevent-2.0.22-stable
./configure  --prefix=/home/slim/libevent
make
make install
查看/home/slim/libevent目录下已安装的文件
2.安装Memcached
1)http://memcached.org/downloads下载
wget http://memcached.org/files/memcached-1.4.22.tar.gz
2)解压安装
mkdir memcached
tar -zxvf memcached-1.4.22.tar.gz
cd memcached-1.4.22
./configure --prefix=/home/slim/memcached  --with-libevent=/home/slim/libevent
make
make install
安装完成,安装目录/home/slim/memcached
查看安装版本:
 ./bin/memcached -h
memcached 1.4.22
......

3.启动参数介绍
./bin/memcached -hmemcached 1.4.22-p <num>      TCP port number to listen on (default: 11211)-U <num>      UDP port number to listen on (default: 11211, 0 is off)-s <file>     UNIX socket path to listen on (disables network support)-A            enable ascii "shutdown" command-a <mask>     access mask for UNIX socket, in octal (default: 0700)-l <addr>     interface to listen on (default: INADDR_ANY, all addresses)              <addr> may be specified as host:port. If you don't specify              a port number, the value you specified with -p or -U is              used. You may specify multiple addresses separated by comma              or by using -l multiple times-d            run as a daemon-r            maximize core file limit-u <username> assume identity of <username> (only when run as root)-m <num>      max memory to use for items in megabytes (default: 64 MB)-M            return error on memory exhausted (rather than removing items)-c <num>      max simultaneous connections (default: 1024)-k            lock down all paged memory.  Note that there is a              limit on how much memory you may lock.  Trying to              allocate more than that would fail, so be sure you              set the limit correctly for the user you started              the daemon with (not for -u <username> user;              under sh this is done with 'ulimit -S -l NUM_KB').-v            verbose (print errors/warnings while in event loop)-vv           very verbose (also print client commands/reponses)-vvv          extremely verbose (also print internal state transitions)-h            print this help and exit-i            print memcached and libevent license-P <file>     save PID in <file>, only used with -d option-f <factor>   chunk size growth factor (default: 1.25)-n <bytes>    minimum space allocated for key+value+flags (default: 48)-L            Try to use large memory pages (if available). Increasing              the memory page size could reduce the number of TLB misses              and improve the performance. In order to get large pages              from the OS, memcached will allocate the total item-cache              in one large chunk.-D <char>     Use <char> as the delimiter between key prefixes and IDs.              This is used for per-prefix stats reporting. The default is              ":" (colon). If this option is specified, stats collection              is turned on automatically; if not, then it may be turned on              by sending the "stats detail on" command to the server.-t <num>      number of threads to use (default: 4)-R            Maximum number of requests per event, limits the number of              requests process for a given connection to prevent               starvation (default: 20)-C            Disable use of CAS-b            Set the backlog queue limit (default: 1024)-B            Binding protocol - one of ascii, binary, or auto (default)-I            Override the size of each slab page. Adjusts max item size              (default: 1mb, min: 1k, max: 128m)-F            Disable flush_all command-o            Comma separated list of extended or experimental options              - (EXPERIMENTAL) maxconns_fast: immediately close new                connections if over maxconns limit              - hashpower: An integer multiplier for how large the hash                table should be. Can be grown at runtime if not big enough.                Set this based on "STAT hash_power_level" before a                 restart.              - tail_repair_time: Time in seconds that indicates how long to wait before                forcefully taking over the LRU tail item whose refcount has leaked.                The default is 3 hours.              - hash_algorithm: The hash table algorithm                default is jenkins hash. options: jenkins, murmur3              - lru_crawler: Enable LRU Crawler background thread              - lru_crawler_sleep: Microseconds to sleep between items                default is 100.              - lru_crawler_tocrawl: Max items to crawl per slab per run                default is 0 (unlimited)
启动选项介绍:
-p TCP监听端口 (default: 11211)
-U UDP 监听端口 (default: 11211, 0 is off)
-s UNIX socket监听路径,不支持网络
-a UNIX socket访问掩码, 八进制 (default: 0700)
-l <ip_addr> 监听的服务器IP地址 (default: all addresses)
-d 启动一个守护进程,参数需要进行进一步的解释:
install 安装memcached
uninstall 卸载memcached
start 启动memcached服务
restart 重启memcached服务
stop 停止memcached服务
shutdown 停止memcached服务
-r 最大限度利用核心文件限制
-u 运行memcached用户
-m 最大的内存使用 (default: 64 MB)
-M 内存耗尽返回错误
-c 最大并发连接 (default: 1024),按照你服务器的负载量来设定。
-k 锁定所有分页内存
-v 输出警告和错误信息
-vv 同时打印客户端请求和返回信息
-vvv 打印内部状态转换信息
-i 打印memcached 和 libevent 版本信息
-P 设置保存pid文件, only used with -d option
-f 块大小增长倍数 (default: 1.25)
-n key+value+flags最小分配空间(default: 48),key+value+flags默认是48
-L 如何有效,尝试使用大内存页。增加内存页大小可以减少失误的TLB数量,提高性能。
-D 指定key和IDs的分隔符 default is “:” (colon). 如果指定此选项,统计信息收集自动开启;
-t 使用的线程数量 (default: 4)
-R 每个事件的最大请求数 (default: 20)
-C 禁止使用 CAS
-b 设置积压队列数限制 (default: 1024)
-B 绑定协议 – one of ascii, binary, or auto (default)
-I 分配给每个slab页(default: 1mb, min: 1k, max: 128m)

4.启动服务
./bin/memcached -d -m 64 -p 11211 -u slim
可以启动多个后台进程,但端口不能一样
检查服务:
1、查看启动的memcache服务:
netstat -lp | grep memcached
2、查看memcache的进程号(根据进程号,可以结束memcache服务:“kill -9 进程号”)
ps -ef | grep memcached 
3、对Memcached缓存服务的状态查询,可以先telnet连接上服务:telnet 127.0.0.1 11211 ,然后使用 stats命令查看缓存服务的状态,会返回如下的数据:    
STAT pid 27954STAT uptime 139STAT time 1427031155STAT version 1.4.22STAT libevent 2.0.22-stableSTAT pointer_size 32STAT rusage_user 0.005999STAT rusage_system 0.011998STAT curr_connections 10STAT total_connections 11STAT connection_structures 11STAT reserved_fds 20STAT cmd_get 0STAT cmd_set 0STAT cmd_flush 0STAT cmd_touch 0STAT get_hits 0STAT get_misses 0STAT delete_misses 0STAT delete_hits 0STAT incr_misses 0STAT incr_hits 0STAT decr_misses 0STAT decr_hits 0STAT cas_misses 0STAT cas_hits 0STAT cas_badval 0STAT touch_hits 0STAT touch_misses 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 7STAT bytes_written 0STAT limit_maxbytes 67108864STAT accepting_conns 1STAT listen_disabled_num 0STAT threads 4STAT conn_yields 0STAT hash_power_level 16STAT hash_bytes 262144STAT hash_is_expanding 0STAT malloc_fails 0STAT bytes 0STAT curr_items 0STAT total_items 0STAT expired_unfetched 0STAT evicted_unfetched 0STAT evictions 0STAT reclaimed 0STAT crawler_reclaimed 0STAT lrutail_reflocked 0
参数项说明:
time:服务器当前的unix时间戳 
total_items:从服务器启动以后存储的items总数量 
connection_structures:服务器分配的连接构造数 
version:memcache版本 
limit_maxbytes:分配给memcache的内存大小(字节) 
cmd_get:get命令(获取)总请求次数 
evictions:为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items) 
total_connections:从服务器启动以后曾经打开过的连接数 
bytes:当前服务器存储items占用的字节数 
threads:当前线程数 
get_misses:总未命中次数 
pointer_size:当前操作系统的指针大小(32位系统一般是32bit) 
bytes_read:总读取字节数(请求字节数) 
uptime:服务器已经运行的秒数 
curr_connections:当前打开着的连接数 
pid:memcache服务器的进程ID 
bytes_written:总发送字节数(结果字节数) 
get_hits:总命中次数 
cmd_set:set命令(保存)总请求次数 
curr_items:服务器当前存储的items数量

5.通过set和get命令测试写入键值对和查询键值对
1)写入键值对
set li 0 0 3
var
STORED
命令解释:
set
-Flag 用于指定是否压缩数据,0不压缩,1压缩
-Expires 指定数据保存的时间,一种方法是秒数(不能超过30天),另一种可以使用unix时间戳指定,0为有效期无限
-Byte 保存值的字节数
2)查询键值对
get li
VALUE li 0 3
var
END
3.其它操作
set counter 0 0 1
1
STORED
incr counter 1 //自增
2
get counter
VALUE counter 0 1
2
END
append counter 0 0 4 //追加
test
STORED
get counter
VALUE counter 0 5
2test
END
delete counter //删除指定键值对
DELETED
flush_all //清空所有键值对
OK
get counter
END

有关常用命令及使用请参考:http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html


参考文章
Linux下的Memcache安装:http://www.ccvita.com/257.html
Windows下的Memcache安装:http://www.ccvita.com/258.html
Memcache基础教程:http://www.ccvita.com/259.html
Discuz!的Memcache缓存实现:http://www.ccvita.com/261.html
Memcache协议中文版:http://www.ccvita.com/306.html
Memcache分布式部署方案:http://www.ccvita.com/395.html
0 0
原创粉丝点击