httpwatch

key:value

name:jerry

http web object:memcached

http://www.magedu.com/index.html

ip

tcp,udp

telnet ip 80
get uri http/1.1 host

get uri
put /etc/issue

get,put,mget,mput

simple protocol,
http:text
ftp:text,binary

xml
procotol:
get
set

memcached:缓存服务器,但本身无法决定缓存任何数据,一半依赖于客户端,一半依赖于服务器

set key 5 60 hello

lazy:惰性,LRU,最近最少使用

内容缓存服务器:
    48bytes
     1MB 

index.html:10k
test.jpg:30k

buddy system:伙伴系统
避免内存外碎片
slab allocator:slab分配器
避免内存内碎片

memcached:不通信分布式缓存服务器

index.html/2

已用,空闲

20bytes
80bytes
72

增长因子
growth factor,1.25
48bytes:slab class,slab chunk
80bytes

工作模式:lazy(惰性)
lur(最近最小使用)
memcached又叫内存缓存服务器,缓存都存在内存中 存储大小为48byte--1MB,比如把一个内存分成每个单元48byte--1MB

index.html:10K
test.jpg:34k

buddy system:伙伴系统,避免内存外碎片
slab allocator:slab分配器,避免内存内碎片
memcached:

增长因子:growth factor,每次增长多少
如果分了一堆大小为单元为48字节的,这一堆就叫做slab class

memcached安装配置

even-driven由libevent提供,主要功能是提供事件驱动

rpm安装:
yum install libevent
yum install memcached
yum inatll cyrus-sasl

编译安装:
tar -zxvf libevent-2.0.20-stable.tar.gz
cd libevent
./configure --prefix=/usr/local/libevent
make
make install

tar -zxvf memcache-1.4.15.tar.gz
cd memcache-1.4.15
./configure --enable-sasl --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make
make install

memcached(见文档)
-p:TCP port,1111
-m #:以MB为单位,指定用于缓存的最大内存空间
-d:以服务模式运行
-u :以指定的用户身份运行memcached进程
-f :设定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
stats
add mykey 0 30 5
hello
get mykey
quit

add命令:
add keyname flag timeout datasize

get命令:
get keyname

memcached的基本命令
get 获取数据
set 创建设置键值key(可以简单把key理解为文件夹)
add 添加数据(可以简单的理解为创建文件)
replace

memcached监听的端口号:11211采用TCP和udp协议

客户端管理工具:
perl module
cache::memcached
php
memcache
memcached(比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
phpinfo
?>

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-memcache
make
make install

mkdir /etc/php.d
vim /etc/php.d/memcache.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

service php-fpm restart

make /web/htdocs
vim /web/htdocs/test.php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211) or die("Could not connect");

$version = $mem->getVersion();
echo "Server's version: ".$version."
\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)
\n";

$get_result = $mem->get('testkey');
echo "$get_result is from memcached server.";
?>

service php-fpm resatrt

telnet localhost 11211
get testkey

tar -xvf memadmin-master.zip

nginx整合memcached:
见文档
server {
listen 80;
server_name www.magedu.com;
#charset koi8-r;

access_log logs/host.access.log main;

location / {
      set $memcached_key $uri;
      memcached_pass 127.0.0.1:11211;
      default_type text/html;
      error_page 404 @fallback;

}
location @fallback {
proxy_pass http://172.16.0.1;
}
}

将php的session会话在memcache中保存

见文档
rpmfind.net找rpm安装的网站