memcached

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 <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
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
<?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
<?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 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安裝的網站

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章