欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TokyoTyrant 搭建 session 服务器 博客分类: TokyoTyrant\Cabinet LuaTokyoCabinetPHPLinux.net 

程序员文章站 2024-03-23 16:28:52
...

纯记录 

 

#安装 Tokyo Cabinet

wget http://1978th.net/tokyocabinet/tokyocabinet-1.4.47.tar.gz
tar zxvf tokyocabinet-1.4.47.tar.gz
cd tokyocabinet-1.4.47
./configure --enable-off64
#32位系统需要 --enable-off64,以让它支持大于2GB的文件
make
make install
cd ..
#安装 lua
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
#要根据系统而定,直接make它会有提示
make linux
make install
ln -s /usr/local/bin/lua /usr/bin/lua
cd ..
#安装Tokyo Cabinet 的 lua 扩展
wget http://1978th.net/tokyocabinet/luapkg/tokyocabinet-lua-1.10.tar.gz
tar zxvf tokyocabinet-lua-1.10.tar.gz
cd tkoyocabinet-lua-1.10
./configure
make
make install
cd ..
#安装 Tokyo Tyrant
wget http://1978th.net/tokyotyrant/tokyotyrant-1.1.41.tar.gz
tar zxvf tokyotyrant-1.1.41.tar.gz
cd tokyotyrant-1.1.41
./configure --enable-lua --with-lua
make
make install
cd ../
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)
end
end
EOF
ulimit -SHn 51200
ttserver -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_tyrant
wget http://pecl.php.net/get/tokyo_tyrant
tar zxvf tokyo_tyrant
cd tokyo_tyrant-*
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config 
make && make install
echo '[tokyo_tyrant]
extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613//tokyo_tyrant.so"' >> /usr/local/php/lib/php.ini
cd ../

<?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"); ?>
<?php

include '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'));