1.通过memecache扩展
配置:
session.save_handler = memcache
session.save_path = "tcp://host:port"
phpinfo中变量值:
Registered save handlers files user sqlite memcache
测试
2)test1.php
<?php
session_start();
if (!isset($_SESSION['TEST'])) {
$_SESSION['TEST'] = time();
}
$_SESSION['TEST3'] = time();
print $_SESSION['TEST'];
print $_SESSION['TEST3'];
print session_id();
输出:
1234506445
1234506445
3bbf03b9cedace4e0164a0c3d56712d1
3)test2.php
取test1.php输出的sessionid来测试
$memcache = memcache_connect('211.162.58.77', 11211);
var_dump($memcache->get('3bbf03b9cedace4e0164a0c3d56712d1'));
输出:
string(37) "TEST|i:1234503252;TEST3|i:1234503398;"
至此,测试成功!
2.通过session_savehandlr人扩展
Some PHP code for memcached session handler.
<?php
class SessionHadler
{
private static $lifetime = 0;
public static function open()
{
self::$lifetime = ini_get('session.gc_maxlifetime');
return true;
}
public static function read($id)
{
return memcached::get("sessions/{$id}");
}
public static function write($id, $data)
{
return memcached::set("sessions/{$id}", $data, self::$lifetime);
}
public static function destroy($id)
{
return memcached::delete("sessions/{$id}");
}
private function __construct(){}
public static function gc(){ return true; }
public static function close(){ return true; }
public function __destruct()
{
session_write_close();
}
}
?>
3.缺点:
memcache把内存分成很多种规格的存储块,有块就有大小,这种方式也就决定了,memcache不能完全利用内存,会产生内存碎片,如果存储块不足,还会产生内存溢出。