TokyoTyrant 搭建 session 服务器
纯记录?
?
#安装 Tokyo Cabinetwget http://1978th.net/tokyocabinet/tokyocabinet-1.4.47.tar.gztar zxvf tokyocabinet-1.4.47.tar.gzcd tokyocabinet-1.4.47./configure --enable-off64#32位系统需要 --enable-off64,以让它支持大于2GB的文件makemake installcd ..
#安装 luawget http://www.lua.org/ftp/lua-5.1.4.tar.gztar zxvf lua-5.1.4.tar.gzcd lua-5.1.4#要根据系统而定,直接make它会有提示make linuxmake installln -s /usr/local/bin/lua /usr/bin/luacd ..
#安装Tokyo Cabinet 的 lua 扩展wget http://1978th.net/tokyocabinet/luapkg/tokyocabinet-lua-1.10.tar.gztar zxvf tokyocabinet-lua-1.10.tar.gzcd tkoyocabinet-lua-1.10./configuremakemake installcd ..
#安装 Tokyo Tyrantwget http://1978th.net/tokyotyrant/tokyotyrant-1.1.41.tar.gztar zxvf tokyotyrant-1.1.41.tar.gzcd tokyotyrant-1.1.41./configure --enable-lua --with-luamakemake installcd ../
mkdir -p /session/
#还需要一个 expire.lua 文件,这个文件用于定时将过期的 session 清理。代码如下:cat > /session/expire.lua << "EOF" function expire()local args = {}local cdate = string.format("%d", _time())table.insert(args, "addcond\0ts\0NUMLE\0" .. cdate)table.insert(args, "out")local res = _misc("search", args)if not res then_log("expiration was failed", 2)endendEOFulimit -SHn 51200ttserver -host 127.0.0.1 -port 11222 -ext /session/expire.lua -extpc expire 5.0 -thnum 8 -dmn -pid /session/session.pid -log /session/session.log -le -ulog /session/ -ulim 128m -sid 1 -rts /session/session.rts '/session/session.tct#idx=key:lex#idx=ts:dec#dfunit=8'
#安装pecl tokyo_tyrantwget http://pecl.php.net/get/tokyo_tyranttar zxvf tokyo_tyrantcd tokyo_tyrant-*/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-config make && make installecho '[tokyo_tyrant]extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613//tokyo_tyrant.so"' >> /usr/local/php/lib/php.inicd ../
<?php/** * Encoding : UTF-8 * Created on : 2011-05-15 05:18:09 */class Session { private $sessionCN = 'sessionid'; private $sessionid; private $tt; public function __construct() { define('SESS_EXPIRE', 3600); $this->sessionCN; $this->issessionid(); $this->tt = new TokyoTyrantTable('127.0.0.1', 11222); } public function get($name) { $sessvl = $this->tt->get($this->sessionid); $arr = unserialize($sessvl['vaule']); return $arr[$name]; } public function set($name, $vaule=NULL, $sessiontimeleft=SESS_EXPIRE) { $sessvl = $this->tt->get($this->sessionid); $arr = unserialize($sessvl['vaule']); if ($vaule == NULL) unset($arr[$name]); else $arr[$name] = $vaule; $this->tt->put($this->sessionid, array('vaule' => serialize($arr), 'ts' => time() + $sessiontimeleft)); } private function creatsessionid() { $this->sessionid = md5(uniqid()); setcookie($this->sessionCN, $this->sessionid, 9999999999, NULL, NULL, NULL, TRUE); } private function issessionid() { if (isset($_COOKIE[$this->sessionCN])) $this->sessionid = $_COOKIE[$this->sessionCN]; else $this->creatsessionid(); }}?使用方法<?php header("Content-type: text/html; charset=utf-8"); ?><?phpinclude 'class/Session.php';$ss = new Session();$ss->set('a', 's2a');$ss->set('b', 's2b');$ss->set('bc', 's2b');$ss->set('bc', '');var_dump($ss->get('bc'));??