[toc]
#php
Memcached相似於mysql同樣,一樣支持相似於mysql中建立一個庫,建立一個表,插入一個表,查看錶數據等。html
[root@xavi ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'.
手動存儲一個數據: set key2 0 30 2 其中key2 表明着 key值 0表明着 flags 標記 30表明着 過時時間 2表明着 2個字節
set key2 0 30 2 abc CLIENT_ERROR bad data chunk ERROR 12 ERROR set key1 0 20 3 abc STORED
set key1 0 30 3 cds STORED get key1 //注意這裏設定的過時時間是30秒,要再過時前查詢有效 VALUE key1 0 3 cds END
示例展現:(在telnet模式下,使用「Ctrl + delete鍵刪除」) set key3 1 100 4 //存儲一個4字節值 abcd STORED get key3 //查詢key3的值 VALUE key3 1 4 abcd END replace key3 1 200 5 //替換key3的值 abcdx STORED get key3 //查詢替換後的值 VALUE key3 1 5 abcdx END delete key3 //刪除key3的值 DELETED get key3 END
隨意創建幾條數據mysql
set name 1 0 6 xavier STORED set age 1 0 2 20 STORED set k1 1 0 5 12345 STORED
按下"ctrl+]" ,而後輸入quitnginx
首先查看下以前的操做web
cmd_get和cmd_set算法
[root@xavi ~]# memcached-tool 127.0.0.1:11211 dump Dumping memcache contents Number of buckets: 1 Number of items : 3 Dumping bucket 1 - 3 total items add k1 1 1527337188 5 12345 add name 1 1527337188 6 xavier add age 1 1527337188 2 20 [root@xavi ~]# memcached-tool 127.0.0.1:11211 dump >data.txt //能夠把導出數據重定向到txt文檔中 Dumping memcache contents Number of buckets: 1 Number of items : 3 Dumping bucket 1 - 3 total items [root@xavi ~]# cat data.txt add k1 1 1527337188 5 12345 add name 1 1527337188 6 xavier add age 1 1527337188 2 20
若nc命令不存在,yum install ncsql
[ ] 注意:導出的數據是帶有一個時間戳的,這個時間戳就是該條數據過時的時間點,若是當前時間已經超過該時間戳,那麼是導入不進去的。vim
[root@xavi ~]# systemctl restart memcached [root@xavi ~]# nc 127.0.0.1 11211 < data.txt STORED STORED STORED
[root@xavi ~]# cd /usr/local/src [root@xavi src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz [root@xavi src]# tar zxf memcache-2.2.3.tgz [root@xavi src]# cd memcache-2.2.3 [root@xavi 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
執行配置文件並編譯windows
./configure --with-php-config=/usr/local/php-fpm/bin/php-config [root@xavi memcache-2.2.3]# make [root@xavi memcache-2.2.3]# make install Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
查看生成的so文件數組
[root@xavi memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/ memcache.so opcache.a opcache.so
vim /usr/local/php-fpm/etc/php.ini 添加一行: extension="memcache.so"
/usr/local/php-fpm/sbin/php-fpm -m 發現已經出現了 memcache 模塊
編輯測試腳本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(); ?> ---------------------------- 如上腳本意爲: 鏈接memcache set一個數據, 保存 替換刪除 關閉 等。
執行這個php文件腳本
[root@xavi memcache-2.2.3]# cd [root@xavi ~]# vim 1.php [root@xavi ~]# /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>
本節應用場景爲LNMP架構下作的負載均衡。假如第一次登陸是在A服務器上,第二次登陸是在B服務器上,假如使用的是nginx代理upstream可使用ip_hash;若是使用LVS呢?解決方法是:把session不存在服務器的磁盤上,而是存在memcached上去。memcached做爲一個公共的服務器,任何web服務器均可以鏈接!
編輯php.ini添加兩行(待測試) vim /usr/local/php-fpm/etc/php.ini session.save_handler = memcache session.save_path = "tcp://192.168.0.9:11211" 或者httpd.conf中對應的虛擬主機中添加 php_value session.save_handler "memcache" php_value session.save_path "tcp://192.168.0.9:11211" 或者php-fpm.conf對應的pool中添加 php_value[session.save_handler] = memcache php_value[session.save_path] = "tcp://192.168.0.9:11211 "
[root@xavi ~]# cat .mem_se.txt <?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(); ?>
打開php.ini文件,查找session
將其移動到虛擬主機目錄中:
如何找到虛擬主機的目錄呢
vim /usr/local/nginx/conf/vhost/aaa.com.conf server { listen 80 default_server; server_name aaa.com; index index.html index.htm index.php; root /data/nginx/default; //這是root路徑 location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/xavi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/nginx/default$fastcgi_script_name; } }
[root@xavi ~]# mv /root/.mem_se.txt /data/wwwroot/www.dsf.com [root@xavi ~]# cd /data/wwwroot/www.dsf.com/ [root@xavi www.dsf.com]# cp .mem_se.txt 1.php
cp .mem_se.txt 1.php
未能驗證成功,待查明緣由