1. 程式人生 > >2018-5-22

2018-5-22

memcached

21.5 memcached命令行


·memcached 語法規則

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

註:\r\n在 windows 下是 Enter 鍵


·<command name> 可以是 set, add, replace

·set 表示按照相應的 <key> 存儲該數據,沒有的時候增加,有的時候覆蓋

·add 表示按照相應的 <key> 添加該數據,但是如果該 <key> 已經存在則會操作失敗

·replace 表示按照相應的 <key> 替換數據,但是如果該 <key> 不存在則操作失敗。


·<key> 客戶端需要保存數據的key,名稱自定義


·<flags> 是一個16位的無符號的整數(以十進制的方式表示)。該標誌將和需要存儲的數據一起存儲,並在客戶端 get 數據時返回。客戶端可以將此標誌用做特殊用途,此標誌對服務器來說是不透明的。


·<exptime> 為過期的時間,單位為秒。若為0表示存儲的數據永遠不過期(但可被服務器算法:LRU 等替換)。如果非0(unix時間或者距離此時的秒數),當過期後,服務器可以保證用戶得不到該數據 (以服務器時間為標準)。


·<bytes> 需要存儲的字節數,當用戶希望存儲空數據時 <bytes> 可以為0。字節數寫幾,那麽必須輸入幾位


·<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 key3 1 100 4
1234
STORED
get key3
VALUE key3 1 4
1234
END
replace key3 1 0 5
abcde
STORED
get key3
VALUE key3 1 5
abcde
END
delete key3
DELETED
get key3
END
^]
telnet> quit
Connection closed.

(輸入錯誤需要按 Ctrl + 退格,退出按 Ctrl + ],再 quit )





21.6 memcached數據導出和導入


·重啟memcached服務的時候,最好將數據導出,重啟完之後,再將數據導入


我們先存儲一些數據:

[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key3
END
set key3 1 0 5
abcde
STORED
get key3
VALUE key3 1 5
abcde
END
set name 1 0 6
alexis
STORED
set age 1 0 2
29
STORED
set k1 1 0 5
12345
STORED
^]
telnet> quit
Connection closed.


·查看數據

[root@localhost ~]# memcached-tool 127.0.0.1:11211 dump
Dumping memcache contents
Number of buckets: 1
Number of items  : 4
Dumping bucket 1 - 4 total items
add k1 1 1526958255 5
12345
add name 1 1526958255 6
alexis
add age 1 1526958255 2
29
add key3 1 1526958255 5
abcde


·導出數據

[root@localhost ~]# memcached-tool 127.0.0.1:11211 dump > data.txt
Dumping memcache contents
Number of buckets: 1
Number of items  : 4
Dumping bucket 1 - 4 total items


·導入數據:

[root@localhost ~]# nc 127.0.0.1 11211 < data.txt
NOT_STORED
NOT_STORED
NOT_STORED
NOT_STORED

(因為之前是add添加的數據,而且已經存在了,所以導入失敗)


可以重啟 memcached ,清空數據,再導入

[root@localhost ~]# systemctl restart memcached.service
[root@localhost ~]# nc 127.0.0.1 11211 < data.txt
STORED
STORED
STORED
STORED


但是導入數據後,查看依然沒有數據

[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get k1
END
get name
END
get age
END
get key3
END
^]
telnet> quit
Connection closed.


這是因為導出數據時,在 data.txt 中的時間戳已經過期

增加一小時時間戳,並更改 data.txt 中時間戳

[root@localhost ~]# date -d "+1 hour" +%s
1526967232
[root@localhost ~]# vim data.txt

技術分享圖片

[root@localhost ~]# systemctl restart memcached.service
[root@localhost ~]# nc 127.0.0.1 11211 < data.txt
STORED
STORED
STORED
STORED
[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get k1
VALUE k1 1 5
12345
END
get name
VALUE name 1 6
alexis
END
get age
VALUE age 1 2
29
END
get key3
VALUE key3 1 5
abcde
END
^]
telnet> quit
Connection closed.

數據已經導入

因此,我們在 set 數據時,添加的 <exptime> 最好大一些。





21.7 php連接memcached


·查看已安裝php-fpm模塊:

[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -m


·下載memcached php擴展

[root@localhost src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
--2018-05-22 22:09:29--  http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
正在解析主機 www.apelearn.com (www.apelearn.com)... 47.104.7.242
正在連接 www.apelearn.com (www.apelearn.com)|47.104.7.242|:80... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:27366 (27K) [application/octet-stream]
正在保存至: “memcache-2.2.3.tgz”
100%[==============================================================================================================>] 27,366      --.-K/s 用時 0.1s
2018-05-22 22:09:29 (193 KB/s) - 已保存 “memcache-2.2.3.tgz” [27366/27366])
[root@localhost memcache-2.2.3]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@localhost memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
......
[root@localhost memcache-2.2.3]# make && make install
......
Installing shared extensions:     /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
[root@localhost memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.so  opcache.a  opcache.so
[root@localhost memcache-2.2.3]# vim /usr/local/php-fpm/etc/php.ini##編輯配置

技術分享圖片

加上extension=memcache.so

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

技術分享圖片

memcache模塊已經添加成功


·下載測試腳本測試:

[root@localhost ~]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
[root@localhost ~]# 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 ~]# /usr/local/php-fpm/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>[root@localhost ~]#
[root@localhost ~]#

或者將1.php放到某個虛擬主機根目錄下面,在瀏覽器訪問,即可看到效果

最終可以看到數據如下:

[0] => aaa

[1] => bbb

[2] => ccc

[3] => ddd

(出現以上就是正確的)


錯誤匯總:

phpize時出現錯誤:

[root@localhost memcache-2.2.3]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解決方法:

[root@localhost memcache-2.2.3]# cd ..
[root@localhost src]# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
[root@localhost src]# tar -zvxf m4-1.4.9.tar.gz
[root@localhost src]# cd m4-1.4.9/
[root@localhost m4-1.4.9]# ./configure && make && make install
[root@localhost m4-1.4.9]# cd ../
[root@localhost src]# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
[root@localhost src]# tar -zvxf autoconf-2.62.tar.gz
[root@localhost src]# cd autoconf-2.62/
[root@localhost autoconf-2.62]# ./configure && make && make install
[root@localhost autoconf-2.62]# cd ../memcache-2.2.3/
[root@localhost memcache-2.2.3]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226





21.8 memcached中存儲sessions


技術分享圖片

session.save_handler = memcache ##指定存儲類型

session.save_path = "tcp://192.168.65.128:11211" ##指定memcached服務器和端口


在/data/wwwroot/default/ 中編輯1.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 test.com]# curl localhost/1.php
1527003347<br><br>1527003347<br><br>4bfh207asaueeasdelhu79em52


·如果使用的是apache,那麽在httpd.conf中對應的虛擬主機中添加

php_value session.save_handler "memcache" php_value session.save_path "tcp://192.168.65.128:11211"


·或者php-fpm.conf對應的pool中添加

php_value[session.save_handler] = memcache

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

技術分享圖片

[root@localhost test.com]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get 4bfh207asaueeasdelhu79em52
VALUE 4bfh207asaueeasdelhu79em52 0 37
TEST|i:1527003347;TEST3|i:1527003347;
END
^]
telnet> quit
Connection closed.

第四課寫的比較匆忙,理解不充分,需要再看一遍

2018-5-22