本文共 3438 字,大约阅读时间需要 11 分钟。
httpwatch
key:value
name:jerry
http web object:memcached
ip
tcp,udp
telnet ip 80
get uri http/1.1 hostget uri
put /etc/issueget,put,mget,mput
simple protocol,
http:textftp:text,binaryxml
procotol:getsetmemcached:缓存服务器,但本身无法决定缓存任何数据,一半依赖于客户端,一半依赖于服务器
set key 5 60 hellolazy:惰性,LRU,最近最少使用内容缓存服务器: 48bytes 1MB
index.html:10k
test.jpg:30kbuddy system:伙伴系统
避免内存外碎片slab allocator:slab分配器避免内存内碎片memcached:不通信分布式缓存服务器
index.html/2
已用,空闲
20bytes
80bytes72增长因子
growth factor,1.2548bytes:slab class,slab chunk80bytes工作模式:lazy(惰性)
lur(最近最小使用)memcached又叫内存缓存服务器,缓存都存在内存中 存储大小为48byte--1MB,比如把一个内存分成每个单元48byte--1MBindex.html:10K
test.jpg:34kbuddy system:伙伴系统,避免内存外碎片
slab allocator:slab分配器,避免内存内碎片memcached:增长因子:growth factor,每次增长多少
如果分了一堆大小为单元为48字节的,这一堆就叫做slab classmemcached安装配置
even-driven由libevent提供,主要功能是提供事件驱动
rpm安装:
yum install libeventyum install memcachedyum inatll cyrus-sasl编译安装:
tar -zxvf libevent-2.0.20-stable.tar.gzcd libevent./configure --prefix=/usr/local/libeventmakemake installtar -zxvf memcache-1.4.15.tar.gz
cd memcache-1.4.15./configure --enable-sasl --prefix=/usr/local/memcached --with-libevent=/usr/local/libeventmakemake installmemcached(见文档)
-p:TCP port,1111-m #:以MB为单位,指定用于缓存的最大内存空间-d:以服务模式运行-u <username>:以指定的用户身份运行memcached进程-f <num>:设定slab allocator定义预先分配内存空间大小固定的块时使用的增长因子-n:指定最小的slab chunk大小,单位字节-S:启用sasl进行用户认证功能/usr/local/memcached/bin/memcached -m 128 -n 20 -f 1.1 -vv -u nobody -d
netstat -tunlp
telnet localhost 11211
statsadd mykey 0 30 5helloget mykeyquitadd命令:
add keyname flag timeout datasizeget命令:
get keynamememcached的基本命令
get 获取数据set 创建设置键值key(可以简单把key理解为文件夹)add 添加数据(可以简单的理解为创建文件)replacememcached监听的端口号:11211采用TCP和udp协议
客户端管理工具:
perl modulecache::memcachedphpmemcachememcached(比memcache的功能更强大)c/c++
libmemcached命令行工具memadmin(图形化管理工具)
session
vim /etc/init.d/memcached
见文档整合memcached,php,nginx
vim /etc/nginx.conf
location ~.php$ { index index.php index.html;root /web/htdocs;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FIFENAME /scripts$fastcgi_script_name;include fastcgi_params;}vim /web/htdocs/index.php
<?phpphpinfo?>tar -zxvf memcache-2.2.6.tgz
cd memcache-2.2.6/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php/php-config --enable-memcachemakemake installmkdir /etc/php.d
vim /etc/php.d/memcache.iniextension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.soservice php-fpm restart
make /web/htdocs
vim /web/htdocs/test.php<?php$mem = new Memcache;$mem->connect("127.0.0.1", 11211) or die("Could not connect");$version = $mem->getVersion();
echo "Server's version: ".$version."<br/>\n";$mem->set('testkey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";$get_result = $mem->get('testkey');
echo "$get_result is from memcached server."; ?>service php-fpm resatrt
telnet localhost 11211
get testkeytar -xvf memadmin-master.zip
nginx整合memcached:
见文档server { listen 80;server_name www.magedu.com;#charset koi8-r;location / { set $memcached_key $uri; memcached_pass 127.0.0.1:11211; default_type text/html; error_page 404 @fallback;
}
location @fallback { proxy_pass ;}}将php的session会话在memcache中保存
见文档
rpmfind.net找rpm安装的网站转载于:https://blog.51cto.com/12406012/2368212