什么Memcached?
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
#Memcached服务端环境准备 yum install gcc* -y [root@A-host tools]# tar xf libevent-1.4.13-stable.tar.gz [root@A-host tools]# cd libevent-1.4.13-stable [root@A-host tools]# ./configure [root@A-host tools]# make && make install [root@A-host tools]# tar xf memcached-1.4.13.tar.gz [root@A-host tools]# cd memcached-1.4.13 [root@A-host tools]# ./configure [root@A-host tools]# make && make install #配置ld.so.conf路径防止启动memcached报错 [root@A-host memcached-1.4.13]# echo "/usr/local/lib">>/etc/ld.so.conf [root@A-host memcached-1.4.13]# ldconfig
[root@A-host memcached-1.4.13]# memcached -help memcached 1.4.13 -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 <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) -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.
#启动 [root@A-host memcached-1.4.13]# memcached -p 11211 -u root -m 16m -c 10240 -d [root@A-host memcached-1.4.13]# memcached -p 11212 -u root -m 16m -c 10240 -d 可放入rc.local启动命令。
#写入检测结果 方法1: printf "set key008 0 0 10\r\noldboy0987\r\n"|nc 127.0.0.1 11211 printf "get key008\r\n"|nc 127.0.0.1 11211 printf "delete key008\r\n"|nc 127.0.0.1 11211 方法2: printf "stats \r\n"|nc 127.0.0.1 11211 printf "stats items\r\n"|nc 127.0.0.1 11211 printf "stats sizes\r\n"|nc 127.0.0.1 11211 printf "stats slabs\r\n"|nc 127.0.0.1 11211 方法3: [root@A-host tools]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. stats STAT pid 11354 STAT uptime 48907 STAT time 1534166615 STAT version 1.4.13 STAT libevent 1.4.13-stable STAT pointer_size 64 STAT rusage_user 0.177972 STAT rusage_system 1.483774 STAT curr_connections 10 STAT total_connections 68 STAT connection_structures 13 STAT reserved_fds 20 STAT cmd_get 7 STAT cmd_set 4 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 3 STAT get_misses 4 STAT delete_misses 0 STAT delete_hits 3 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 9148 STAT bytes_written 1087 STAT limit_maxbytes 16777216 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT bytes 0 STAT curr_items 0 STAT total_items 3 STAT evictions 0 STAT reclaimed 0 END
#关闭方法 [root@A-host memcached-1.4.13]# memcached -p 11211 -u root -m 16m -c 10240 -d -P /var/run/11211.pid [root@A-host memcached-1.4.13]# memcached -p 11212 -u root -m 16m -c 10240 -d -P /var/run/11212.pid kill `cat /var/run/11212.pid` pkill memcached
#Memcache客户端 Memcache缓存软件(客户端) tar xf memcache-2.2.5.tgz cd memcache-2.2.5 /application/php/bin/phpize ./configure --enable-xcache --with-php-config=/application/php/bin/php-config make && make install [root@E-host memcache-2.2.5]# ll /www/server/php/55/lib/php/extensions/no-debug-non-zts-20121212/ total 612 -rwxr-xr-x 1 root root 258027 Aug 26 04:43 memcache.so #编辑php.ini 添加 memcache.so ; extension_dir = "./" extension_dir = "/www/server/php/55/lib/php/extensions/no-debug-non-zts-20121212/" extension = memcache.so
停留在世界边缘,与之惜别