Memcached

安裝memcachedphp

[root@localhost ~]# yum install -y epel-release ; yum install -y libevent memcached libmemcachedhtml

啓動程序nginx

[root@localhost ~]# /etc/init.d/memcached startweb


啓動參數算法

[root@localhost ~]# cat /etc/sysconfig/memcachedapache

# 端口windows

PORT="11211"數組

# 用戶服務器

USER="memcached"session

# 併發數

MAXCONN="1024"

# 內存

CACHESIZE="64"

# 選項

OPTIONS=""

# 啓動參數說明,啓動參數用在OPTIONS=""配置項,如OPTIONS="-l 127.0.0.1"

# -d 啓動一個守護進程

# -m 分配給Memcached 使用的內存數量

# -l 監聽IP

# -p 監聽端口,默認是11211

# -c 最大運行併發數,默認是1024

# -P 大寫P,設置保存Memcached 的PID文件


查看進程,查看啓動參數

[root@localhost ~]# ps aux | grep memcached

498       1306  0.0  0.3  56276   820 ?        Ssl  18:46   0:00 memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid -l 127.0.0.1

查看監聽端口

[root@localhost ~]# netstat -nlp | grep memcached

tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      1551/memcached

udp        0      0 127.0.0.1:11211             0.0.0.0:*                               1551/memcached

查看運行狀態

[root@localhost ~]# memcached-tool 127.0.0.1:11211 stats

[root@localhost ~]# echo stats | nc 127.0.0.1 11211

[root@localhost ~]# memstat --servers=127.0.0.1:11211

其中nc和memstat須要安裝工具

[root@localhost ~]# yum install -y nc libmemcached

登錄,須要安裝工具telnet

[root@localhost ~]# yum install -y telnet

[root@localhost ~]# telnet 127.0.0.1 11211


簡單數據存/取過程

[root@localhost ~]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

# set 表示設置鍵屬性

# key1 表示鍵名

# 0 表示標記,能夠隨意填

# 300 表示過時時間

# 3 表示值位數

set key1 0 300 3

ab1

# 輸入完值後,加車,出現STORED表示存儲完畢

STORED

# get key1 表示查看鍵key1的值

get key1

# 顯示key1鍵屬性,再顯示key1鍵值

VALUE key1 0 3

ab1

# 當出現END表示結束

END

# 同上

set key2 219 5 7

# 上面設置值位數爲7,輸入的爲6位,顯示錯誤

asdqas

CLIENT_ERROR bad data chunk

ERROR

# 退出

quit

Connection closed by foreign host.


Mamcached 語法規則

<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n

注:\r\n在windows下是Enter鍵。

a) <command name>能夠是"set","add","replace"。

"set" 表示按照相應的<key>存儲該數據,若是沒有就增長,有就覆蓋。

"add" 表示按照相應的<key>添加該數據,但若是該<key>已經存在,則會操做失敗。

"replace" 表示按照相應的<key>替換數據,可是若是該<key>不存在則操做失敗。

b)<key>客戶端須要保存數據的key。

c) <flags> 是一個16位的無符號的整數(以十進制的方式表示)。

該標誌將和須要存儲的數據一塊兒存儲,並在客戶端get數據時返回。

客戶能夠將此標誌用作特殊用途,此標誌對服務器來講是不透明的。

d) <exptime> 過時時間。

若爲0表示存儲的數據永遠不過時(但可被服務器算法:LRU等替換)。

若是非0(unix時間或者距離此時的秒數),當過時後,服務器能夠保證用戶得不到該數據(以服務器時間爲標準)。

e) <bytes> 須要存儲的字節數,當用戶但願存儲空數據時<bytes>能夠爲0。

f) <data block> 須要存儲的內容,輸入完成後,最後客戶端須要加上"\r\n"(直接點擊Enter)做爲"命令頭"的結束標誌。


[root@localhost ~]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

# 存儲數據

set key1 1122 120 3

abc

STORED

# 讀取數據

get key1

VALUE key1 1122 3

abc

END

# 替換數據

replace key1 3344 240 2

a1

STORED

get key1

VALUE key1 3344 2

a1

END

# 刪除數據

delete key1

DELETED


php鏈接memcache

[root@localhost ~]# wget http://www.lishiming.net/data/p_w_upload/forum/memcache-2.2.3.tgz

[root@localhost ~]# tar zxvf memcache-2.2.3.tgz

[root@localhost ~]# cd memcache-2.2.3

[root@localhost memcache-2.2.3]# yum install m4 autoconf

[root@localhost memcache-2.2.3]# /usr/local/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[root@localhost memcache-2.2.3]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@localhost memcache-2.2.3]# make && make install

記住下面這行

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

[root@localhost memcache-2.2.3]# vi /usr/local/php/etc/php.ini

; extension_dir = "./"

修改成

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"

並添加行

extension="memcache.so"

檢查有沒有添加成功

[root@localhost memcache-2.2.3]# /usr/local/php/bin/php -m | grep memcache

下載測試文檔

[root@localhost memcache-2.2.3]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php

查看1.php內容

[root@localhost memcache-2.2.3]# cat 1.php

<?php

//鏈接Memcache Memcache

$mem = new Memcache;

$mem->connect("localhost", 11211);

//保存數據

$mem->set('key1', 'This is first value', 0, 60);

$val = $mem->get('key1');

echo "Get key1 value: " . $val ."<br>";

//替換數據

$mem->replace('key1', 'This is replace value', 0, 60);

$val = $mem->get('key1');

echo "Get key1 value: " . $val . "<br>";

//保存數組數據

$arr = array('aaa', 'bbb', 'ccc', 'ddd');

$mem->set('key2', $arr, 0, 60);

$val2 = $mem->get('key2');

echo "Get key2 value: ";

print_r($val2);

echo "<br>";

//刪除數據

$mem->delete('key1');

$val = $mem->get('key1');

echo "Get key1 value: " . $val . "<br>";

//清除全部數據

$mem->flush();

$val2 = $mem->get('key2');

echo "Get key2 value: ";

print_r($val2);

echo "<br>";

//關閉鏈接

$mem->close();

?>

測試,若是結果以下,表示正常

[root@localhost memcache-2.2.3]# /usr/local/php/bin/php 1.php

Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array

(

    [0] => aaa

    [1] => bbb

    [2] => ccc

    [3] => ddd

)

<br>Get key1 value: <br>Get key2 value: <br>


memcached 實現session 共享

===============================================================================

LNMP環境確認nginx有沒有啓用虛擬主機配置

[root@localhost memcache-2.2.3]# cat /usr/local/nginx/conf/nginx.conf | grep "include vhosts"

    include vhosts/*.conf;

確認網站根目錄

[root@localhost memcache-2.2.3]# cat /usr/local/nginx/conf/vhosts/myweb.conf | grep root

    root /usr/local/nginx/html;

==============================================================================

LAMP環境確認apache有沒有啓用虛擬主機配置

[root@localhost memcache-2.2.3]# cat /usr/local/apache2/conf/httpd.conf | grep ^"Include conf"

Include conf/extra/httpd-vhosts.conf

確認網站根目錄

[root@localhost memcache-2.2.3]# cat /usr/local/apache2/conf/extra/httpd-vhosts.conf | grep DocumentRoot

=====================================================================================

如下3個方法分別對應php、apache、nginx,請根據不一樣的環境(LAMP或LNMP)選擇其中一個方法配置,

1、通用。更改php配置文件

[root@localhost memcache-2.2.3]# vi /usr/local/php/etc/php.ini

把下面這行註釋起來或者刪掉

session.save_handler = files

添加如下兩行

session.save_handler = "memcache"

127.0.0.1 爲服務器IP,須要修改成實際環境中對應的IP

session.save_path = "tcp://127.0.0.1:11211"

2、LAMP環境。更改apache虛擬主機文件,在<VirtualHost *:80>和</VirtualHost>內加入內容

[root@localhost memcache-2.2.3]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

php_value session.save_handler "memcache"

php_value session.save_path "tcp://127.0.0.1:11211"

3、LNMP環境。

[root@localhost memcache-2.2.3]# vi /usr/local/php/etc/php-fpm.conf

在對應的pool內添加

php_value[session.save_handler] = memcache

php_value[session.save_path] = "tcp://127.0.0.1:11211"

====================================================================================

LAMP環境寫入測試文檔

[root@localhost memcache-2.2.3]# vi /usr/local/apache2/htdocs/masker.net.cn/session.php

<?php

session_start();

if (!isset($_SESSION['TEST'])) {

$_SESSION['TEST'] = time();

}

$_SESSION['TEST3'] = time();

print $_SESSION['TEST'];

print "<br><br>";

print $_SESSION['TEST3'];

print "<br><br>";

print session_id();

?>

[root@localhost memcache-2.2.3]# /usr/local/apache2/bin/apachectl restart

==================================================================================

LNMP環境寫入測試文檔

[root@localhost memcache-2.2.3]# vi /usr/local/nginx/html/session.php

<?php

session_start();

if (!isset($_SESSION['TEST'])) {

$_SESSION['TEST'] = time();

}

$_SESSION['TEST3'] = time();

print $_SESSION['TEST'];

print "<br><br>";

print $_SESSION['TEST3'];

print "<br><br>";

print session_id();

?>

[root@localhost memcache-2.2.3]# /etc/init.d/nginx restart

=================================================================================

[root@localhost memcache-2.2.3]# curl localhost/session.php

1452682942<br><br>1452682942<br><br>7aloo42a8dohhv3udqo1uac0p6

[root@localhost memcache-2.2.3]# curl -x127.0.0.1:80 localhost/session.php

1452684165<br><br>1452684165<br><br>99qfsqo263jdmij0r879vqolc4

[root@localhost memcache-2.2.3]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get q4c8gd5tj2jm5qdsvdeojv3ho0

VALUE q4c8gd5tj2jm5qdsvdeojv3ho0 0 37

TEST|i:1452683072;TEST3|i:1452683072;

END

quit

Connection closed by foreign host.


暫時到這裏。

相關文章
相關標籤/搜索