1. 程式人生 > >21.5 memcached命令行;21.6 memcached數據導出和導入;21.7 php連接

21.5 memcached命令行;21.6 memcached數據導出和導入;21.7 php連接

php連接memcached

21.5 memcached命令行

Memcached語法規則:

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

註:\r\n在windows下是Enter

2. <command name> 可以是set, add, replace

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

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

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

6. <key> 客戶端需要保存數據的key

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

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

9. <bytes> 需要存儲的字節數,當用戶希望存儲空數據時<bytes>可以為0

10. <data block>需要存儲的內容,輸入完成後,最後客戶端需要加上\r\n(直接點擊Enter)作為結束標誌。

啟動

[root@hao-01 ~]# systemctl start memcached

1. 安裝telnet

[root@hao-01 ~]# yum install -y telnet

2. telnet 指定ip 端口進入telnet界面

[root@hao-01 ~]# telnet 127.0.0.1 11211

寫入存儲格式(英語單詞表示):

set key1

flags exptime bytes

data block

提示:在telnet模式下,退格鍵需要按住Ctrl鍵

寫入存儲格式

set key1 0 30 5

12345

技術分享圖片技術分享圖片

查看key1存儲的內容(查看超出了存儲時間,會查不到的)

get key1

技術分享圖片技術分享圖片

get key1

技術分享圖片技術分享圖片

提示:在telnet模式下,退格鍵需要按住Ctrl鍵

實驗案例:

設定key1flags1 120秒到期時間 4個存儲字節

set key1 1 120 4

1234

技術分享圖片技術分享圖片

替換key1flags1 0為不限制時間 6個存儲字節

replace key1 1 0 6

123456

技術分享圖片技術分享圖片

查看key1的值

技術分享圖片技術分享圖片

刪除key1的值

delete key1

技術分享圖片技術分享圖片

21.6 memcached數據導出和導入

1. 導出memcached數據:

[root@hao-01 ~]# memcached-tool 127.0.0.1:11211 dump

技術分享圖片技術分享圖片

2. 導出memcached數據,重定向到指定文件data.txt內:

[root@hao-01 ~]# memcached-tool 127.0.0.1:11211 dump > data.txt

3. 重啟memcached

(清理之前存儲的數據,為了導入已存在的數據會不被覆蓋的)

[root@hao-01 ~]# systemctl restart memcached

4. data.txt導入到memcached

技術分享圖片技術分享圖片

若nc命令不存在,yum install nc

註意:導出的數據是帶有一個時間戳的,這個時間戳就是該條數據過期的時間點,如果當前時間已經超過該時間戳,那麽是導入不進去的

技術分享圖片技術分享圖片

21.7 php連接memcached

1. /...src/目錄下,下memcache

[root@hao-01 ~]# cd /usr/local/src/

[root@hao-01 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz

2. 解壓

[root@hao-01 src]# tar zxvf memcache-2.2.3.tgz

3. 進入memcache-2.2.3生成phpize文件

[root@hao-01 src]# cd memcache-2.2.3

#[root@hao-01 src]# yum install -y autoconf

[root@hao-01 memcache-2.2.3]# /usr/local/php-fpm/bin/phpize

4. 編譯

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

[root@hao-01 memcache-2.2.3]# echo $?

5. makemake install執行:

[root@hao-01 memcache-2.2.3]# make && make install

[root@hao-01 memcache-2.2.3]# echo $?

6. make install 生成如下文件:

[root@hao-01 memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

技術分享圖片技術分享圖片

7. 編輯php-ini

[root@hao-01 memcache-2.2.3]# vim /usr/local/php-fpm/etc/php-ini

添加內容擴展

extension=memcache.so

技術分享圖片技術分享圖片

8. 檢查有沒有memcache這個模塊:

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

21.8 memcached中存儲session

1. 下載session存儲腳本

[root@hao-01 ~]# wget http://study.lishiming.net/.mem_se.txt

2. 進入

[root@hao-01 ~]# cd /data/wwwroot/test/

3. 移動session存儲腳本,並重命名php格式腳本:

[root@hao-01 test.com]# mv /root/.mem_se.txt 1.php

4. curl訪問1.php

[root@hao-01 test.com]# curl localhost/1.php

5. 查看tmp下有沒有sess_格式的文件:

[root@hao-01 test.com]# ls -lt /tmp/

6. 編輯php-ini

[root@hao-01 test.com]# vim /usr/local/php-fpm/etc/php-ini

註釋掉這行:session.save_handler = files

添加:

session.save_handler = memcache session.save_path

"tcp://192.168.0.9:11211"

技術分享圖片技術分享圖片

本實例是在lamp/lnmp環境下實現(下面哪種沒問題,就用哪種)

1. 編輯php.ini添加兩行

session.save_handler = memcache session.save_path

"tcp://192.168.0.9:11211"

2. 或者httpd.conf中對應的虛擬主機中添加

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

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

php_value[session.save_handler] = memcache

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

7. curl訪問1.php

[root@hao-01 test.com]# curl localhost/1.php

21.5 memcached命令行;21.6 memcached數據導出和導入;21.7 php連接