session_set_save_handler無關的memcached保存session的方法
在memcached伺服器上
1)下載memcached
#wget http://memcached.code.com/files/meach由於memcached依賴libevent需要先安裝libevent庫,這裡直接yum安裝
#yum install *libevent*
3)安裝memcached
#./configure --prefix=/usr/local/memcached
#./configure --prefix=/usr/local/memcached# make install
4)啟動memcached
-d daemon ?-p port -p 11211 -u root
-d daemon ?-p port -u web server伺服器上
5)在web server上安裝php的memcache模組
#/usr/local/php/bin/pecl install memcache
Enable memcache session handler support? [
6)在php.ini加入以下內容:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
7)修改php. ini中的session.save_handler及session.save_path為以下內容:
session.save_handler = memcache
session.save_path = "tcp://memcached伺服器ip:11211set = "tcp://memcached伺服器ip:11211」 session.save_handler', 'memcache');ini_set('session.save_path', 'tcp://memcached伺服器ip:11211');
在php.ini中
session.save_path = tcp://127.0 .0.1:11211memcache的PECL這個擴充非常強大,可以支援failover以及分散式儲存。
使用方法很簡單,只需要在session.save_path的參數清單中,使用逗號分隔各個memcached伺服器,如:
session.save_path = "tcp://172.16.8.81:11211,tc = "tcp://172.16.8.81:11211,tcp://172.16. 8.82:11211,tcp://172.16.8.83:11211"
則保存的session會經過hash之後保存到各個memcached伺服器中,而hash的演算法memcache支援兩種,crc32以及fnv:
語
<?php ini_set("memcache.hash_function", "crc32"); $memcache = new Memcache; $memcache1 = new Memcache; $memcache2 = new Memcache; $memcache->addServer('localhost', 11211); $memcache->addServer('localhost', 11212); $memcache->flush(); $memcache1->connect('localhost', 11211); $memcache2->connect('localhost', 11212); $fp1 = fopen("mem1.txt", "w"); $fp2 = fopen("mem2.txt", "w"); for ($i = 0; $i < 1000; $i++) { $memcache->set($i, $i, 0, 1000); fwrite($fp1, $memcache1->get($i) . " "); fwrite($fp2, $memcache2->get($i) . " "); } fclose($fp1); fclose($fp2);
<?php ini_set("memcache.hash_function", "fnv"); ini_set("error_reporting", "E_CORE_ERROR"); $memcache1 = new Memcache; $memcache1->connect('localhost', 11211); $memcache1->flush(); $memcache2 = new Memcache; $memcache2->connect('localhost', 11212); $memcache2->flush(); $memcache3 = new Memcache; $memcache3->connect('localhost', 11213); $memcache3->flush(); $fp1 = fopen('mem1.txt', 'w'); $fp2 = fopen('mem2.txt', 'w'); $fp3 = fopen('mem3.txt', 'w'); for ($i = 0; $i < 1000; $i++) { session_start(); $ssid = session_id(); echo $ssid; session_register("id"); $_SESSION["id"] = $ssid; session_write_close(); fwrite($fp1, $memcache1->get($ssid) . ' '); fwrite($fp2, $memcache2->get($ssid) . ' '); fwrite($fp3, $memcache3->get($ssid) . ' '); //session_destroy(); } fclose($fp1); fclose($fp2); fclose($fp3);
<?php ini_set("memcache.hash_strategy", "consistent"); ini_set("memcache.hash_function", "crc32"); ini_set("error_reporting", "E_CORE_ERROR"); ini_set("memcache.allow_failover", "0"); $memcache1 = new Memcache; $memcache1->connect('localhost', 10001); $memcache1->flush(); $memcache2 = new Memcache; $memcache2->connect('localhost', 10002); $memcache2->flush(); $memcache3 = new Memcache; $memcache3->connect('localhost', 10003); $memcache3->flush(); $fp1 = fopen("mem1.txt", "a+"); $fp2 = fopen("mem2.txt", "a+"); $fp3 = fopen("mem3.txt", "a+"); session_start(); $ssid = session_id(); echo $ssid . "\n"; session_register("id"); $_SESSION["id"] = $ssid; //session_destroy(); session_write_close(); fwrite($fp1, $memcache1->get($ssid) . " "); fwrite($fp2, $memcache2->get($ssid) . " "); fwrite($fp3, $memcache3->get($ssid) . " "); session_destroy(); fclose($fp1); fclose($fp2); fclose($fp3);